目前我對兩個不同的表執行兩個查詢並得到此例外,我必須打開兩個連接才能執行兩個不同的查詢?
連接未關閉。連接的當前狀態已打開。
這就是我要做的,
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["userID"].ToString());
string deleteStatement = "Delete from Table1 where [email protected]";
string deleteStatement2 = "Delete from Table2 where [email protected]";
using (SqlConnection connection = new SqlConnection(CS()))
using (SqlCommand cmd = new SqlCommand(deleteStatement, connection))
{
connection.Open();
cmd.Parameters.Add(new SqlParameter("@userID", userID));
cmd.ExecuteNonQuery();
using (SqlCommand cmd2 = new SqlCommand(deleteStatement2, connection))
{
connection.Open();
cmd2.Parameters.Add(new SqlParameter("@userID", userID));
int result2 = cmd2.ExecuteNonQuery();
if (result2 == 1)
{
BindData();
}
}
}
}
我這樣做是因爲表2具有userID
爲外鍵,必須刪除用戶實際
考慮級聯刪除您的FK關係。 – spender 2013-04-30 16:00:40
我該怎麼做,先生,其實我會嘗試谷歌它,謝謝:) – Mathematics 2013-04-30 16:03:19