Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - SqlDataReader.GetValue Hangs

I run a process built in C# .Net 4.6.1 that accesses a SQL Server database. The process runs on Windows Server 2012 R2 Standard. The SQL Server database is on another server with SQL Server version 11.0.6567.0

The process executes the following code:

01  var readStatement = “select * from TableA order by ColumnX,ColumnY, ColumnZ offset 0 rows”;   // TableA has 13 million rows
02  var readCommand = new SqlCommand(readStatement, myConnection) { CommandTimeout = 72000 };
03  var reader = readCommand.ExecuteReader();
04  while (reader.Read())
05  {
06     foreach (var columnName in columnNames)
07     {
08        Console.WriteLine(“Checkpoint 1”);
09        var value = reader[columnName];
10        Console.WriteLine(“Checkpoint 2”);
11     }
12  }

This process normally completes with no problem. Occasionally, it goes into line 09 (a call to SqlDataReader.GetValue) and never returns. I was able to witness this problem while running a trace on the database. The process went to that call at about row 8 million (out of the 13 million), but never returned. At the time it hung, the database trace reported “Batch Completed” and then “Audit Logout”. I am unable to replicate the problem in the debugger to see how deep into SqlDataReader.GetValue the process is hanging. It does not run into memory issues or other resource constrain

Here is the call stack to where the process hangs:

System.Data.dll!SNINativeMethodWrapper.SNIReadSyncOverAsync Normal [Managed to Native Transition]
System.Data.dll!SNINativeMethodWrapper.SNIReadSyncOverAsync(System.Runtime.InteropServices.SafeHandle pConn, ref System.IntPtr packet, int timeout)
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadByteArray(byte[] buff, int offset, int len, out int totalRead)
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadDouble(out double value)
System.Data.dll!System.Data.SqlClient.TdsParser.TryReadSqlValueInternal(System.Data.SqlClient.SqlBuffer value, byte tdsType, int length, System.Data.SqlClient.TdsParserStateObject stateObj)
System.Data.dll!System.Data.SqlClient.TdsParser.TryReadSqlValue(System.Data.SqlClient.SqlBuffer value, System.Data.SqlClient.SqlMetaDataPriv md, int length, System.Data.SqlClient.TdsParserStateObject stateObj, System.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionOverride, string columnName)
System.Data.dll!System.Data.SqlClient.SqlDataReader.TryReadColumnInternal(int i, bool readHeaderOnly)
System.Data.dll!System.Data.SqlClient.SqlDataReader.TryReadColumn(int i, bool setTimeout, bool allowPartiallyReadColumn)
System.Data.dll!System.Data.SqlClient.SqlDataReader.GetValueInternal(int i)
System.Data.dll!System.Data.SqlClient.SqlDataReader.GetValue(int i)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...