将本地C#数据表与SqlBulkCopy一起使用
因为有很多这样的例子。。我在网上进行了研究,尽管在以下场景中没有显示使用将本地C#数据表与SqlBulkCopy一起使用,c#,winforms,sql-server-2008,datatable,sqlbulkcopy,C#,Winforms,Sql Server 2008,Datatable,Sqlbulkcopy,因为有很多这样的例子。。我在网上进行了研究,尽管在以下场景中没有显示使用SqlBulkCopy: 我使用了一个查询,以便从SqlServer(2008)将现有数据提取到一个数据表中, 这样我就可以在本地对数据进行排序,避免在处理过程中碰到数据库 现在,在那个阶段,我已经可以选择克隆源数据表模式 使用localDataTable=DataTableFromOnlineSqlServer.Clone() 通过这样做,Clone(),我现在拥有了所有列,以及每个列的数据类型 然后在程序的下一阶段,我将
SqlBulkCopy
:
我使用了一个查询,以便从SqlServer(2008)将现有数据提取到一个数据表中,
这样我就可以在本地对数据进行排序,避免在处理过程中碰到数据库
现在,在那个阶段,我已经可以选择克隆源数据表模式
使用localDataTable=DataTableFromOnlineSqlServer.Clone()代码>
通过这样做,Clone()
,我现在拥有了所有列,以及每个列的数据类型
然后在程序的下一阶段,我将用一些新数据填充克隆自Db的本地(但为空)新的DataTable
所以现在我有了一个填充的数据表
,它可以存储在sql server中了
使用下面的代码没有结果
public string UpdateDBWithNewDtUsingSQLBulkCopy(DataTable TheLocalDtToPush, string TheOnlineSQLTableName)
{
// Open a connection to the AdventureWorks database.
using (SqlConnection connection = new SqlConnection(RCLDBCONString))
{
connection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM "+TheOnlineSQLTableName +";", connection);
long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
var nl = "\r\n";
string retStrReport = "";
retStrReport = string.Concat(string.Format("Starting row count = {0}", countStart), nl);
retStrReport += string.Concat("==================================================", nl);
// Create a table with some rows.
DataTable newCustomers = TheLocalDtToPush;
// Create the SqlBulkCopy object.
// Note that the column positions in the source DataTable
// match the column positions in the destination table so
// there is no need to map columns.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.DestinationTableName = TheOnlineSQLTableName;
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(newCustomers);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
retStrReport += string.Concat(string.Format("Ending row count = {0}", countEnd), nl);
retStrReport += string.Concat("==================================================", nl);
retStrReport += string.Concat(string.Format("{0} rows were added.", countEnd - countStart),nl);
retStrReport += string.Concat("New Customers Was updated successfully", nl, "END OF PROCESS !");
Console.ReadLine();
return retStrReport;
}
}
现在的问题是,根本没有插入任何数据
我做了一些研究,没有解决办法
我还进行了检查,以确保:
源和目标的所有列都对齐(尽管
这是一个克隆人,因此不必担心)
有一个PK
设置为IDENTITY
Sql server表上的列
我错过了什么
…为了计算插入的行,我所做的“报告”告诉我们
“已添加0行”
也就是说,没有报告/抛出错误或验证。您是否检查了SQL错误日志,我怀疑您的localDT与联机的主键相同。若并没有为自动增量列启用标识插入,我建议不要在主字段中添加任何值。另外,如果要放置大量字段,请先尝试使用一小部分数据。请让我知道如何检查错误日志的问题&如果我制作了一个克隆,但我没有填写custId(pk)-它是身份自动递增的,bulkCopy是否会处理以下问题-source online有一个记录custId=1,这是因为“插入过程”吗?@SumitGupta我认为这很简单,我缺乏使用SqlBulkCopy的经验。顺便说一句,我用了大约230张唱片。是不是太多了(:@KingKing我在断点中查看它有大约200条记录(没有custId!!),现在对于行状态,请在使用范围内发布一些特定阶段的代码。。。PLS@LoneXcoderbulkCopy.WriteToServer(新客户,DataRowState.Added)
我不确定,您可以尝试使用数据行状态。未更改的。。。