-1
註釋代碼的工作,但不盡快刷新,這並不是cmd.Parameters.AddWithValue("@CustomerID2", '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "');
在字符文字字符太多,我刪除語法
private void button4_Click_2(object sender, EventArgs e)
{
disp_data();
SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;Initial Catalog=rick_inventiory;Integrated Security=True");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete from tbl_Orders where CustomerID2 = '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "'";
con.Open();
// cmd.Parameters.AddWithValue("@CustomerID2", txtCustomerID2.Text);
cmd.Parameters.AddWithValue("@CustomerID2", '" + dataGridView5.SelectedRows[0].Cells[0].Value.ToString() + "');
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted Successfully");
}
C#中的單引號用於字符。對於字符串,你需要使用:雙引號「 –
[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不要**連接你的SQL語句 - 使用**參數化查詢**來代替以避免SQL注入 - 檢查[Little Bobby Tables](https://xkcd.com/327/) –