2012-03-19 59 views
0

我喜歡訪問DB,我的一個函數(C#.net)需要使用事務執行超過4000次的SQL。如何防止長時間運行的查詢消耗資源?

看來,執行後DB文件保持獨佔打開。因爲有一個* .ldb文件,並且該文件在那裏呆了很長時間。

是由於處理資源不正確造成的嗎???

private int AmendUniqueData(Trans trn) 
{ 
    int reslt = 0; 



    foreach (DataRow dr in _dt.Rows) 
    { 
     OleDbParameter[] _params = { 
            new OleDbParameter("@templateId",dr["Id"].ToString()), 
            new OleDbParameter("@templateNumber",dr["templateNumber"].ToString()) 
           }; 

     string sqlUpdateUnique = "UPDATE " + dr["proformaNo"].ToString().Substring(0,2) + "_unique SET templateId = @templateId WHERE [email protected]"; 

     reslt = OleDBHelper.ExecSqlWithTran(sqlUpdateUnique, trn, _params); 
     if (reslt < 0) 
     { 
      throw new Exception(dr["id"].ToString()); 
     } 
    } 
    return reslt; 
} 

交易:

using (Trans trn = new Trans()) 
    { 
     try 
     { 
      int reslt=AmendUniqueData(trn); 
      trn.Commit(); 
      return reslt; 
     } 
     catch 
     { 
      trn.RollBack(); 
      throw; 
     } 
     finally 
     { 
      trn.Colse(); 
     } 
    } 
+1

確保您關閉數據庫連接。 – 2012-03-19 12:29:07

回答

0

忘記關閉數據庫連接。