C# MySQL/Connector Number always 0

拜拜、爱过 提交于 2019-12-11 18:46:09

问题


connectionString = "SERVER=localhost;DATABASE=NT;UID=" + dbuser + ";PASSWORD=" + dbpass + ";";

connection = new MySqlConnection(connectionString);

OpenConnection();


//open connection to database
    private bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (MySqlException ex)
        {
            //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 contact MySQL Server ");//MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;

                case 1045:
                    Console.WriteLine(" >>>> Invalid username/password, please try again");
                    break;

                case 1042:
                    Console.WriteLine(" >>>> Unable to resolve DNS");
                    break;
            }
            return false;
        }
    }

The code above is successfully connecting to the localhost database and logging in but the database "NT" doesn't exist (which I know) but the ex.Number property is set to 0 I also got the same issue with wrong username/password combo. The exception message/text gives me the string of the error but the number field is always set to 0. Any help would be greatly appreciated.

Many Thanks, Ben

EDIT/UPDATE

The only difference between the 2 screengrabs is that I changed the NT to test (NT doesn't exist (which the message reports correctly but not the number) and test does exist)

I'm wondering if this is maybe intended behaviour? Is it basically refusing the connection because that DB doesn't exist?


回答1:


I am using MySql Connector .Net 6.9.4 and also had this issue. I found out that there is an InnerException of type MySqlException that has the Error Number. You can try my Helper Method:

public static int GetExceptionNumber(MySqlException my)
{
    if (my != null)
    {
        int number = my.Number;
        //if the number is zero, try to get the number of the inner exception
        if (number == 0 && (my = my.InnerException as MySqlException) != null)
        {
            number = my.Number;
        }
        return number;
    }
    return -1;
}

Greetings

Frank




回答2:


Don't set PASSWORD but PWD like this:

connectionString = "SERVER=localhost;DATABASE=NT;UID=" + dbuser + ";PWD=" + dbpass + ";";



回答3:


The same problem generated me, too. I think the bug of Connector/Net. Please correct in the following code and try.

switch (ex.InnerException.Number)
{

It was tested in 6.9.8.




回答4:


Having had the same problem myself, it seems that the Number field is only filled in the inner exception. I'm not sure why this is the case but the MySqlException contains an inner exception also of type MySqlException, and the inner exception has the number.

So to get the number you should probably use something like this:

int number;
if (ex.InnerException != null && ex.InnerException is MySqlException)
{
    number = ((MySqlException) ex.InnerException).Number;
}
else
{
    number = ex.Number;
}

This includes a null check and type check for the inner exception, and falls back to the number in the outer exception if there is no inner exception or it's the wrong type.



来源:https://stackoverflow.com/questions/24899020/c-sharp-mysql-connector-number-always-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!