问题
I want to catch a specific mysql error with the number 1046. If i use MessageBox.Show to display the error number it is always 0, no matter which Exception it is. Shouldn't the Exception have a number like the number 1046?
catch (MySqlException mysqlex)
{
MessageBox.Show(mysqlex.Number.ToString());
}
EDIT: Sorry, i am using the MySQL-Connector / Net.
回答1:
You don't see a number because you did not connect to a database. And probably MySQL is assigning numbers to error exceptions.
If you look at description for Number property here it says
Gets a number that identifies the type of error. This number corresponds to the error numbers given in Server Error Codes and Messages.
Because you did not made a connection to the server you cannot get server error code.
Check your connection string.
回答2:
Probably same problem as this.
please try in the following code.
MessageBox.Show(mysqlex.InnnerException.Number.ToString());
来源:https://stackoverflow.com/questions/22557433/catch-mysql-exception-using-number