在我的Windows窗体上出现以下错误:连接未关闭。连接的当前状态是打开的。 [英] geeting following error on my windows form:The connection was not closed. The connection's current state is open.
问题描述
private void textBox5_TextChanged(对象发件人,EventArgs e)
{
objConn1.Open();
int nQty = 0 ;
int nActualQty;
if (!int.TryParse(textBox5.Text, out nActualQty))
{
// 向用户报告问题 - 输入错误。
}
string sql3 = 从dbo.RateMouldQuantity中选择数量,其中ratechart =' + comboBox5.SelectedValue.ToString()+ '跨度>;
SqlCommand com = new SqlCommand(sql3,objConn1);
SqlDataReader objQty = com.ExecuteReader();
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty [ 数量]);
}
if (nQty < nActualQty)
{
MessageBox.Show( 数量更大);
}
textBox5.Clear();
objConn1.Close();
}
可能与你的
< pre lang =cs> if (!int.TryParse(textBox5.Text, out nActualQty))
{
// 向用户报告问题 - 他输错了。
}部分 - 你没有显示它,但是如果你没有关闭那里的连接,它将保持打开状态(因为我假设你在那个位置做了回报) 。
这里有两种选择:
1)使用此代码打开:
if (objConn1.State!= ConnectionState.Open)
{
objConn1.Open();
}
2)每次打开连接时都使用try..finalize块:
尝试
{
objConn1.Open();
...
}
最后
{
if (objConn1.State == ConnectionState.Open)
{
objConn1.Close();
}
}
我会同时执行这两项操作,并将验证代码移到try块之外(那里如果您不打算使用它,那么在连接中打开是没有意义的):
private void textBox5_TextChanged( object sender,EventArgs e)
{
int nQty = 0 ;
int nActualQty;
if (!int.TryParse(textBox5.Text, out nActualQty))
{
// 向用户报告问题 - 输入错误。
}
其他
{
尝试
{
if (objConn1.State!= ConnectionState.Open)
{
objConn1.Open();
}
string sql3 = 从dbo.RateMouldQuantity中选择qty,其中ratechart =' + comboBox5.SelectedValue.ToString()+ 跨度>;
使用(SqlCommand com = new SqlCommand(sql3,objConn1))
{
使用(SqlDataReader objQty = com.ExecuteReader())
{
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty [ 数量跨度>]);
}
if (nQty < nActualQty)
{
MessageBox.Show( 数量更大);
}
}
}
textBox5.Clear();
}
最后
{
objConn1.Close();
}
}
}(我还使用块投入了几个
,因为你应该处理SQL命令和读取器)
试试这个。
private void textBox5_TextChanged( object sender,EventArgs e)
{
if (objConn1.State == ConnectionState.Open)
{
objConn1.Close();
}
objConn1.Open();
int nQty = 0 ;
int nActualQty;
if (!int.TryParse(textBox5.Text, out nActualQty))
{
// 向用户报告问题 - 他输错了。
}
string sql3 = 从dbo中选择数量.RateMouldQuantity where ratechart =' + comboBox5.SelectedValue.ToString()+ ';
SqlCommand com = new SqlCommand(sql3,objConn1);
SqlDataReader objQty
try
{
objQty = com.ExecuteReader();
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty [ 数量]);
}
if (nQty < nActualQty)
{
MessageBox.Show( 数量更大);
}
} catch (Exception exp){}
if (objQty!= null )
{
objQty.Close(); // 完成使用后,您需要关闭Reader类。
}
textBox5.Clear();
objConn1.Close();
}
string sql3 = 从dbo.RateMouldQuantity中选择数量,其中ratechart =' + comboBox5.SelectedValue.ToString() + ';
SqlCommand com = new SqlCommand(sql3,objConn1);
SqlDataReader objQty = com.ExecuteReader();
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty [ 数量]);
}
if (nQty < nActualQty)
{
MessageBox.Show( 数量更大);
}
textBox5.Clear();
com.Connection.Close(); // 添加此...
objConn1.Close();
private void textBox5_TextChanged(object sender, EventArgs e)
{
objConn1.Open();
int nQty = 0;
int nActualQty;
if (!int.TryParse(textBox5.Text, out nActualQty))
{
// Report problem to user - he typed wrong.
}
string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'";
SqlCommand com = new SqlCommand(sql3, objConn1);
SqlDataReader objQty = com.ExecuteReader();
if (objQty.Read())
{
nQty = Convert.ToInt32(objQty["Qty"]);
}
if (nQty < nActualQty)
{
MessageBox.Show("Qty is greater");
}
textBox5.Clear();
objConn1.Close();
}
Probably, it is to do with your
if (!int.TryParse(textBox5.Text, out nActualQty)) { // Report problem to user - he typed wrong. }Section - you don''t show it, but if you do not close the connection there, it will remain open (since I assume you do a return in that bit).
There are two alternatives here:
1) Use this code for the open:
if (objConn1.State != ConnectionState.Open) { objConn1.Open(); }
2) Use a try..finalize block each time you open the connection:
try { objConn1.Open(); ... } finally { if (objConn1.State == ConnectionState.Open) { objConn1.Close(); } }
I would do both, and also move the validation code outside the try block (there is no point in opening in the connection if you aren''t going to use it):
private void textBox5_TextChanged(object sender, EventArgs e) { int nQty = 0; int nActualQty; if (!int.TryParse(textBox5.Text, out nActualQty)) { // Report problem to user - he typed wrong. } else { try { if (objConn1.State != ConnectionState.Open) { objConn1.Open(); } string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'"; using (SqlCommand com = new SqlCommand(sql3, objConn1)) { using (SqlDataReader objQty = com.ExecuteReader()) { if (objQty.Read()) { nQty = Convert.ToInt32(objQty["Qty"]); } if (nQty < nActualQty) { MessageBox.Show("Qty is greater"); } } } textBox5.Clear(); } finally { objConn1.Close(); } } }(I have also thrown in a couple of
using
blocks, because you should dispose SQL commands and readers)
try this.
private void textBox5_TextChanged(object sender, EventArgs e) { if( objConn1.State == ConnectionState.Open) { objConn1.Close(); } objConn1.Open(); int nQty = 0; int nActualQty; if (!int.TryParse(textBox5.Text, out nActualQty)) { // Report problem to user - he typed wrong. } string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'"; SqlCommand com = new SqlCommand(sql3, objConn1); SqlDataReader objQty try { objQty = com.ExecuteReader(); if (objQty.Read()) { nQty = Convert.ToInt32(objQty["Qty"]); } if (nQty < nActualQty) { MessageBox.Show("Qty is greater"); } } catch(Exception exp){} if( objQty != null) { objQty.Close(); // You need to close the Reader class once you finished using it. } textBox5.Clear(); objConn1.Close(); }
string sql3 = "select qty from dbo.RateMouldQuantity where ratechart= '" + comboBox5.SelectedValue.ToString() + "'"; SqlCommand com = new SqlCommand(sql3, objConn1); SqlDataReader objQty = com.ExecuteReader(); if (objQty.Read()) { nQty = Convert.ToInt32(objQty["Qty"]); } if (nQty < nActualQty) { MessageBox.Show("Qty is greater"); } textBox5.Clear(); com.Connection.Close(); // add this... objConn1.Close();
这篇关于在我的Windows窗体上出现以下错误:连接未关闭。连接的当前状态是打开的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!