C# 调试SqlBulkCopy时出现问题';不要返回任何错误
现在我对sqlBulkCopy的问题是它完成时没有任何错误,但是当我查看表时,我看不到任何数据 try-catch块没有捕获错误,我是否遗漏了什么?因为我看不到我的代码有任何问题 这是我使用的代码:C# 调试SqlBulkCopy时出现问题';不要返回任何错误,c#,sqlbulkcopy,C#,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", t
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);
}
DataTable=newdatatable();
table.Columns.Add(新数据列(“购买ID”,typeof(字符串));
table.Columns.Add(新数据列(“购买BPC ID”,typeof(string));
table.Columns.Add(新数据列(“购买BP名称”,typeof(字符串));
table.Columns.Add(新数据列(“购买BP邮政编码”,typeof(string)));
... 剪
table.Columns.Add(新数据列(“买方BP类型”,类型(字符串));
表.Columns.Add(新数据列(“卖方BP类型”,类型(字符串));
table.Columns.Add(新的DataColumn(“FileSource”,typeof(string));
table.Columns.Add(新数据列(“ImportDate”,typeof(System.DateTime)));
而((line=sr.ReadLine())!=null)
{
string[]data=line.Split('\t');
DataRow行=table.NewRow();
对于(inti=0;i哦,天哪,我真蠢……我忘了这一行:table.Rows.Add(row);
我试图插入一个空的datatable。这就是为什么我喜欢stackoverflow,即使我没有从这个社区得到答案,它也能帮助我解决我的问题:D可能是事务问题?什么是db?要么是事务,要么表中没有行,要么你在SSMS中查看错误的表。数据库是MSSQL 2008,我正在连接到我t作为管理员(它只是一个测试数据库)而不是其他人。因此我认为这不是事务的问题:(我刚刚做了一些批量复制的工作,没有任何问题。+1看看结果是什么……如果你找到解决方案,请更新。当然,如果我找到了解决方案,我会发布更新。很奇怪,我没有收到错误,但数据库中没有数据。如果NewRow
足够,我实际上是怀疑的,但我不熟悉数据表:)很高兴你找到了它。是的,这是我第一次使用数据表,从其他地方我已经习惯了,当你调用NewRow()函数时,它会创建一个新行,并将其添加到datatable中,然后返回一个指向该行的指针,这样我就可以更新该行:)@ohigho_state当然,这只是我一个愚蠢的错误:)但当有人犯同样的错误时,我会把这个问题留在这里,这样他就能找出答案:D