当我失去互联网时,c# MySQL连接失败

本文关键字:MySQL 连接 失败 失去 互联网 | 更新日期: 2023-09-27 18:03:36

我正在运行一个c#程序,它连接到自己网络上的本地MySQL服务器。这一切都很好,直到我失去互联网,然后c#将不连接到服务器,尽管被IP引用(见下面的代码)。

 public bool IsConnect()
    {
        bool result = true;
        if (Connection == null)
        {
            if (String.IsNullOrEmpty(databaseName))
                result = false;
            string connstring = string.Format("Server=192.168.0.254; database={0}; UID=show; password=", "");
            try
            {
                connection = new MySqlConnection(connstring);
                connection.Open();
                result = true;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        // MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;
                    case 1045:
                        // MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
            }
        }
        return result;
    }

当我失去互联网时,c# MySQL连接失败

您没有检查您的databaseName

public bool IsConnect()
    {
        bool result = true;
        if (Connection == null)
        {
            if (String.IsNullOrEmpty(databaseName))
                result = false;
            string connstring = string.Format("Server=192.168.0.254; database={0}; UID=show; password=", "");
            try
            {
            //Database name is not empty 
            if(result)
                {
                connection = new MySqlConnection(connstring);
                connection.Open();
                result = true;
                }
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        // MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;
                    case 1045:
                        // MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
            }
        }
        return result;
    }

正想多发点信息,却突然找到了答案。

添加

skip-name-resolve

To my.cnf停止MySQL试图通过DNS解析客户端,这导致了我的问题