C# Visual Studio和SQL错误连接未关闭。连接';它的当前状态是开放的
每当我单击添加按钮时,它会告诉我错误“连接未关闭。连接的当前状态为打开”。我是VisualStudio2010和SQLServer2008的新手,请提供帮助或任何建议C# Visual Studio和SQL错误连接未关闭。连接';它的当前状态是开放的,c#,visual-studio-2010,sql-server-2008,C#,Visual Studio 2010,Sql Server 2008,每当我单击添加按钮时,它会告诉我错误“连接未关闭。连接的当前状态为打开”。我是VisualStudio2010和SQLServer2008的新手,请提供帮助或任何建议 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syst
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();
}
}
}
类实现了IDisposable。所以您可以直接使用Dispose()
方法。有效的方法是使用块
要确保连接始终处于关闭状态,请打开内部连接
如下面的代码片段所示,使用块
创建一个。这样做可以确保
当代码退出块时,连接将自动关闭
using(SqlConnection connection = new SqlConnection(Helper.GetCon()))
{
// Do something
}// Here it will automatically call Dispose()
您仍然需要打开连接,但不需要关闭它,因为正如我提到的,Dispose()
方法将处理using块末尾的对象
Using (SqlConnection con = new SqlConnection(Helper.GetCon()))
{
//Your rest of the code inside here.
}
这会关闭您的连接,但您必须手动打开它。con.Open() 置于条件下,而不是con.open()
似乎只有在您设置的部分之前,GetSuppliers()
`中的一个才被执行
con.Close()
你有两个选择:
1.只打开一次连接,并在每种方法中使用它,而不关闭它,然后在应用程序上关闭它。退出:
public Add()
{
InitializeComponent();
con.Open();
}
.........
private void Add_Closing(object sender, EventArgs e)
{
con.Close();
}
在每次打开con的尝试中设置此检查:
if(con.State==ConnectionState.Closed)
{
con.Open();
}
请发布堆栈跟踪
public Add()
{
InitializeComponent();
con.Open();
}
.........
private void Add_Closing(object sender, EventArgs e)
{
con.Close();
}