2016-07-06 135 views
3

我正在處理大量的約會,以便csv向用戶發送通知等。我想要做的就是設置一個isProcessed標誌來表示當前行已經被處理了我不知道該怎麼辦在我目前的循環中。批量導出爲CSV

public void DumpTableToFile(SqlConnection connection, string tableName, string destinationFile) 
    { 
     using (var command = new SqlCommand("select LineType,CustomerFirstName AS 'Forename' ,CustomerLastName,Age,dob as 'Date of Birth',maritalStatus AS 'Marital Status',homePhone AS 'Home', mobileNumber AS Mobile,emailAddress AS Email,Address1 + Address2 + PostCode AS 'Address' ,employmentStatus AS Employment,occupation AS Occupation,propertyValue AS 'Property Value',mortgageBalance AS 'Mortgage Balance',balanceOnSecuredDebt AS 'Balance on secured Debt',mortgageType as 'Mortgage Type' from " + tableName, connection)) 
     using (var reader = command.ExecuteReader()) 
     using (var outFile = File.CreateText(destinationFile)) 
     { 
      string[] columnNames = GetColumnNames(reader).ToArray(); 
      int numFields = columnNames.Length; 
      outFile.WriteLine(string.Join(",", columnNames)); 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        string[] columnValues = 
         Enumerable.Range(0, numFields) 
            .Select(i => reader.GetValue(i).ToString()) 
            .Select(field => string.Concat("\"", field.Replace("\"", "\"\""), "\"")) 
            .ToArray(); 

        outFile.WriteLine(string.Join(",", columnValues)); 
       } 
      } 
     } 

的標誌被稱爲isProcessed,我想將它設置爲true一旦其通過CSV導出了。這是我可以做批量出口。它存在於同一個表約會

編輯在1

對不起,不說明,我想這個標誌被寫回預約表,它是在CSV吐出導出CSV了短路電流記錄出口工程我只需要一種方法來確定它已被導出,所以它不再被處理。

+0

你要什麼這個標誌呢?它應該是你寫出的文件的一部分嗎?內存中狀態? –

+0

@EricJ。在那裏做了一個編輯來解釋我自己,請參閱編輯1 – rogue39nin

+0

非常感謝您的投票uppreferred, – rogue39nin

回答

2

執行如下步驟:

  1. 添加到繞圈查詢:WHERE NOT isProcessed,讓旁邊的你做一個出口,只有不被處理的記錄將被處理。
  2. 完成導出後,將此命令發送到數據庫:"UPDATE " + tableName + " SET isProcessed=true WHERE <exact same criteria as the select statement>"。這樣所有的記錄現在都被標記爲已處理。
  3. 包裹一個TransactionScope arround export-mechanisme incl。更新。這樣,當某些事情失敗時,整個操作將被回滾。
  4. 圍繞TransactionScope環繞一個try-catch,所以當導出失敗時,CSV文件被刪除,所以你永遠不會有一半的導出批。

您的代碼將成爲這樣的事情(我沒有測試):

public void DumpTableToFile(SqlConnection connection, string tableName, string destinationFile) 
{ 
    try 
    { 
     using(var transaction = new TransactionScope()) 
     { 
      // Select all non-processed records. 
      using (var command = new SqlCommand("select LineType,CustomerFirstName AS 'Forename' ,CustomerLastName,Age,dob as 'Date of Birth',maritalStatus AS 'Marital Status',homePhone AS 'Home', mobileNumber AS Mobile,emailAddress AS Email,Address1 + Address2 + PostCode AS 'Address' ,employmentStatus AS Employment,occupation AS Occupation,propertyValue AS 'Property Value',mortgageBalance AS 'Mortgage Balance',balanceOnSecuredDebt AS 'Balance on secured Debt',mortgageType as 'Mortgage Type' from " + tableName 
       + " WHERE NOT isProcessed", connection)) 
      using(var reader = command.ExecuteReader()) 
      using(var outFile = File.CreateText(destinationFile)) 
      { 
       string[] columnNames = GetColumnNames(reader).ToArray(); 
       int numFields = columnNames.Length; 
       outFile.WriteLine(string.Join(",", columnNames)); 
       if (reader.HasRows) 
       { 
        while(reader.Read()) 
        { 
         string[] columnValues = 
          Enumerable.Range(0, numFields) 
           .Select(i => reader.GetValue(i).ToString()) 
           .Select(field => string.Concat("\"", field.Replace("\"", "\"\""), "\"")) 
           .ToArray(); 

         outFile.WriteLine(string.Join(",", columnValues)); 
        } 
       } 
      } 

      // Update the same records that were just exported. 
      using (var command = new SqlCommand("UPDATE " + tableName + " SET isProcessed=true WHERE NOT isProcessed", connection)) 
       command.ExecuteNonQuery(); 

      transaction.Complete(); 
     } 
    } 
    catch 
    { 
     // If something went wrong, delete the export file. 
     File.Delete(destinationFile); 
     throw; 
    } 
} 
+0

您好TransactionScope從哪裏來?它根本不能解決我的問題 – rogue39nin

+0

您已經引用了System.Transactions.dll。 –

+0

這是一個很好的答案,但是如果用戶能夠影響'tableName'的內容(就像OP的代碼那樣),就容易受到SQL注入攻擊。 @david請確保這是不可能的。當用用戶輸入構建查詢時請確保使用參數化查詢。 –