【问题标题】:The connection was not closed. The connection's current state is open error连接未关闭。连接的当前状态是打开错误
【发布时间】:2015-02-03 16:12:25
【问题描述】:

我有循环每一行的网格视图,但我收到了这个错误:

The variable name '@UserId' has already been declared. Variable names must be unique within a query batch or stored procedure

我已经修复了连接问题,但现在我看到了新问题。请帮忙。

    protected void btn_App_Click(object sender, EventArgs e)
    {

        using (SqlConnection myCon = new SqlConnection(strConnString))
        {
            using (SqlCommand myCmd = new SqlCommand())
            {
                myCmd.Connection = myCon;
                myCon.Open();

                foreach (GridViewRow row in myGV.Rows)
                {

                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label UserId = row.FindControl("lblUsrID") as Label;
                        TextBox Start_Date = row.FindControl("txtStartDate") as TextBox;

                        TextBox End_Date = row.FindControl("txtEndDate") as TextBox;
                        CheckBox Reg_Appr = ((CheckBox)row.FindControl("txtchkUsrApp")) as CheckBox;

                        string myUserID = UserId.Text;


                             myCmd.CommandType = CommandType.Text;

                             myCmd.CommandText = "update myTable set Start_Date = @Start_Date, End_Date = @End_Date, Reg_Appr = @Reg_Appr where UserId = @UserId  ";
                             myCmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId.Text;
                             myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar).Value = Start_Date.Text;
                             myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar).Value = End_Date.Text;

                             myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit).Value = Reg_Appr.Checked;   
                             myCmd.ExecuteNonQuery();                             
                             SendActivationEmail(myUserID);
                    }
               }

                myCon.Close();


            }

       }


    }


    //send email
    private void SendActivationEmail(string myUserID)
    {
        // send email
    }

【问题讨论】:

  • 你有两次myCon.Close()。没有必要,甚至一次都没有,因为您将它包装在 using 块中。此外,您的 foreach 循环也应该封装您的 using 语句。
  • 循环内部有myCon.Open(),外部有Close()。将Open() 移到循环之外。
  • 另外,你真的希望ExecuteNonQuery() 在循环之外吗?只会更新 GridView 中最后一行的表格

标签: c# asp.net


【解决方案1】:

移动这些语句

 myCmd.Connection = myCon;
 myCmd.CommandType = CommandType.Text;
 myCon.Open();

在循环之外。

那么你将只打开一次连接,而不是每行一次。

根据您的 cmets,您还必须执行以下操作:

在 open 语句下面添加以下内容:

var uParm = myCmd.Parameters.Add("@UserId", SqlDbType.VarChar);
var sdParm = myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar);
var edParm = myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar);
var rParm = myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit);

然后你有参数添加语句之前替换为这个。

uParm.Value = UserId.Text;
sdParm.Value = Start_Date.Text;
edParm.Value = End_Date.Text;
rParm.Value = Reg_Appr.Checked; 

【讨论】:

  • 谢谢,我已经听从了你的建议,但现在我得到了新的错误:“变量名'@UserId'已经被声明。变量名在查询批处理或存储过程中必须是唯一的”。请看我最初的帖子,我已经更新了。谢谢
  • 霍根,谢谢。我这里有点糊涂了,是不是需要先在gridview中找到值再分配参数?你能把整个代码贴出来,这样我就可以添加变量了。谢谢
  • 我刚刚想通了。我所要做的就是添加这个:myCmd.Parameters.Clear();
【解决方案2】:

您在每个循环期间都打开了连接。您需要先关闭它,然后再打开它。

不过,您可能会在对 Close() 的其他调用中遇到错误,因此您需要先检查状态以验证连接是否打开。

if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label UserId = row.FindControl("lblUsrID") as Label;
                        TextBox Start_Date = row.FindControl("txtStartDate") as TextBox;

                        TextBox End_Date = row.FindControl("txtEndDate") as TextBox;
                        CheckBox Reg_Appr = ((CheckBox)row.FindControl("txtchkUsrApp")) as CheckBox;

                        string myUserID = UserId.Text;

                             myCmd.Connection = myCon;
                             myCmd.CommandType = CommandType.Text;
                             myCon.Open();
                             myCmd.CommandText = "update myTable set Start_Date = @Start_Date, End_Date = @End_Date, Reg_Appr = @Reg_Appr where UserId = @UserId  ";
                             myCmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId.Text;
                             myCmd.Parameters.Add("@Start_Date", SqlDbType.VarChar).Value = Start_Date.Text;
                             myCmd.Parameters.Add("@End_Date", SqlDbType.VarChar).Value = End_Date.Text;

                             myCmd.Parameters.Add("@Reg_Appr", SqlDbType.Bit).Value = Reg_Appr.Checked;                                
                             SendActivationEmail(myUserID);
                             // added this line
                             myCon.Close();
                    }

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多