2016-07-12 27 views
0

我在200臺機器上有一個簡單的程序,記錄用戶正在打開的窗體。每次打開一個表單時,打開一個Sql連接,插入一行,我猜連接已關閉?我讀默認情況下打開連接池,所以我猜這不是真的關閉它嗎?我不允許調用Web服務或更好的方式,所以我的問題是爲什麼這個錯誤是存在的,並且知道如何解決它?或者在SQL結束的東西?也許我可以嘗試改變一個設置?爲什麼許多客戶端試圖寫入表時出現SQL Server錯誤?

using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 
      try 
      { 
       connection.Open(); 

       using (SqlCommand command = new SqlCommand(
       "INSERT INTO LoggerLoanForm VALUES(@Session, @Form, @DateStamp, @LoanNumber)", connection)) 
       { 
        command.Parameters.Add(new SqlParameter("Session", llf.SesssionId)); 
        command.Parameters.Add(new SqlParameter("Form", llf.Form)); 
        command.Parameters.Add(new SqlParameter("DateStamp", llf.DateStamp)); 
        command.Parameters.Add(new SqlParameter("LoanNumber", llf.LoanNumber)); 

        command.ExecuteNonQuery(); 
       } 


      } 
      catch (Exception ex) 
      { 
       AppInsightHelper.TrackException(ex); 
      } 
     } 

建立與SQL Server的連接時發生網絡相關或實例特定的錯誤。服務器未找到或無法訪問。驗證實例名稱是否正確,並將SQL Server配置爲允許遠程連接。 (提供程序:TCP提供程序,錯誤:0 - 請求的名稱有效,但未找到請求類型的數據。)請求的名稱有效,但沒有找到請求類型的數據

錯誤似乎在高峯時段發生的更多,所以我想沒有足夠的開放連接或SQL服務器的東西?

+2

您正在使用的應用程序代碼連接屬性 - 當它這樣做了'using'塊將關閉連接。我懷疑有一些東西需要在_server_一方更改爲[允許更多同時連接](http://stackoverflow.com/questions/1499718)或使用不同的併發模型。你也可能有更深的網絡問題,但只有200個用戶,似乎不太可能,除非你有真正便宜的交換機或路由器。 –

+1

如果你谷歌 - 與SQL Server間歇建立連接時發生網絡相關或實例特定的錯誤 - 你會看到其他Q的人有這個問題,很多人都提到網絡過載,防火牆等等 – Cato

+1

我想象它是網絡而不是SQL服務器,如果你用完了連接,你會得到一條消息,說通常沒有更多的連接 - 我假設它是間歇性的 - 如果你在錯誤後睡了100ms,然後自動重試有限的幾次 - 這將有助於 – Cato

回答

2

又何談重試

 const int max_try = 5; 

     int i = max_try; 

     while (i-- > 0) 
     { 
      using (SqlConnection connection = new SqlConnection(connectionString)) 
      { 
       try 
       { 
        connection.Open(); 

        using (SqlCommand command = new SqlCommand(
        "INSERT INTO LoggerLoanForm VALUES(@Session, @Form, @DateStamp, @LoanNumber)", connection)) 
        { 
         command.Parameters.Add(new SqlParameter("Session", llf.SesssionId)); 
         command.Parameters.Add(new SqlParameter("Form", llf.Form)); 
         command.Parameters.Add(new SqlParameter("DateStamp", llf.DateStamp)); 
         command.Parameters.Add(new SqlParameter("LoanNumber", llf.LoanNumber)); 

         command.ExecuteNonQuery(); 
         i = 0; 
        } 


       } 
       catch (Exception ex) 
       { 
        if (i == 0) 
          AppInsightHelper.TrackException(ex); 
         System.Threading.Thread.Sleep(50); 

       } 
      } 
     } 
+0

這是一個好主意..我試試這個..如果問題是擁塞,那麼5個重試應該足以通過..我可以檢查異常日誌並找出 – punkouter

相關問題