Visual Studio 和 SQL 错误连接未关闭。连接的当前状态是打开的
Visual Studio and SQL Error The connection was not closed. The connection's current state is open
每当我单击添加按钮时,它都会告诉我错误 "The connection was not closed. The connection's current state is open."。我是 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();
}
}
}
SqlConnection class 实施 IDisposable
。所以你直接使用 Dispose()
方法。有效的方法是使用 using
块
要保证连接一直关闭,打开里面的连接
using block
,如以下代码片段所示。这样做可以确保
代码退出块时连接自动关闭
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();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
似乎 GetSuppliers()
` 之一直到您设置的部分才执行
con.Close()
你有两个选择:
1.Open 仅连接一次并在每个方法中使用它而不关闭它并在 Application.Exit
:
上关闭它
public Add()
{
InitializeComponent();
con.Open();
}
.........
private void Add_Closing(object sender, EventArgs e)
{
con.Close();
}
在每次尝试打开 con 时设置此检查:
if (con.State == ConnectionState.Closed)
{
con.Open();
}
每当我单击添加按钮时,它都会告诉我错误 "The connection was not closed. The connection's current state is open."。我是 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();
}
}
}
SqlConnection class 实施 IDisposable
。所以你直接使用 Dispose()
方法。有效的方法是使用 using
块
要保证连接一直关闭,打开里面的连接
using block
,如以下代码片段所示。这样做可以确保
代码退出块时连接自动关闭
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();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
似乎 GetSuppliers()
` 之一直到您设置的部分才执行
con.Close()
你有两个选择:
1.Open 仅连接一次并在每个方法中使用它而不关闭它并在 Application.Exit
:
public Add()
{
InitializeComponent();
con.Open();
}
.........
private void Add_Closing(object sender, EventArgs e)
{
con.Close();
}
在每次尝试打开 con 时设置此检查:
if (con.State == ConnectionState.Closed) { con.Open(); }