How is SqlException Number assigned

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 00:22:30

The theory goes that is the SQL error number, eg. the server side ERROR_NUMBER(). In other words, the first list.

However there are a number of exceptions reported by SqlClient that occur on the client side, not on the server side. A typical example would be an error like failure to connect to the server, since you did not connect there is no server side error to speak of. For example a bad server name (does not resolve in DNS), in such cases the InnerException will point toward a Win32Exception with NativeErrorCode value of ERROR_BAD_NETPATH. In this case 53, the OS system error code, will be reported as SqlException error number.

Other cases the error reported by the SqlClient is a socket error, like an abrupt disconnect. Again, there is no 'server side' error to speak of, and the SqlException will wrap an InnerException of type SocketException (a subclass of Win32Error) with the NativeErrorCode of one of the well known WSA error numbers, like WSAECONNRESET. In this case the SqlException.ErrorNumber will be 10054, but is the 10054 from the WSA range, not the 10054 from the SQL Server errors range. I know...

So what are you supposed to do? Make sure you check the InnerException, if is a Win32Exception then the ErrorNumber is coming from a system error code (OS error). Otherwise it should be a SQL Server error number.

Oh, and then there is -1... I think that is reported by SqlClient itself (eg. some internal state errors) but I'm not sure.

Daniel A. White

I would look at the documentation for the SqlException.Number property.

This is what it says

This is a wrapper for the Number property of the first SqlError in the Errors property. For more information on SQL Server engine errors, see SQL Server Books Online.

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