问题描述
SqlException 具有 Number 属性.
然后是这个:http://msdn.microsoft.com/en-us/library/cc645603.aspx
还有这个:http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
这似乎是其中之一
问题:
如何决定哪个?
询问原因:
我需要捕获某些 SqlExceptions 并根据 Number 属性决定如何处理它们,但我不知道应该查看哪个列表就像系统使用来自两者的消息一样,我不知道使用什么标准进行选择.
I need to catch certain SqlExceptions and decide how to deal with them based on the Number property but I don't know which list I should look at when it seems like the system is using messages from both, and I don't know what criteria is used for choosing.
例如:
- 编号 53 - 来自服务器错误消息列表(两者都存在)
- 编号 10054 - 来自系统错误消息列表(两者都存在)
- 数字 -1 - 来自服务器错误消息列表(仅存在于服务器列表中)
- 编号 121 - 来自系统错误消息列表(两者都存在)......
推荐答案
理论上是SQL错误号,eg.服务器端 ERROR_NUMBER()
.也就是说,第一个列表.
The theory goes that it's the SQL error number, eg. the server side ERROR_NUMBER()
. In other words, the first list.
但是,SqlClient 报告了许多发生在客户端而不是服务器端的异常.一个典型的例子是连接服务器失败之类的错误,因为您没有连接,所以没有服务器端错误可言.例如,错误的服务器名称(无法在 DNS 中解析),在这种情况下,InnerException
将指向 Win32Exception
与 NativeErrorCode
值库/windows/desktop/ms681382(v=vs.85).aspx" rel="nofollow noreferrer">ERROR_BAD_NETPATH
.本例中OS系统错误码53,会报SqlException错误号.
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.
其他情况SqlClient报错是socket错误,比如突然断开.同样,没有服务器端"错误可言,SqlException 将包装 SocketException
(Win32Error 的子类)与 NativeErrorCode
是众所周知的 WSA 错误编号之一,例如 WSAECONNRESET
.在这种情况下,SqlException.ErrorNumber
将为 10054,但它是 WSA 范围中的 10054,而不是 SQL Server 错误范围中的 10054.我知道...
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 it's the 10054 from the WSA range, not the 10054 from the SQL Server errors range. I know...
那你应该怎么做?确保检查 InnerException
,如果它是 Win32Exception
,则 ErrorNumber
来自系统错误代码(操作系统错误).否则它应该是一个 SQL Server 错误号.
So what are you supposed to do? Make sure you check the InnerException
, if it's a Win32Exception
then the ErrorNumber
is coming from a system error code (OS error). Otherwise it should be a SQL Server error number.
哦,然后是 -1
... 我 认为 是由 SqlClient 本身报告的(例如,一些内部状态错误),但我不确定.
Oh, and then there is -1
... I think that is reported by SqlClient itself (eg. some internal state errors) but I'm not sure.
这篇关于SqlException Number 是如何分配的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!