2013-05-28 35 views
0

Application_Start()我從數據庫加載一些靜態數據以供進一步使用。但問題是,當服務器處於空閒狀態,例如1天,然後到DB的第一個請求失敗並返回此錯誤:ASP.NET首次在我的應用程序中訪問數據庫錯誤

The client was unable to establish a connection because of an error during connection initialization process before login. 
Possible causes include the following: 
the client tried to connect to an unsupported version of SQL Server; 
the server was too busy to accept new connections; 
or there was a resource limitation (insufficient memory or maximum allowed connections) on the server. 

(provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.) 

然後在刷新後,一切工作正常。那麼有什麼想法?爲什麼每天都要撥打Application_Start()?我認爲只有在第一次請求應用程序時才調用它,然後才從IIS重新啓動應用程序。

編輯:
我使用SQL Server 2008R2表達
不應該有在我的代碼,我認爲問題的,但是她是:

public static SqlConnection sqlCon() 
    { 
     return new SqlConnection(WebConfigurationManager.ConnectionStrings["Test"].ConnectionString);    
    } 
public void fillFromDB()          
    { 
     SqlConnection con = defVal.sqlCon(); 
     SqlCommand cmd_getK = new SqlCommand("usp_getAllKomponenty", con); 
     cmd_getK.CommandType = CommandType.StoredProcedure; 
     try 
     { 
      con.Open(); 
      SqlDataReader reader = cmd_getK.ExecuteReader(); 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        this.adKomp(reader[0].ToString(), new elisKomponent(reader[0].ToString(), reader[1].ToString(), new date(Convert.ToInt32(reader[2])))); 
       } 
       reader.Close(); 
      } 
     } 
     catch (SqlException e) 
     { 
      throw new ApplicationException("Chyba při čtení komponenty databáze"+e.Message); 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 
+0

您正在使用哪個版本的sql server? –

+0

你用來收集這些數據的代碼在哪裏?感覺就像你有一個你正在存儲的'SqlConnection'的實例**(你不應該這樣做)**,並試圖在以後再利用它。 *總是創建新的並正確處理'SqlConnection'對象。* –

+0

查找「using」語句。你會想這樣...所以你得到IDispose.Dispose被調用。使用你的連接,然後把它們處理掉...........不要試圖去想它......讓連接池爲你處理巫術......尤其是在一個asp.net網站上。 – granadaCoder

回答

1

嘗試更/最具體類型的連接串。

IP地址,端口號。網絡協議。

Data Source=192.168.1.333,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase; 
User ID=myUsername;Password=myPassword; 

這可能有助於......因爲它會擺脫一些「解決」檢查。

+0

連接字符串看起來像這樣但這不應該是問題,它就像服務器第一次嘗試訪問它時卡住了,但事實並非如此。一旦我在當時通過我的遠程登錄服務器登錄到服務器,我的應用程序正在嘗試連接,並且我能夠,但該應用程序stil不是 – david

+0

當您放入服務器名稱時,必須解決。如果您沒有輸入端口號,則必須找出端口號。當您不指定網絡協議時,它將使用註冊表中的默認值。試圖改變連接字符串是一個便宜的嘗試。我並不是說你的連接字符串是錯誤的,我說你可以嘗試使它更具體化。 – granadaCoder

相關問題