Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言连接MySQL数据库#_C#_Mysql_Database_Ado.net_Mysql Connector - Fatal编程技术网

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