必须声明标量变量

时间:2015-06-05 20:20:52

标签: c# sql windows forms

一直在查看此类其他问题,但找不到任何有助于我解决问题的问题。

获取错误:必须声明标量变量“@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 个答案:

答案 0 :(得分:0)

我可以看到你创建一个SqlCommand,设置查询并添加一个参数。 但是你创建一个新的SqlDataAdapter而不使用SqlCommand和它的Parameter。 您也可以使用查询创建SqlDataAdapter,但是您应该已经使用了SqlCommand,因此SqlDataAdapter将使用SqlCommand作为SelectCommand进行实例化。

daProduct = new SqlDataAdapter(FindTheProducts);

同时设置@SupplierID的值,以便您实际上要对supplierId进行过滤。

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