我已經嘗試了很多在Internet上的建議,以便運行executeScalar
,但出現錯誤ExecuteScalar: Connection property has not been initialized
。我的INSERT
查詢工作正常,問題與executeScalar
。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 Ammount");
Ammount = (double)cmd.ExecuteScalar();
MessageBox.Show(Ammount.ToString());
conn.Close();
http://stackoverflow.com/editing-help#code – SLaks
您有一個SQL注入漏洞。 – SLaks
沒問題,它只是一個家庭使用的小應用程序。 沒有互聯網連接。 – alternatefaraz