目前我对两个不同的表执行两个查询并得到此例外,我必须打开两个连接才能执行两个不同的查询?
连接未关闭。连接的当前状态已打开。
这就是我要做的,
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