批量导出为CSV

时间:2016-07-06 07:20:32

标签: c# sql-server csv ado.net

我正在处理csv向用户发送通知的大量约会等。我想要做的是设置一个isProcessed标志,说明当前行已被处理我不知道该怎么做在我目前的循环中。

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,我希望在经过csv导出后将其设置为true。这样我就可以进行批量导出。它存在于同一个表约会中

修改1

很抱歉没有说明我想要将这个标志写回到约会表中,因为它在csv导出中吐出来的csv导出工作我只是需要一种方法来识别它已被导出所以它不是第二次处理。

1 个答案:

答案 0 :(得分: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文件将被删除,因此您将永远不会有一半导出的批次。
  5. 你的代码会变成这样(我没有测试过):

    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;
        }
    }