[英]Must declare Scalar Variable
一直在寻找其他此类问题,但找不到任何可以帮助我解决问题的方法。
收到错误:必须声明标量变量“ @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");
我可以看到您创建了一个SqlCommand,设置了查询并添加了一个参数。 但是随后您无需使用SqlCommand及其参数就可以创建一个新的SqlDataAdapter。 您也可以使用查询创建SqlDataAdapter,但是您应该已经使用SqlCommand,因此SqlDataAdapter将以SqlCommand作为SelectCommand实例化。
daProduct = new SqlDataAdapter(FindTheProducts);
还要设置@SupplierID的值,以便您实际上将要根据SupplierId进行过滤。
int supplierId = 1; //Should be an input parameter
FindTheProducts.Parameters.Add("@SupplierID", supplierId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.