让 SqlBulkCopy 遵循列名

发布于 2024-12-01 03:59:43 字数 964 浏览 6 评论 0原文

我正在将一些基于存储过程的报告例程转换为在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

背叛残局 2024-12-08 03:59:43

这是修复此“错误”的解决方案。

默认是按序号/位置映射。

就我而言,我从电子表格中加载,其中的列按随机顺序排列。这是一个快速修复(table 是我的 DataTable,它“不按顺序排列”,bulkCopy 是 SqLBulkCopy 对象)

foreach (DataColumn col in table.Columns)
{
    bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}

如您所见,我只是强制它按名称重新排序,即使名称相同。

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)

foreach (DataColumn col in table.Columns)
{
    bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}

As you can see, i'm just forcing it to re-order by name, even though the names are IDENTICAL.

早乙女 2024-12-08 03:59:43

来自 http://msdn.microsoft.com/en-us/library /system.data.sqlclient.sqlbulkcopy.columnmappings.aspx :

但是,如果列数不同,或者序数位置不同
一致,您必须使用 ColumnMappings 来确保数据是
复制到正确的列中。

有关示例代码,请参阅 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 :

However, if the column counts differ, or the ordinal positions are not
consistent, you must use ColumnMappings to make sure that data is
copied into the correct columns.

For sample code see "Mapping Columns" at http://www.sqlteam.com/article/use-sqlbulkcopy-to-quickly-load-data-from-your-client-to-sql-server

刘备忘录 2024-12-08 03:59:43

下面是执行此操作的扩展方法:

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

public static class SqlBulkCopyExtensions
{
    public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, DataColumnCollection columns) => WithColumnMappings(sqlBulkCopy, columns.Cast<DataColumn>());

    public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, IEnumerable<DataColumn> columns)
    {
        sqlBulkCopy.ColumnMappings.Clear();

        foreach (DataColumn column in columns)
        {
            sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
        }

        return sqlBulkCopy;
    }
}

用法:

bulkCopy
    .WithColumnMappings(table.Columns)
    .WriteToServer(table);

这会清除现有的列映射,然后为传入的每个列添加映射。

Here's an extension method that does it:

using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

public static class SqlBulkCopyExtensions
{
    public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, DataColumnCollection columns) => WithColumnMappings(sqlBulkCopy, columns.Cast<DataColumn>());

    public static SqlBulkCopy WithColumnMappings(this SqlBulkCopy sqlBulkCopy, IEnumerable<DataColumn> columns)
    {
        sqlBulkCopy.ColumnMappings.Clear();

        foreach (DataColumn column in columns)
        {
            sqlBulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
        }

        return sqlBulkCopy;
    }
}

Usage:

bulkCopy
    .WithColumnMappings(table.Columns)
    .WriteToServer(table);

This clears existing column mappings, then adds a mapping for each column passed in.

想你的星星会说话 2024-12-08 03:59:43

项目选项中的 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.

随遇而安 2024-12-08 03:59:43

在批量复制中实现列映射的一种简单方法是,确保数据表列名称与要转储所有数据的表中的列名称完全相同

using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dc.ConnectionString))
{
        bulkcopy.BulkCopyTimeout = 150; 

        // column mapping defined
        dt.Columns.Cast<DataColumn>().ToList().ForEach(f =>
        {
            SqlBulkCopyColumnMapping bccm = new SqlBulkCopyColumnMapping();
            bccm.DestinationColumn = f.ColumnName;
            bccm.SourceColumn = f.ColumnName;
            bulkcopy.ColumnMappings.Add(bccm);
         });
        // column mapping defined

         bulkcopy.DestinationTableName = outputTable;
         bulkcopy.WriteToServer(dt);

}

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

using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dc.ConnectionString))
{
        bulkcopy.BulkCopyTimeout = 150; 

        // column mapping defined
        dt.Columns.Cast<DataColumn>().ToList().ForEach(f =>
        {
            SqlBulkCopyColumnMapping bccm = new SqlBulkCopyColumnMapping();
            bccm.DestinationColumn = f.ColumnName;
            bccm.SourceColumn = f.ColumnName;
            bulkcopy.ColumnMappings.Add(bccm);
         });
        // column mapping defined

         bulkcopy.DestinationTableName = outputTable;
         bulkcopy.WriteToServer(dt);

}
离旧人 2024-12-08 03:59:43

有一些重载可让您指定序数位置或要映射到目标列的源列的名称。

查看 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.

樱娆 2024-12-08 03:59:43

我可能遗漏了一些东西,但是为什么你不能更改列的顺序呢?我通常不会手动构建我的DataTable。我使用 SqlDataAdapterFillSchema 方法,使用 “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 of SqlDataAdapter, using a "select * from targetTable" query. So I'm always sure that my DataTablefits the target table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文