[英]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.