如何正确地赶一个SQLException:发生传输级错误正确地、错误、发生、SQLException

2023-09-04 12:24:48 作者:恰似惊鸿落人间

我正在一个 SQLEXCEPTION 的.NET 3.5的应用程序日志中,我在寻找相应的编号(值的属性的SQLException的。数)。

I am getting an SqlException in the logs of .NET 3.5 app, I am looking for the corresponding number (value of the property SqlException.Number).

System.Data.SqlClient.SqlException: A transport-level error has occurred 
when receiving results from the server. (provider: TCP Provider, error: 0 
- The specified network name is no longer available.) at
System.Data.SqlClient.SqlConnection.OnError

我也越来越错误,如:

I am also getting error such as:

System.Data.SqlClient.SqlException: A network-related or instance-specific error 
occurred while establishing a connection to SQL Server. The server was not found 
or was not accessible. Verify that the instance name is correct and that SQL Server
is configured to allow remote connections.

有谁知道如何正确地捕捉那些异常? (您也可以发表你的答案这里)

推荐答案

这特定的错误并非来自于SQL Server的sys.messages - 因为它与连接有问题。所以,我没有出手的次数。

That particular error does not come from sys.messages in SQL Server - as it's a problem with the connection. So I don't have the number to hand.

TBH,你应该做的最好的事情是要确保数量是写到日志例外 - 这样,你总能并排日志中的编号和消息面。可能不会回答你的当前问题,但是这将是最好的去向前恕我直言。不仅 SqlException.Number ,但 SqlException.Error code 太 - 在这样的情况下,我觉得 .Error code 可能是你真正需要的人(不知道,不检查)。

TBH, the best thing you should do is to ensure the Number is written out to the log with the exception - that way, you'll always have the number and message side by side in the logs. May not answer your current question, but it will be the best going forwards IMHO. Not only SqlException.Number but SqlException.ErrorCode too - in instances like this I think .ErrorCode may be the one you actually need (not sure without checking).