2015-08-17 30 views
1

我有一個foreach循環調用每個文件夾如何「重設」 C#SqlCommand對象,所以我可以在一個循環再利用它

foreach (string strCurrentFolder in strLocalSubFolderList) 
{   
    SqlCommand sqlComm1 = new SqlCommand("dbo.fnChkXfer", _sqlConn); 
    sqlComm1.CommandType = CommandType.StoredProcedure; 
    sqlComm1.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
    sqlComm1.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder); 
    sqlComm1.Parameters.AddWithValue("@FileType", "Type 1"); 
    ...if files not transferred, then transfer them 

    SqlCommand sqlComm2 = new SqlCommand("dbo.fnChkXfer", _sqlConn); 
    sqlComm2.CommandType = CommandType.StoredProcedure; 
    sqlComm2.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
    sqlComm2.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder); 
    sqlComm2.Parameters.AddWithValue("@FileType", "Type 2"); 
    ...if files not transferred, then transfer them 

    SqlCommand sqlComm3 = new SqlCommand("dbo.fnChkXfer", _sqlConn); 
    sqlComm3.CommandType = CommandType.StoredProcedure; 
    sqlComm3.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
    sqlComm3.Parameters.AddWithValue("@UNCFolderPath", strCurrentFolder); 
    sqlComm3.Parameters.AddWithValue("@FileType", "Type 3"); 
    ...if files not transferred, then transfer the 
}     

它工作正常SQL函數的代碼,但是有是很多重複的代碼,這可能是不必要的。

是否有可能在循環之外創建一次SqlCommand對象,然後「重置」循環中的參數值?我如何重用該對象? (請非常具體)。

或者我應該繼續在循環中包含這個代碼塊,每次迭代使用不同的數據執行3次fn?當SqlCommand對象每次幾乎完全相同時,繼續重新創建SqlCommand對象似乎效率低下。

+0

您打電話給fnChkXfer最多3次從您的列表中彈出每個項目,基本上不同的是@FileType的值?看起來像你可以只是有一個內循環,如果沒有理由繼續檢查文件,就跳出循環。 – billinkc

+0

這就是說,Whatcha在做什麼?根據名稱,看起來您有一個所有子文件夾的列表,並且基於此tsql函數調用,您正在執行一些操作系統級活動(假定複製文件)。它感覺不太... SSIS-ey。我懷疑我們可以使用更多開箱即用的組件,如果您可以幫助我們瞭解您嘗試解決的問題,那麼這可能會導致更易於維護的軟件包,但缺陷更少。 – billinkc

+0

是的,這是正確的。我將這個函數調用了3次,不同的是文件類型。我必須確定文件夾中的文件是否已針對每種類型進行傳輸,然後使用WinSCP傳輸文件(從源地的3個位置到目的地的2個差異位置)。我無法使用SyncFiles中構建的WinSCP,因爲2系統上的文件結構不同,我不重寫工作代碼來簡化此傳輸(目前手動完成)。通過此次轉移,我正在結束現有的SSIS包。我正在解決的問題是自動化SFTP傳輸。 – Maa421s

回答

2

我將您的列表更改爲字典以添加文件類型。如果您要重用相同的sqlcommand對象,則需要在每次循環迭代中執行它。所以我加了那部分。你可能想添加一個試試,也可以試試。

Dictionary<string,string> strLocalSubFolderDict = new Dictionary<string, string>(); 

strLocalSubFolderDict.Add("Type 1", "Directory 1"); 
strLocalSubFolderDict.Add("Type 2", "Directory 2"); 
strLocalSubFolderDict.Add("Type 3", "Directory 3"); 

using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn)) 
{ 
    sqlComm.CommandType = CommandType.StoredProcedure; 
    sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
    sqlComm.Parameters.Add("@UNCFolderPath"); 
    sqlComm.Parameters.Add("@FileType"); 

    foreach (var val in strLocalSubFolderDict) 
    { 
     sqlComm.Parameters["@UNCFolderPath"].Value = val.Value; 
     sqlComm.Parameters["@FileType"].Value = val.Key; 
     sqlComm.ExecuteNonQuery(); 
     // ...if files not transferred, then transfer them 
     } 
} 

在這種代碼的循環和在的foreach的唯一改變外的對象被創建完整完成到的值上的對象的參數。

在一個側面,我不是很確定你與FileXferred返回參數做什麼。你在什麼地方使用它?

更新

這裏有,每一個目錄應用目錄中的所有文件類型的代碼。

 List<string> strLocalSubFolderList = new List<string>(); 
     List<string> typesList = new List<string>(); 

     typesList.Add("Type 1"); 
     typesList.Add("Type 2"); 
     typesList.Add("Type 3"); 

     using (SqlCommand sqlComm = new SqlCommand("dbo.fnChkXfer", _sqlConn)) 
     { 
      sqlComm.CommandType = CommandType.StoredProcedure; 
      sqlComm.Parameters.Add("@FileXferred", SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
      sqlComm.Parameters.Add("@UNCFolderPath"); 
      sqlComm.Parameters.Add("@FileType"); 

      foreach (var directval in strLocalSubFolderList) 
      { 
       foreach (var typeval in typesList) 
       { 
        sqlComm.Parameters["@UNCFolderPath"].Value = directval; 
        sqlComm.Parameters["@FileType"].Value = typeval; 
        sqlComm.ExecuteNonQuery(); 
        // ...if files not transferred, then transfer them 
       } 
      } 
     } 
+0

@Binkinkc,你是對的。它需要更新以反映這一點。但可重用的單個sqlcommand對象的想法在這裏。 – dpimente

+0

感謝您的建議。我喜歡第二個版本,並將採用這種方式。 – Maa421s

1

你肯定可以(甚至應該)在循環外部創建你的SqlCommand,並且在循環過程中不斷變化參數。爲了做到這一點,您需要在添加參數時存儲參數,然後在循環中設置它們的值。您應該在完成這些命令時關閉這些命令,最好以某種自動方式進行。聲明是最常見的選擇。

using (var cmd = new SqlCommand("dbo.fnChkXfer", _sqlConn)) { 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add("@FileXferred",SqlDbType.Bit).Direction = ParameterDirection.ReturnValue; 
    var p2 = cmd.Parameters.AddWithValue("@UNCFolderPath", DbType.Varchar, 32); // << Set the correct size 
    var typeParam = cmd.Parameters.AddWithValue("@FileType", , DbType.Varchar, 32); 
    foreach (string strCurrentFolder in strLocalSubFolderList) { 
     p2.Value = strCurrentFolder; 
     foreach (var typeVal in new[] {"Type 1", "Type 2", ...}) { 
      typeParam.Value = typeVal; 

      ... // Set values of the remaining parameters 
      ... // Use your command as needed 
     } 
    } 
} 
// After this point all three commands will be closed automatically 
+0

您正在創建三個sqlcommands,而不是一個可重用的。 – dpimente

+0

@dpimente固定。 – dasblinkenlight

相關問題