我在我的项目中撞到了一面墙。我有一个表单,带有一些列表框,并添加按钮。当我点击add按钮时,我得到了一个带有文本框、OK和Cancel按钮的小对话框。该应用程序连接到一个MySQL数据库。因此,每当文本更改时,程序将检查数据库中是否存在名称,禁用OK按钮,如果名称存在,文本框将变为红色,否则会使它们恢复正常。当我写作的时候没有问题,名字也不存在,当它出现的时候,它会变成红色,就像它应该的那样。这就是问题所在。在变红之后,即使我输入了一个有效的名称,它也不会恢复正常。
以下是代码:
private void DialogTxb_TextChanged(object sender, EventArgs e)
{
//ConnexionData class where i do all the SQL manipulation
MySqlDataReader selection = ConexionData.CheckSectionName(DialogTxb.Text);
while (selection.Read())
{
if (selection.HasRows)
{
DialogOk.Enabled = false;
toolTip1.Show("La section existe", TooltibLb);
DialogTxb.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffaaaa");
}
else
{
toolTip1.Hide(TooltibLb);
DialogTxb.BackColor = Color.White;
DialogOk.Enabled = true;
}
}
ConexionData.ConexionClose();//Method to close connection
}我想我知道问题出在哪里,但不知道为什么会发生,也不知道如何解决。如果我只是退出表单并尝试执行其他任何操作,比如从列表框中选择另一个元素将触发一些数据库交互,则程序close和visual studio将为我提供有关错误的信息:“连接已经打开”。我试着在代码的其他时刻结束,在互联网上寻找一些解决方案,尝试使用MysqlConection.ClearAllPools(),但仍然是同一个问题。
连接在应用程序的其他部分正确地打开和关闭。
感谢您的关注。
编辑
以下是ConexionData中的方法
class ConnexionData
{
private static MySqlConnection Connexion;
public static MySqlCommand Command;
//Many other methods
//.......
public static void ConnexionClose()
{
Connexion.Close();
}
public static MySqlDataReader CheckSectionName(string name)
{
Connexion.Open();
string checkSectionName = ("Select sectionName from section where sectionName = '" + name + "'");
Command.CommandText = checkSectionName;
Reader = Command.ExecuteReader();
return Reader;
}
}我在程序的许多部分使用了Connexion.Close()。我有2个database视图,以及一些从数据库加载数据的列表框。我打开并关闭这些DGV和更改列表框中的值,所有这些都可以正常工作。然后,我尝试我的小表单在这些表上插入一个新的值,测试它并关闭它(实际上我没有插入任何东西,我只是关闭它并且在close事件中有一个ConexionData.Close() ),在这里连接问题开始了。
编辑解决方案
我终于解决了这个问题。我将Private MysqlConection转换为public,并在关闭对话框后直接关闭该属性。
发布于 2018-07-24 11:45:59
如果selection.Read()返回true,则意味着至少有1记录。看来你是在找
private void DialogTxb_TextChanged(object sender, EventArgs e) {
try {
//TODO: check ConexionData.CheckSectionName (re-)creates connection if required
using (MySqlDataReader selection = ConexionData.CheckSectionName(DialogTxb.Text)) {
if (selection.Read()) {
// Name exists (we've read it)
DialogOk.Enabled = false;
toolTip1.Show("La section existe", TooltibLb);
DialogTxb.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffaaaa");
}
else {
// Name doesn't exist: the cursor is empty (so we've failed to read it)
toolTip1.Hide(TooltibLb);
DialogTxb.BackColor = Color.White;
DialogOk.Enabled = true;
}
}
}
finally {
// finally: rain or shine the connection should be closed
ConexionData.ConexionClose(); // Method to close connection
}
}发布于 2018-07-24 12:02:23
如果连接没有关闭,那么在执行方法"CheckSectionName()“之前,可以尝试调用close() connection或sqldatareader。下面是一些例子,让我知道,如果它有帮助。
Approch 1:
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
if(sqlConn.State!= System.Data.ConnectionState.Closed)
{
sqlConn.Close();
}
System.Data.SqlClient.SqlDataReader SqlReader= new System.Data.SqlClient.SqlDataReader();
if(!SqlReader.IsClosed)
{
SqlReader.Close();
}
MySqlDataReader selection = ConexionData.CheckSectionName(DialogTxb.Text);Approch 2:我们可以使用“使用”子句
using (MySqlDataReader selection = ConexionData.CheckSectionName(DialogTxb.Text))方法3:将close()和dispose()添加到最终块中。
https://stackoverflow.com/questions/51497704
复制相似问题