简体   繁体   English

必须声明标量变量

[英]Must declare Scalar Variable

Been looking over loads of other questions of this type but couldn't find any that helped me solved my problem. 一直在寻找其他此类问题,但找不到任何可以帮助我解决问题的方法。

Getting the error: Must declare the scalar variable "@SupplierID". 收到错误:必须声明标量变量“ @SupplierID”。

The code that's throwing the error: 引发错误的代码:

 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");

I can see you creating a SqlCommand, setting the query and adding a parameter. 我可以看到您创建了一个SqlCommand,设置了查询并添加了一个参数。 But then you create a new SqlDataAdapter without using the SqlCommand and it's Parameter. 但是随后您无需使用SqlCommand及其参数就可以创建一个新的SqlDataAdapter。 You create the SqlDataAdapter with the query also, but you should have used the SqlCommand so the SqlDataAdapter will be instantiated with the SqlCommand as the SelectCommand. 您也可以使用查询创建SqlDataAdapter,但是您应该已经使用SqlCommand,因此SqlDataAdapter将以SqlCommand作为SelectCommand实例化。

daProduct = new SqlDataAdapter(FindTheProducts);

Also set the value of @SupplierID so you are actually going to filter on a supplierId. 还要设置@SupplierID的值,以便您实际上将要根据SupplierId进行过滤。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM