【发布时间】:2012-12-04 19:13:15
【问题描述】:
为了运行 executeScalar,我在 Internet 上尝试了很多建议,但我收到了错误 ExecuteScalar: Connection property has not been initialized。我的INSERT 查询工作正常,问题出在executeScalar。
conn.Open();
SqlCommand cmd = new SqlCommand(
"INSERT INTO Products (Product_Name,Product_BarCode,Product_CP,Product_SP,
Product_Countainer,Product_Pcs,Product_MFGDate,
Product_ExpiryDate,Product_Grade)
Values ('" + Name.Text + "','" + BarCode.Text + "','" + CostP.Value + "','" +
SellingP.Value + "','" + Countainer.Value + "','" + Pcs.Value + "','" +
MfgDate.Value + "','" + ExpDate.Value + "','" + Grade.SelectedItem + "')",
conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Open();
cmd.Connection = conn;
cmd = new SqlCommand("SELECT SUM(Product_CP) FROM Products AS Amount");
Amount = (double)cmd.ExecuteScalar();
MessageBox.Show(Amount.ToString());
conn.Close();
【问题讨论】:
-
你有一个 SQL 注入漏洞。
-
没问题,它只是一个家庭使用的小应用程序。没有互联网连接。