2013-04-30 46 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(); 
     } 
    } 
相關問題