且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查在C#MySQL连接状态

更新时间:2023-11-26 11:03:10

我认为错误应该在连接字符串。首先检查你的连接字符串。

I think error should be in connection string. Check your connection string first.

如果连接字符串是正确的,有一些另外一个问题你可以试试下面。

if connection string is correct and there is some another issue try something like below.

var sqlCon= new SqlConnection(Properties.Settings.Default.sString);
var mySQLCon= new MySqlConnection(Properties.Settings.Default.dString);
sqlCon.Open();
mySQLCon.Open();
var temp = mySQLConn.State.ToString();
if (sqlCon.State==ConnectionState.Open && temp=="Open")
 {
   MessageBox.Show(@"Connection working.");
 }
else
 {
  MessageBox.Show(@"Please check connection string");
 }



还有一件事是@Leri提到你应该总是关闭/处置非托管资源。
希望它为你工作。

And one more thing as @Leri mentioned you should always close/dispose unmanaged resources. Hope it work for you.