gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:30:50 24 4
gpt4 key购买 nike

我正在运行一个 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;
}

最佳答案

您没有检查您的数据库名称。

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;
}

关于当我失去互联网时,C# MySQL 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com