嗨我在C#中连接到MySQL,我需要检查是否打开了SQL连接.如果打开然后做某事或不做某事.我正在尝试下面的代码,但我收到错误.
var sqlCon= new SqlConnection(Properties.Settings.Default.sString);
var mySQLCon= new MySqlConnection(Properties.Settings.Default.dString);
sqlCon.Open();
mySQLCon.Open();
if (sqlCon.State==ConnectionState.Open && mySQLCon.State==ConnectionState.Open)
{
MessageBox.Show(@"Connection working.");
}
else
{
MessageBox.Show(@"Please check connection string");
}
Run Code Online (Sandbox Code Playgroud)
我收到错误mySQLCon.State==ConnectionState.Open
错误是InvalidOperationException
我们如何检查MySQL连接状态.
我认为错误应该在连接字符串中.首先检查您的连接字符串.
如果连接字符串是正确的,还有一些问题尝试如下所示.
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");
}
Run Code Online (Sandbox Code Playgroud)
而@Leri提到的另一件事应该是关闭/处置非托管资源.希望它对你有用.
| 归档时间: |
|
| 查看次数: |
26610 次 |
| 最近记录: |