English 中文(简体)
• 如何适当捕捉SqlException: 发生了运输一级错误。
原标题:How to catch properly an SqlException: A transport-level error has occurred

我在 .NET 3.5 应用程序的日志中发现了一个 SqlException,我正在寻找相应的编号(即属性 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.

有人知道如何正确捕获这些异常吗?(您还可以在此处发布您的回答https://stackoverflow.com/questions/380547/a-list-of-common-sqlexception-numbers)

最佳回答

那个特定的错误不是来自于SQL Server中的sys.messages,因为它是连接问题。所以我手头没有编号。

TBH是你应该做的最好事情,就是确保数字在记录上签字,但例外情况除外,因此,你总是在记录中同时有数字和电文。 愿不回答你目前的问题,但这将是最好的前进道路。 不仅SqlException.Number ,而且SqlException.ErrorCode - in cases such as this Iink . 脱orCode可以是你实际需要(但不能不核对)。

问题回答

您将无法找到这个的关联 SQL 错误编号-简单地说,出现错误是因为连接服务器时遇到了问题。

由于代码无法连接到SQL服务器,因此它将无法报告SQL错误。

从MSDN关于SQLException.Number的文章:

这个数字对应于master.dbo.sysmessages表中的一个条目。

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

各位,请在盲目声称SqlException没有.Number之前,仔细查看实际异常堆栈,因为某些MSDN页面具有模板文本。.ErrorCode通常只获取一个单一的通用值(由私有CTOR设置)。对于这个特定的错误,您可以在异常堆栈中看到。

将此翻译成中文:http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/cb61ec85-c0d4-4a26-90e0-8c98cd28332f

如果您遵循它,您会发现准备这个代码的代码会填入所有内容,但是要在反射器中跟踪实际错误号码有点复杂。

因此,任何人看到以下错误,请发布实际的错误编号(或编号)。这些错误往往非常罕见和短暂,有编号会很有用,因为当服务器看到连接故障时,追踪其编号已经太晚了。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签