catch mysql-exception using .Number

北战南征 提交于 2019-12-13 07:26:49

问题


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

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