2013-04-30 45 views
0

目前我对两个不同的表执行两个查询并得到此例外,我必须打开两个连接才能执行两个不同的查询?

连接未关闭。连接的当前状态已打开。

这就是我要做的,

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为外键,必须删除用户实际

+0

考虑级联删除您的FK关系。 – spender 2013-04-30 16:00:40

+0

我该怎么做,先生,其实我会尝试谷歌它,谢谢:) – Mathematics 2013-04-30 16:03:19

回答

3

您呼叫Open()两次就可以使用。您可以删除第二个电话Open()

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(); // remove this line 
      cmd2.Parameters.Add(new SqlParameter("@userID", userID)); 
      int result2 = cmd2.ExecuteNonQuery(); 

      if (result2 == 1) 
      { 
       BindData(); 
      } 
     } 
    } 
} 
+0

在10分钟内接受的答案,谢谢 – Mathematics 2013-04-30 16:00:16

1

第二connection.Open()是不是之前被删除因为它将从第一个声明开放。

还是要在安全方面,

if (connection.State == ConnectionState.Closed) 
    connection.Open(); 
+0

它将*总是*已经打开,所以这将永远是错误的。何必? – Servy 2013-04-30 15:59:21

1

第二个connection.Open();不需要在那里。第一个就足够了;如错误消息所示,它已经打开。

一个连接可以执行多个查询,只需要对Open进行一次调用。

0
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())) 
    { 
    connection.Open(); 

    using (SqlCommand cmd = new SqlCommand(deleteStatement, connection)) 
    { 
     cmd.Parameters.Add(new SqlParameter("@userID", userID)); 
     cmd.ExecuteNonQuery(); 

    } 
     using (SqlCommand cmd2 = new SqlCommand(deleteStatement2, connection)) 
     { 
      cmd2.Parameters.Add(new SqlParameter("@userID", userID)); 
      int result2 = cmd2.ExecuteNonQuery(); 

      if (result2 == 1) 
      { 
       BindData(); 
      } 
     } 
    } 
} 
+0

这不会编译。通过此更改,当您尝试创建第二个命令时,连接现在已超出范围。 – Servy 2013-04-30 16:02:23

+0

更改代码 – 2013-04-30 16:04:15

0

我认为这将工作:

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()) 
     { 
      cmd.Connection = connection; 
      cmd.CommandType = CommandType.Text; 

      cmd.CommandText = deleteStatement; 
      cmd.Parameters.Add(new SqlParameter("@userID", userID)); 
      cmd.ExecuteNonQuery(); 


      cmd.CommandText = deleteStatement2; 
      int result = cmd.ExecuteNonQuery(); 

      if (result == 1) BindData(); 
     } 
    } 
相关问题