2013-03-30 31 views
0

我有一個從平面文件加載數據到sql表的過程,然後需要立即將文件移動到存檔文件夾。 但運行代碼時,它導入數據,而是拋出IOException異常,並{ 「因爲它正由另一個進程使用該進程無法訪問文件。」}SqlBulkCopy和文件存檔

有似乎是在過程中的一些爭論。我在哪裏以及如何避免這種情況?

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     string sourceFolder = @"c:\ImportFiles\"; 
     string destinationFolder = @"c:\ImportFiles\Archive\"; 

     foreach (string fileName in Directory.GetFiles(sourceFolder, "*.*")) 
     { 
      string sourceFileName = Path.GetFileName(fileName); 
      string destinationFileName = Path.GetFileName(fileName) + ".arc"; 

      ProcessFile(fileName); 

      string source = String.Concat(sourceFolder,sourceFileName); 
      string destination = String.Concat(destinationFolder,destinationFileName); 
      File.Move(source, destination);   
     } 
    } 



    static void ProcessFile(string fileName) 
    { 
     Encoding enc = new UTF8Encoding(true, true); 
     DataTable dt = LoadRecordsFromFile(fileName, enc, ','); 

     SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=(local);Database=test;Trusted_Connection=True;", 
               SqlBulkCopyOptions.TableLock); 
     bulkCopy.DestinationTableName = "dbo.tblManualDataLoad"; 
     bulkCopy.WriteToServer(dt); 
     bulkCopy.Close(); 

    } 


    public static DataTable LoadRecordsFromFile(string fileName, Encoding encoding, char delimeter) 
    { 
     DataTable table = null; 

     if (fileName != null && 
      !fileName.Equals(string.Empty) && 
      File.Exists(fileName)) 
     { 
      try 
      { 
       string tableName = "DataImport"; 
       FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 

       List<string> rows = new List<string>(); 
       StreamReader reader = new StreamReader(fs, encoding); 
       string record = reader.ReadLine(); 

       while (record != null) 
       { 
        rows.Add(record); 
        record = reader.ReadLine(); 
       } 

       List<string[]> rowObjects = new List<string[]>(); 
       int maxColsCount = 0; 
       foreach (string s in rows) 
       { 
        string[] convertedRow = s.Split(new char[] { delimeter }); 
        if (convertedRow.Length > maxColsCount) 
         maxColsCount = convertedRow.Length; 
        rowObjects.Add(convertedRow); 
       } 

       table = new DataTable(tableName); 
       for (int i = 0; i < maxColsCount; i++) 
       { 
        table.Columns.Add(new DataColumn()); 
       } 

       foreach (string[] rowArray in rowObjects) 
       { 
        table.Rows.Add(rowArray); 
       } 
       //Remove Header Row From Import file 
       DataRow row = table.Rows[0]; 
       row.Delete(); 
       table.AcceptChanges(); 
      } 
      catch 
      { 
       //TODO SEND EMAIL ALERT ON ERROR 
       throw new Exception("Error in ReadFromFile: IO error."); 
      } 
     } 
     else 
     { 
      //TODO SEND EMAIL ALERT ON ERROR 
      throw new FileNotFoundException("Error in ReadFromFile: the file path could not be found."); 
     } 
     return table; 
    } 
} 

回答

1

您的程序很可能保持打開文件。您應將和StreamReader對象包含在using聲明中。這會在using塊完成時關閉這些對象。

LoadRecordsFromFile功能的一部分,它讀取文件應該是這個樣子:

... 
string tableName = "DataImport"; 
List<string> rows = new List<string>(); 
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
{ 
    using (StreamReader reader = new StreamReader(fs, encoding)) 
    { 
     string record = reader.ReadLine(); 
     while (record != null) 
     { 
      rows.Add(record); 
      record = reader.ReadLine(); 
     } 
    } 
} 
... 
+0

這做到了...謝謝! – user1934500