我必须打开两个连接来执行两个不同的查询?

时间:2013-04-30 15:55:43

标签: c# gridview webforms sqldataadapter

目前我正在针对两个不同的表执行两个查询并获得此异常,

  

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

这就是我想要做的,

    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 userID=@userID";
        string deleteStatement2 = "Delete from Table2 where userID=@userID";

        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();
                }
            }
        }
    }

我这样做是因为Table2有userID作为外键,必须在删除用户之前删除

5 个答案:

答案 0 :(得分: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 userID=@userID";
    string deleteStatement2 = "Delete from Table2 where userID=@userID";

    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();
            }
        }
    }
}

答案 1 :(得分:1)

第二个connection.Open()不是必需的,因为它将从第一个声明中打开。

仍然是更安全的一面,你可以使用

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

答案 2 :(得分:1)

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

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

答案 3 :(得分: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 userID=@userID";
    string deleteStatement2 = "Delete from Table2 where userID=@userID";

    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();
            }
        }
     }
}

答案 4 :(得分: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 userID=@userID";
        string deleteStatement2 = "Delete from Table2 where userID=@userID";

        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();
        }
    }