Ian Jannasch

Ian Jannasch

  • NA
  • 9
  • 1.3k

SQL Server error: Timeout expired

Apr 22 2020 1:33 PM
Hi all, I am connecting to a SQL server 2005 using C#. All running on Windows server 2003. I have to read 270 000 PDF files and save them in a folder. A random time into the reading and saving the PDF files I get this error. I have run out of ideas a long time ago, so I am not sure what else I can do?
 
Below is the error and basic code saving each PDF. 
 
  1. Message: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.  
  2. StackTrace:    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)  
  3.    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)  
  4.    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)  
  5.    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)  
  6.    at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()  
  7.    at System.Data.SqlClient.SqlDataReader.get_MetaData()  
  8.    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)  
  9.    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)  
  10.    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)  
  11.    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)  
  12.    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)  
  13.    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)  
  14.    at WindowsFormsApplication1.Form1.SaveUserPDF(String FolderName, String CustID) in C:\Users\Ian\Documents\Visual Studio 2008\Projects\FilesExporter\FilesExporter\Form1.cs:line 110  
  15. Source: .Net SqlClient Data Provider  
  16. TargetSite: Void OnError(System.Data.SqlClient.SqlException, Boolean) 
 
  1. String queryString = "SELECT  dbo.DBDocument.DocumentName,dbo.DBDocBlob.Object FROM dbo.DBDocBlob, dbo.DBDocument WHERE dbo.DBDocBlob.DocumentID = dbo.DBDocument.DocumentID AND dbo.DBDocument.CustomerCode = '" + CustID + "' AND dbo.DBDocBlob.Object IS NOT NULL";  
  2.   
  3.                 using (SqlConnection connection = new SqlConnection(ConnectionString))  
  4.                 {  
  5.                     SqlCommand command = new SqlCommand(queryString, connection);  
  6.                     connection.Open();  
  7.                     using (var myReader = command.ExecuteReader(CommandBehavior.SequentialAccess))  
  8.                     {  
  9.                     while (myReader.Read())  
  10.                     {  
  11.                         filename= myReader.GetString(0);  
  12.                         countUser++;  
  13.                         count++;  
  14.   
  15.                         var filePath1 = Path.Combine(Directory.GetCurrentDirectory(), "PDFs");  
  16.                         var filePath2 = Path.Combine(filePath1, ToSafeFileName(FolderName));  
  17.                         var filePath3 = Path.Combine(filePath2, ToSafeFileName(filename) + "(" + countUser + ").pdf");  
  18.   
  19.                         if (!(Directory.Exists(filePath2)))  
  20.                         {  
  21.                             DirectoryInfo di = Directory.CreateDirectory(filePath2);  
  22.                         }  
  23.   
  24.                         using (var file = File.OpenWrite(filePath3))  
  25.                         {  
  26.                             const int BUFFER_SIZE = 4096;  
  27.                             var buffer = new byte[BUFFER_SIZE];  
  28.                             var index = 0;  
  29.                             int byteCount;  
  30.   
  31.                             while ((byteCount = (int)myReader.GetBytes(1, index, buffer, 0, BUFFER_SIZE)) > 0)  
  32.                             {  
  33.                                 file.Write(buffer, 0, byteCount);  
  34.                                 index += byteCount;  
  35.                             }  
  36.   
  37.                         }  
  38.   
  39.                     }  
  40.                     // Close the reader and the connection.  
  41.                     myReader.Close();  
  42.                     connection.Close();  
  43.                 }  
  44.   
  45.   
  46.   
  47.                 } 

Answers (5)