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(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外键,必须在实际删除用户之前删除

4

5 回答 5

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();
            }
        }
    }
}
于 2013-04-30T15:57:23.880 回答
1

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

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

于 2013-04-30T15:58:26.453 回答
1

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

为了更安全,您可以使用

if (connection.State == ConnectionState.Closed)
    connection.Open();
于 2013-04-30T15:57:06.340 回答
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();
            }
        }
     }
}
于 2013-04-30T16:00:29.080 回答
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();
        }
    }
于 2013-04-30T16:06:44.720 回答