2015-06-05 167 views
0

一直在寻找这类其他问题的负载,但找不到任何帮助我解决了我的问题的任何问题。必须声明标量变量

获取错误:必须声明标量变量“@SupplierID”。

说的投掷的错误代码:

sqlProductFind = @"SELECT * from Product WHERE SupplierID = @SupplierID"; 
     conn = new SqlConnection(connstr); 
     FindTheProducts = new SqlCommand(sqlProductFind, conn); 
     FindTheProducts.Parameters.Add("@SupplierID", SqlDbType.Int); 
     daProduct = new SqlDataAdapter(sqlProductFind, conn); //putting connection string in here 
     //cmdProduct = new SqlCommandBuilder(daProduct); 
     daProduct.FillSchema(dsProduct, SchemaType.Source, "Product"); 
+1

什么行会抛出错误? –

+2

您没有设置@SupplierID的值,只设置类型。尝试添加'FindTheProducts.Parameters [“@ SupplierID”]。Value =(some value);' – silencedmessage

+1

尝试'FindTheProducts.Parameters.AddWithValue(“@ SupplierID”,supplierId)' – Tom

回答

0

我可以看到你创建一个SqlCommand,设置查询并添加参数。 但是,然后创建一个新的SqlDataAdapter而不使用SqlCommand及其参数。 您也可以使用查询创建SqlDataAdapter,但您应该使用SqlCommand,以便SqlDataAdapter将以SqlCommand实例化为SelectCommand。

daProduct = new SqlDataAdapter(FindTheProducts); 

还设置@SupplierID的值,以便您实际上要过滤supplierId。

int supplierId = 1; //Should be an input parameter 
FindTheProducts.Parameters.Add("@SupplierID", supplierId);