调试未返回任何错误时的 SqlBulkCopy 问题



现在我对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);
}
哦,

天哪,我很愚蠢...我忘了这一行:table.Rows.Add(row);

我试图插入一个空的数据表。这就是为什么我喜欢 stackoverflow,即使我没有从这个 avesome 社区得到答案,它也可以帮助我解决我的问题:D

最新更新