批量复制顺序插入到SQL中 [英] Bulk Copy Sequential insert into sql

查看:261
本文介绍了批量复制顺序插入到SQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用 SqlBulkCopy 将我的DataTable数据插入到sql表中。我正在以所需的方式对我的c#Datatable中的数据进行排序并插入它们。但是当使用BulkCopy时,它不是以顺序方式插入的。请建议我如何使用 SqlBulkCopy
顺序插入记录




Hi,
I am using SqlBulkCopy to insert my DataTable data into a sql table. I am sorting the data in my c# Datatable in desired manner and inserting them. But when using BulkCopy it is not inserting in sequential manner. Please advice me how to insert the records in sequential manner using SqlBulkCopy


SqlBulkCopy bcp = new SqlBulkCopy(SqlConnectionString);
              bcp.ColumnMappings.Add("LineUp", "LineupID");
              bcp.ColumnMappings.Add("ChannelName", "ChannelName");
              bcp.ColumnMappings.Add("ChannelNumber", "ChannelID");
              bcp.ColumnMappings.Add("ItemID", "ItemID");
              bcp.ColumnMappings.Add("CLUIId", "CLUIId");
              bcp.DestinationTableName = "_ChannelLineUp";
              bcp.WriteToServer(dv.Table);







在上面的代码中我没有任何映射到的标识列桌子。请查看代码,并向我推荐所需的更改。



谢谢

Jagan Atmakuri

推荐答案

您无法对批量复制命令进行排序,您可以做的是将数据复制到临时表中并对其进行排序然后插入。 />


一个问题是,为什么要在插入表时对数据进行排序会增加额外的执行成本,您可以在从表中读取数据时对数据进行排序。 br />


或者其他方法是在表上为要排序的键列创建聚簇索引。
You can not do sorting on bulk copy command, What you can do is that copy the data into a temp table and sort that and then insert.

One question is that why you want to sort your data while inserting into a table it adds an extra execution cost , you can sort the data while reading it from the table.

Or other way is to create clustered index on table for the key columns you want to be sorted.


这篇关于批量复制顺序插入到SQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆