6

SqlException在 .NET 3.5 应用程序的日志中得到一个,我正在寻找相应的数字(属性的值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

我也收到错误,例如:

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.

有谁知道如何正确捕获这些异常?(你也可以在这里发布你的答案)

4

3 回答 3

5

该特定错误并非来自 SQL Server 中的 sys.messages - 因为它是连接问题。所以我手头没有号码。

TBH,您应该做的最好的事情是确保将数字写入日志,但有例外 - 这样,您将始终在日志中并排显示数字和消息。可能无法回答您当前的问题,但恕我直言,这将是最好的前进方向。不仅SqlException.Number如此SqlException.ErrorCode- 在这样的情况下,我认为.ErrorCode可能是你真正需要的(不检查就不确定)。

于 2010-02-15T13:14:00.947 回答
4

您将找不到与此相关的 SQL 错误编号 - 简单地说,发生错误是因为连接到服务器时出现问题。

由于代码无法连接到 SQL Server,因此不会报告 SQL 错误。

来自关于SQLException.Number的 MSDN 文章:

This number corresponds to an entry in the master.dbo.sysmessages table.

对于没有关联 SQL 错误号的异常,您可以使用将在异常对象的ErrorCode属性中的 windows HRESULT 错误代码。

于 2010-02-15T13:05:44.713 回答
2

伙计们,在盲目声称 SqlException 没有 .Number 之前,请检查实际的异常堆栈,只是因为某些 MSDN 页面有样板文本。.ErrorCode 通常获取一个通用值(由私有 CTOR 设置)。对于这个特殊错误,您可以在此处查看异常堆栈:

http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/cb61ec85-c0d4-4a26-90e0-8c98cd28332f

如果您遵循它,您会看到正在准备的代码填充了所有内容,但是跟踪反射器中的实际错误号有点复杂。

因此,接下来看到事情的人请张贴实际错误号(或数字)。这些错误往往是非常罕见和短暂的,最好有数字,因为当服务器看到连接中断时,去追逐他们的数字已经太晚了。

于 2010-07-23T09:52:06.497 回答