早上好!
提前道歉,我确实已经意识到已经提出了类似的问题但是通过这些问题并没有解决我的问题,因此这篇文章。
在发布异常详细信息之前,这里是概述:
我使用SQL Server(2005和2008r2)进行数据存储的.NET 4.0智能桌面应用程序。在数据库中,我有一个表,用于记录实体何时被更改。这允许应用程序的其他实例检查更改并重新加载某些数据。该表有三个字段:
Id [int], EntityName [nvarchar(4000)], ChangeDateTime [datetime]
Id是主键,ChangeDateTime字段有一个索引。
为了完成这项工作,应用程序会以一定的时间间隔检查此表,或者在执行操作时通过检索某个实体的最新更改日期时间并将其与内部存储的值进行比较来查看是否发生了更新。
查询如下:
Query: SELECT TOP(@p2) MAX([BondFM].[dbo].[EntityTypeChangedLog].[ChangeDateTime]) AS [ChangeDateTime] FROM [BondFM].[dbo].[EntityTypeChangedLog] WHERE ( [BondFM].[dbo].[EntityTypeChangedLog].[ChangedEntityTypeName] = @p3)
Parameter: @p2 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
Parameter: @p3 : String. Length: 4000. Precision: 0. Scale: 0. Direction: Input. Value: "SampleEntityName".
因此,该表仅接收来自正在运行的应用程序的两个交互:
问题是在执行select语句时我得到以下类型的许多异常:
Message : An exception was caught during the execution of a retrieval query: 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.).
有时它也会出现:
此外,我得到以下内部异常,这对我没有帮助缩小问题范围:
Inner Exception
---------------
Type : System.Data.SqlClient.SqlException, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : 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.)
Source : .Net SqlClient Data Provider
Help link :
Errors : System.Data.SqlClient.SqlErrorCollection
Class : 20
LineNumber : 0
Number : 64
Procedure :
Server : SQLServer
State : 0
ErrorCode : -2146232060
Data : System.Collections.ListDictionaryInternal
TargetSite : Void OnError(System.Data.SqlClient.SqlException, Boolean)
Stack Trace : at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.ExecuteScalar()
我已经阅读并测试了连接方面的各种问题,这些问题没有帮助,我想知道表格内是否发生了某些事情(例如锁?)导致这种情况?
感谢阅读,任何帮助或指示将不胜感激!
麦克
答案 0 :(得分:2)
您可以尝试增加连接字符串中的连接超时。
private static void OpenSqlConnection()
{
string connectionString = GetConnectionString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionTimeout: {0}",
connection.ConnectionTimeout);
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI;Connection Timeout=30";
}
答案 1 :(得分:0)
原因
错误是由长时间运行的查询的超时异常引起的。在以前版本的Visual Studio .NET中,异常被正确表示为具有超时描述的异常。
解决方案
将命令对象的commandtimeout属性设置为适当的值。使用零值等待而不抛出异常。