C# 用C语言连接MySQL数据库#
我正在使用:C# 用C语言连接MySQL数据库#,c#,mysql,database,ado.net,mysql-connector,C#,Mysql,Database,Ado.net,Mysql Connector,我正在使用: if (connection.State != ConnectionState.Open) { OpenConnection(); } 及 我总是得到System.invalidoOperationException:连接已打开。这没有意义,因为我检查它是否已经打开了 提前感谢。除了打开和关闭之外,还有其他连接状态:断开、关闭、连接、执行、获取。只有在“关闭”的情况下才应尝试打开。如果还有其他问题,则需要以不
if (connection.State != ConnectionState.Open)
{
OpenConnection();
}
及
我总是得到System.invalidoOperationException:连接已打开
。这没有意义,因为我检查它是否已经打开了
提前感谢。除了打开和关闭之外,还有其他连接状态:断开、关闭、连接、执行、获取。只有在“关闭”的情况下才应尝试打开。如果还有其他问题,则需要以不同方式处理。@user1335122在If语句之前打印连接状态。这可能会给你一些关于州的信息。
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.WriteLine("Cannot connect to server. Contact administrator");
Console.Read();
break;
case 1045:
Console.WriteLine("Invalid username/password, please try again");
Console.Read();
break;
}
return false;
}
}