Visual Studio和SQL错误连接未关闭。连接的当前状态是打开的

时间:2015-02-19 10:32:07

标签: c# visual-studio-2010 sql-server-2008

它告诉我错误"连接未关闭。连接的当前状态是开放的。"每当我点击添加按钮。我是Visual Studio 2010和Sql Server 2008的新手,帮助或任何建议都可以。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MRP.SupplierMaterial
{
    public partial class Add : Form
    {

        SqlConnection con = new SqlConnection(Helper.GetCon());

        public Add()
        {
            InitializeComponent();
        }

       void GetSuppliers()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT CompanyName, ContactPerson, Phone, Mobile, Status, DateAdded, DateModified FROM Suppliers";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new System.Data.DataSet();
            da.Fill(ds, "Suppliers");
            cmbSupplierID.DataSource = ds.Tables["Suppliers"];
            cmbSupplierID.DisplayMember = "CompanyName";
            cmbSupplierID.ValueMember = "SupplierID";
            con.Close();
        }

       void GetMaterials()
       {
           con.Open();
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandText = "SELECT Materials.MaterialID, " +
               "Materials.Name + ' (' + UnitID.UnitMeasure + ')' AS MaterialName " +
               "FROM Materials INNER JOIN UnitID ON Materials.UnitID = UnitID.UnitID";
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new System.Data.DataSet();
           da.Fill(ds, "Materials");
           cmbMaterialID.DataSource = ds.Tables["Materials"];
           cmbMaterialID.DisplayMember = "MaterialName";
           cmbMaterialID.ValueMember = "MaterialID";
           con.Close();
       }

       private void btnAdd_Click(object sender, EventArgs e)
       {
           con.Open();
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandText = "INSERT INTO SupplierMaterials VALUES (@SupplierID, @MaterialID);";
           cmd.Parameters.AddWithValue("@SupplierID", cmbSupplierID.SelectedValue);
           cmd.Parameters.AddWithValue("@MaterialID", cmbMaterialID.SelectedValue);
           cmd.ExecuteNonQuery();
           con.Close();
       }

       private void Add_Load(object sender, EventArgs e)
       {
           GetMaterials();
           GetSuppliers();

       }

    }
}

4 个答案:

答案 0 :(得分:0)

SqlConnection类实现IDisposable。所以你直接使用Dispose()方法。有效的方法是使用using

要确保始终关闭连接,请打开内部连接     using block的一个,如下面的代码片段所示。这样做可以确保     当代码退出块时,连接自动关闭

using(SqlConnection connection = new SqlConnection(Helper.GetCon()))
{
// Do something 
}// Here it will automatically call Dispose()

您仍然需要打开连接但不需要关闭它,因为我提到Dispose()方法会处理使用块末尾的对象

答案 1 :(得分:0)

Using (SqlConnection con = new SqlConnection(Helper.GetCon()))
{
 //Your rest of the code inside here.
}

这会关闭您的连接,但您必须手动打开它。 con.Open();

答案 2 :(得分:0)

置于条件下而不是con.open();

if (con.State == ConnectionState.Closed)
   {
        con.Open();
   }

答案 3 :(得分:0)

似乎GetSuppliers()`之一在你设置的部分

之前没有被执行
con.Close()

您有两种选择:

1.仅打开连接一次并在每种方法中使用它而不关闭它并在Application.Exit上关闭它:

     public Add()
            {
                InitializeComponent();
                con.Open();
            }
.........
 private void Add_Closing(object sender, EventArgs e)
       {
         con.Close();
       }
  1. 在每次打开con:

    的尝试中设置此检查

    if(con.State == ConnectionState.Closed)        {             con.Open();        }