我很難從SQL數據庫中刪除行,我沒有收到任何錯誤,它似乎工作正常,但沒有被刪除。當我運行代碼它會輸出「名稱已被刪除」從表中刪除一行似乎失敗
感謝您的任何幫助。
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\MyDB.mdf;Integrated Security=True";
try
{
conn.Open();
SqlCommand Command = conn.CreateCommand();
Command.CommandText = "DELETE FROM Contacts WHERE [First Name] = '@Name';";
Command.Parameters.AddWithValue("@Name", DropDownList1.SelectedValue);
Command.ExecuteNonQuery();
TextBox1.Text = DropDownList1.SelectedValue + " Has Been Deleted";
}
catch (Exception ex)
{
TextBox1.Text = "Nope";
}
finally
{
conn.Close();
}
的ExecuteNonQuery返回一個數字,表示有多少行已被刪除,這樣你就不會得到一個異常,如果它是沒有的。 – Andrew
請刪除'@ Name'周圍的''引號,然後重試 – Amitd
您不需要在查詢中使用'@ Name'周圍的引號 – valverij