让 SqlBulkCopy 遵循列名
我正在将一些基于存储过程的报告例程转换为在 C# 中运行。总体思路是利用 C#/.NET Framework 的所有功能,然后将结果返回到数据库中。一切都进展顺利,除了我昨天遇到并今天解决的一个问题。
为了进行导入,我以编程方式创建一个 DataTable 并使用 SqlBulkCopy 将结果移动到服务器。
例如,
DataTable detail = new DataTable();
detail.Columns.Add(new DataColumn("Stock", Type.GetType("System.Decimal")));
detail.Columns.Add(new DataColumn("Receipts", Type.GetType("System.Decimal")));
detail.Columns.Add(new DataColumn("Orders", Type.GetType("System.Decimal")));
导入表的字段按以下顺序排列。
...
Stock decimal(18,4),
Orders decimal(18,4),
Receipts decimal(18,4)
...
本质上,收据的价值会转化为订单,反之亦然。
显然,这是因为 SqlBulkCopy 似乎不遵守我告诉它填充的列名称 - 它只是按顺序位置。
这对我来说是一个问题,因为我们使用 Redgate 的 SQL Compare。将更改从一个数据库推送到另一个数据库时,不一定会维护列的顺序位置。 (例如,如果您插入一个新列作为第三个序数字段,SQL Compare 只会在同步时将其附加到表中,使其成为最后一列)。
这严重扰乱了我的解决方案。现在,这些只是“导入”表,因此如果需要,我可以删除并重新创建它们,作为任何迁移脚本的一部分,并且序号位置保持不变。但我宁愿不这样做。
有没有办法强制 SqlBulkCopy 遵循您告诉它填充的列名称?
I'm in the process of converting some stored procedure based reporting routines to run in C#. The general idea is to use all the wonders of C#/.NET Framework and then blast the results back into the DB. Everything has been going swimmingly, except for one issue I ran into yesterday and resolved today.
To do the import, I'm programmatically creating a DataTable and using SqlBulkCopy to move the results to the server.
e.g.
DataTable detail = new DataTable();
detail.Columns.Add(new DataColumn("Stock", Type.GetType("System.Decimal")));
detail.Columns.Add(new DataColumn("Receipts", Type.GetType("System.Decimal")));
detail.Columns.Add(new DataColumn("Orders", Type.GetType("System.Decimal")));
The import table had the fields in the following order.
...
Stock decimal(18,4),
Orders decimal(18,4),
Receipts decimal(18,4)
...
Essentially, the value for Receipts was going into Orders, and vice versa.
Evidently, this is because SqlBulkCopy doesn't seem to be honoring the column name I've told it to populate - it just goes by ordinal position.
This is a problem for me as we use Redgate's SQL Compare. When pushing changes from one database to another, the ordinal position of columns isn't necessarily maintained. ( e.g. if you insert a new column as the third ordinal field, SQL Compare will just append it to the table on a sync, making it the last column ).
This seriously messes with my solution. Now, these are only 'import' tables, so if need be, I can drop and recreate them as part of any migration script with the ordinal positions intact. I'd rather not do that though.
Is there a way to force SqlBulkCopy to honor the column names you tell it to fill?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是修复此“错误”的解决方案。
默认是按序号/位置映射。
就我而言,我从电子表格中加载,其中的列按随机顺序排列。这是一个快速修复(table 是我的 DataTable,它“不按顺序排列”,bulkCopy 是 SqLBulkCopy 对象)
如您所见,我只是强制它按名称重新排序,即使名称相同。
Here's a solution to fix this 'bug'.
The default is to map by ordinal/position.
In my case I was loading from a spreadsheet with columns in random order. here's a quick fix(table is my DataTable that is 'out of ordinal order', and bulkCopy is the SqLBulkCopy object)
As you can see, i'm just forcing it to re-order by name, even though the names are IDENTICAL.
来自 http://msdn.microsoft.com/en-us/library /system.data.sqlclient.sqlbulkcopy.columnmappings.aspx :
有关示例代码,请参阅 http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server
From http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.columnmappings.aspx :
For sample code see "Mapping Columns" at http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server
下面是执行此操作的扩展方法:
用法:
这会清除现有的列映射,然后为传入的每个列添加映射。
Here's an extension method that does it:
Usage:
This clears existing column mappings, then adds a mapping for each column passed in.
项目选项中的 SQL Compare 中有一个选项是强制列顺序。这将生成一个脚本,对列进行重新排序。我可能是错的,但我认为此操作可能需要重建表,因此请谨慎使用,因为这可能会影响性能。但是,该脚本将保留表数据。
There is an option in SQL Compare to Force Column Order in the project Options. This will generate a script that will reorder the columns. I might be wrong but I think that this operation might require the table to be rebuilt, so please use with caution as this could impact performance. The script will, however, preserve the table data.
在批量复制中实现列映射的一种简单方法是,确保数据表列名称与要转储所有数据的表中的列名称完全相同
A simple way of implementing the column mapping in bulk copy, make sure your datatable columns names are exactly the same as in the table you want to dump all the data
有一些重载可让您指定序数位置或要映射到目标列的源列的名称。
查看 SqlBulkCopyColumnMappingCollection.Add 并设置
SqlBulkCopy.ColumnMappings
属性。There are overloads that let you specify the ordinal position, or the name of the source column to be mapped to the destination column.
Check out SqlBulkCopyColumnMappingCollection.Add and set the
SqlBulkCopy.ColumnMappings
property.我可能遗漏了一些东西,但是为什么你不能更改列的顺序呢?我通常不会手动构建我的
DataTable
。我使用SqlDataAdapter
的 FillSchema 方法,使用“select * from targetTable”
查询。因此,我始终确信我的 DataTable 适合目标表。I might be missing something, but why couldn't you change the ordering of your columns? I usually don't build my
DataTable
by hand. I use the FillSchema method ofSqlDataAdapter
, using a"select * from targetTable"
query. So I'm always sure that myDataTable
fits the target table.