我有一個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對象似乎效率低下。
您打電話給fnChkXfer最多3次從您的列表中彈出每個項目,基本上不同的是@FileType的值?看起來像你可以只是有一個內循環,如果沒有理由繼續檢查文件,就跳出循環。 – billinkc
這就是說,Whatcha在做什麼?根據名稱,看起來您有一個所有子文件夾的列表,並且基於此tsql函數調用,您正在執行一些操作系統級活動(假定複製文件)。它感覺不太... SSIS-ey。我懷疑我們可以使用更多開箱即用的組件,如果您可以幫助我們瞭解您嘗試解決的問題,那麼這可能會導致更易於維護的軟件包,但缺陷更少。 – billinkc
是的,這是正確的。我將這個函數調用了3次,不同的是文件類型。我必須確定文件夾中的文件是否已針對每種類型進行傳輸,然後使用WinSCP傳輸文件(從源地的3個位置到目的地的2個差異位置)。我無法使用SyncFiles中構建的WinSCP,因爲2系統上的文件結構不同,我不重寫工作代碼來簡化此傳輸(目前手動完成)。通過此次轉移,我正在結束現有的SSIS包。我正在解決的問題是自動化SFTP傳輸。 – Maa421s