【问题标题】:How to insert a column with a specific value in sql bulk copy如何在sql批量复制中插入具有特定值的列
【发布时间】:2011-08-03 08:00:36
【问题描述】:

我正在使用 c# 中的 sql 批量复制在 excel 中填充值。

DataTable dt = new DataTable();

string line = null;
int i = 0;

using (StreamReader sr = File.OpenText(@"c:\temp\table1.csv"))
{  
      while ((line = sr.ReadLine()) != null)
      {
            string[] data = line.Split(',');
            if (data.Length > 0)
            {
                  if (i == 0)
                  {
                  foreach (var item in data)
                  {
                        dt.Columns.Add(new DataColumn());
                  }
                  i++;
             }
             DataRow row = dt.NewRow();
             row.ItemArray = data;
             dt.Rows.Add(row);
             }
      }
}


using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString))
{
      cn.Open();
      using (SqlBulkCopy copy = new SqlBulkCopy(cn))
      {
            copy.ColumnMappings.Add(0, 0);
            copy.ColumnMappings.Add(1, 1);
            copy.ColumnMappings.Add(2, 2);
            copy.ColumnMappings.Add(3, 3);
            copy.ColumnMappings.Add(4, 4);
            copy.DestinationTableName = "Censis";
            copy.WriteToServer(dt);
      }
} 

在上面的代码中,我将 excel 中的记录插入到表中。但是,我在 Censis 表中还有一列“ProcessID”。对于每次运行,我需要生成一个 GUID 并用它填充此列。

当我像上面那样使用为该运行的所有行生成的 GUID 进行批量复制时,谁能帮助我如何填充 ProcessID 列?

【问题讨论】:

    标签: c# sql-server-2008 sqlbulkcopy


    【解决方案1】:

    当您插入数据库时​​,请使用函数newsequentialid

    【讨论】:

      【解决方案2】:

      感谢乔恩。但是,我为我的问题找到了答案。我可以使用这样的东西。我可以将具有默认值的新 DataColumn 添加到用于批量复制的数据表中。

      DataTable dt = new DataTable();
      
      string line = null;
      int i = 0;
      
      using (StreamReader sr = File.OpenText(@"c:\temp\table1.csv"))
      {  
            while ((line = sr.ReadLine()) != null)
            {
                  string[] data = line.Split(',');
                  if (data.Length > 0)
                  {
                        if (i == 0)
                        {
                        foreach (var item in data)
                        {
                              dt.Columns.Add(new DataColumn());
                        }
                        i++;
                   }
                   DataRow row = dt.NewRow();
                   row.ItemArray = data;
                   dt.Rows.Add(row);
                   }
            }
      
            DataColumn col = new DataColumn("BatchId", typeof(System.Guid));
            col.DefaultValue = Guid.NewGuid();
            dt.Columns.Add(col);
      }
      
      
      using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString))
      {
            cn.Open();
            using (SqlBulkCopy copy = new SqlBulkCopy(cn))
            {
                  copy.ColumnMappings.Add(0, 0);
                  copy.ColumnMappings.Add(1, 1);
                  copy.ColumnMappings.Add(2, 2);
                  copy.ColumnMappings.Add(3, 3);
                  copy.ColumnMappings.Add(4, 4);
                  copy.DestinationTableName = "Censis";
                  copy.WriteToServer(dt);
            }
      } 
      

      【讨论】:

      • 强烈不推荐使用 .net 生成 GUID。这会导致数据和碎片之间的差距。推荐使用newsequentialid()
      猜你喜欢
      • 1970-01-01
      • 2013-11-10
      • 2013-09-21
      • 2019-04-29
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多