更新时间:2022-10-18 14:15:43
我找到了问题解决方案!
我应该传递这样的整个命令参数SqlDataAdapter ad = new SqlDataAdapter (COM);
不仅仅是com.CommandText ......这很愚蠢....但我失去了这么多时间.. 。
从此示例 C#SqlParameter [ ^ ]看起来你应该使用SqlParameter parametrNazwisko = new SqlParameter( Nazwisko,SqlDbType.VarChar, 50 );代替
SqlParameter parametrNazwisko = new SqlParameter( @ Nazwisko,SqlDbType.VarChar, 50 );
因此,请从参数名称中删除@
。
,因为您使用存储过程作为SQL st你不需要用@符号传递参数名称只是传递参数的名称SQL将完成剩下的工作。SqlParameter parametrNazwisko =新的SqlParameter(Nazwisko,Dziubak);
Hello
I searched through this site and found some topic about my problem but still cannot make this work....
I am simply trying to retrieve data from SQL Database using a stored procedure.
I am passing one parameter that is a SqlDbType.Varchar(50) type, but get back an SQL error:
Procedure or function 'WyszukajPrzesylki' expects parameter '@Nazwisko', which was not supplied.
What am I doing wrong? Store procedure works if I test it in SSMS ...
It even works when I type
SqlCommand com = new SqlCommand("WyszukajPrzesylki @Nazwisko = Example", con);
You are my last hope guys... I've already lost 2 days with this problem and cannot get any further... don't have any problems with inserting and updating records using c# ... just this....
So here is the code of SP
ALTER PROCEDURE [dbo].[WyszukajPrzesylki]
-- Add the parameters for the stored procedure here
@Nazwisko varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- SELECT * FROM Przesylki WHERE (NazwiskoAdresata = @NazwiskoAdresata)
SELECT * FROM Przesylki WHERE (Przesylki.NazwiskoAdresata = @Nazwisko)
END
and action of the button:
SqlConnection con = new SqlConnection(Properties.Settings.Default.Monitoring_PrzesylekString1);
SqlCommand com = new SqlCommand("WyszukajPrzesylki", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter parametrNazwisko = new SqlParameter("@Nazwisko", SqlDbType.VarChar, 50);
parametrNazwisko.Value = "Dziubak";
com.Parameters.Add(parametrNazwisko);
SqlDataAdapter ad = new SqlDataAdapter(com.CommandText, con);
DataSet ds = new DataSet();
ad.Fill(ds, "id");
con.Close();
DataTable datatableA = ds.Tables[0];
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "id";
I found problem solution!
I should pass whole command parameter like thisSqlDataAdapter ad = new SqlDataAdapter(com);
not just com.CommandText ... it was stupid.... but I lost so much time...
From this sample C# SqlParameter[^] it looks like you should useSqlParameter parametrNazwisko = new SqlParameter("Nazwisko", SqlDbType.VarChar, 50);in stead off
SqlParameter parametrNazwisko = new SqlParameter("@Nazwisko", SqlDbType.VarChar, 50);
So remove the '@
' from the parameter name.
since you are using the stored procedure as the SQL statement you dont need to pass the parameter name with @ symbol just pass the name of the parameter SQL will do the rest.SqlParameter parametrNazwisko = new SqlParameter("Nazwisko", "Dziubak");