C#代码错误-未关闭连接. [英] Error in C# code - The connection was not closed.

查看:87
本文介绍了C#代码错误-未关闭连接.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时,它将引发错误消息连接未关闭.连接的当前状态为打开".在尝试打开连接的尝试块之后.
有人可以帮我解决这个问题吗?


When I run this code it throws an error sayng "The connection was not closed. The connection''s current state is open." exactly after try block where I open the connection.
Can anybody help me to solve this please??


private void btnAdd_Click(object sender, EventArgs e)
{
    try
    {
        // Connection Open

        newConnection.conn.Open();


        string updSql = "Insert Into tblCustomer values (@CustomerID, @FirstName, @LastName, @Age, @DOB, @Address, @NID, @Registered_Date, @Registration_Fee, @Fine, @No_Of_Books, @Borrow_Date, @Date_should_Return, @Return_Date, @Over_Due, @Fine_per_Day)";

        SqlCommand cmdupd = new SqlCommand(updSql, newConnection.conn);


        cmdupd.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.NChar));
        cmdupd.Parameters["@CustomerID"].Value = txtCustomerID1.Text;

        cmdupd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar,100));
        cmdupd.Parameters["@FirstName"].Value = txtFirstName.Text;

        cmdupd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 100));
        cmdupd.Parameters["@LastName"].Value = txtLastName.Text;

        cmdupd.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 250));
        cmdupd.Parameters["@Address"].Value = txtAddress.Text;

        cmdupd.Parameters.Add(new SqlParameter("@DOB", SqlDbType.DateTime));
        cmdupd.Parameters["@DOB"].Value = DateTime.Today;

        cmdupd.Parameters.Add(new SqlParameter("@Age", SqlDbType.Decimal));
        cmdupd.Parameters["@Age"].Value = Convert.ToDecimal(txtAge.Text);

        cmdupd.Parameters.Add(new SqlParameter("@NID", SqlDbType.NChar));
        cmdupd.Parameters["@NID"].Value = txtNID.Text;

        cmdupd.Parameters.Add(new SqlParameter("@Registered_Date", SqlDbType.DateTime));
        cmdupd.Parameters["@Registered_Date"].Value = DateTime.Today;

        cmdupd.Parameters.Add(new SqlParameter("@Registration_Fee", SqlDbType.Decimal));
        cmdupd.Parameters["@Registration_Fee"].Value = Convert.ToDecimal(txtRegistFee.Text);

        cmdupd.Parameters.Add(new SqlParameter("@Fine", SqlDbType.Decimal));
        cmdupd.Parameters["@Fine"].Value = 0;

        cmdupd.Parameters.Add(new SqlParameter("@No_Of_Books", SqlDbType.Decimal));
        cmdupd.Parameters["@No_Of_Books"].Value = 0;

        cmdupd.Parameters.Add(new SqlParameter("@Borrow_Date", SqlDbType.DateTime));
        cmdupd.Parameters["@Borrow_Date"].Value = DateTime.Today;

        cmdupd.Parameters.Add(new SqlParameter("@Date_should_Return", SqlDbType.DateTime));
        cmdupd.Parameters["@Date_should_Return"].Value = DateTime.Today;

        cmdupd.Parameters.Add(new SqlParameter("@Return_Date", SqlDbType.DateTime));
        cmdupd.Parameters["@Return_Date"].Value = DateTime.Today;

        cmdupd.Parameters.Add(new SqlParameter("@Over_Due", SqlDbType.Decimal));
        cmdupd.Parameters["@Over_Due"].Value = 0;

        cmdupd.Parameters.Add(new SqlParameter("@Fine_per_Day", SqlDbType.Decimal));
        cmdupd.Parameters["@Fine_per_Day"].Value = 0;


        cmdupd.ExecuteNonQuery();
        newConnection.conn.Close();
        MessageBox.Show("New Customer Detail Updated.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    catch (SqlException e1)
    {
        Console.WriteLine(e1.ErrorCode);
        Console.WriteLine(e1.Message);
        if (e1.Number == 2627)
        {
            MessageBox.Show("Duplicate Customer ID.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            newConnection.conn.Close();
            this.txtCustomerID1.Focus();
        }

    }

}

推荐答案

好吧,看看它是否类似于此处 [
Well, just see if it is something like this issue[^].

Surely, you have already opened up the connection and trying to re-open. Use DEBUGGER and see what''s the case.
Just in case, if needed, have a look at various experiences and reason of this error here[^]. Using try-catch-finally and ''using'' blocks are few of the best practices related to Connections.


请使用finally块关闭连接.
也许代码中的某些地方引发了异常,因此无法关闭连接.为确保连接状态,请使用

Please use finally block to Close the connection.
Maybe there are some places in your code raised an exception, so that the connection cannot be Closed. To ensure the state of the Connection, please use

if (conn.State == ConnectionState.Closed)
        {
            conn.Open()
        }




问候,
安迪




Regards,
Andy


这篇关于C#代码错误-未关闭连接.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆