首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL连接未关闭

问MySQL连接未关闭
EN

Stack Overflow用户
提问于 2018-07-24 11:41:58
回答 2查看 150关注 0票数 0

我在我的项目中撞到了一面墙。我有一个表单,带有一些列表框,并添加按钮。当我点击add按钮时,我得到了一个带有文本框、OK和Cancel按钮的小对话框。该应用程序连接到一个MySQL数据库。因此,每当文本更改时,程序将检查数据库中是否存在名称,禁用OK按钮,如果名称存在,文本框将变为红色,否则会使它们恢复正常。当我写作的时候没有问题,名字也不存在,当它出现的时候,它会变成红色,就像它应该的那样。这就是问题所在。在变红之后,即使我输入了一个有效的名称,它也不会恢复正常。

以下是代码:

代码语言:javascript
复制
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中的方法

代码语言:javascript
复制
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,并在关闭对话框后直接关闭该属性。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-24 11:45:59

如果selection.Read()返回true,则意味着至少有1记录。看来你是在找

代码语言:javascript
复制
   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
     }
   }
票数 3
EN

Stack Overflow用户

发布于 2018-07-24 12:02:23

如果连接没有关闭,那么在执行方法"CheckSectionName()“之前,可以尝试调用close() connection或sqldatareader。下面是一些例子,让我知道,如果它有帮助。

Approch 1:

代码语言:javascript
复制
    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:我们可以使用“使用”子句

代码语言:javascript
复制
using (MySqlDataReader selection = ConexionData.CheckSectionName(DialogTxb.Text))

方法3:将close()和dispose()添加到最终块中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51497704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档