现在我对 sqlBulkCopy 的问题是它完成时没有任何错误,但是当我查看表时,我看不到任何数据。
try catch 块没有捕获任何错误,我错过了什么吗?因为我没有看到我的代码有任何问题
这是我使用的代码:
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Buying ID", typeof(string)));
table.Columns.Add(new DataColumn("Buying BPC ID", typeof(string)));
table.Columns.Add(new DataColumn("Buying BP Name", typeof(string)));
table.Columns.Add(new DataColumn("Buying BP Post Code", typeof(string)));
... snip ...
table.Columns.Add(new DataColumn("Buyer BP Type", typeof(string)));
table.Columns.Add(new DataColumn("Seller BP Type", typeof(string)));
table.Columns.Add(new DataColumn("FileSource", typeof(string)));
table.Columns.Add(new DataColumn("ImportDate", typeof(System.DateTime)));
while ( (line = sr.ReadLine()) != null)
{
string[] data = line.Split('\t');
DataRow row = table.NewRow();
for (int i=0; i<data.Length; i++)
{
row[i] = data[i];
}
row["FileSource"] = fi.Name;
row["ImportDate"] = DateTime.Now;
}
try
{
db.Connection.Open();
SqlBulkCopy bulkcopy = new SqlBulkCopy(db.Connection);
bulkcopy.BatchSize = 500;
bulkcopy.BulkCopyTimeout = 600;
bulkcopy.DestinationTableName = "dbo.LNST_test";
foreach (var col in table.Columns)
{
bulkcopy.ColumnMappings.Add(col.ToString(), col.ToString()); //col names in datatable are identical to col names in database
}
bulkcopy.WriteToServer(table);
db.Connection.Close();
}
catch (Exception e)
{
Console.WriteLine("ERROR : "+e.Message);
}