【发布时间】: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