如何使用SqlBulkCopyColumnMappingCollection?如何使用、SqlBulkCopyColumnMappingCollection

2023-09-02 10:51:17 作者:不愿将就

我想打,我可以通过传递特定的数据通过参数用我所有的批量插入一个SqlBulkCopy的方法。

现在我需要做的映射在其中的一些。我不知道如何做一个SqlBulkCopyColumnMappingCollection因为这是我的计划通过在映射收集并使用它。但是我不知道如何做到这一点。我不能做它的一个新的对象。

这是我现在有。如何添加它做映射放它传递的?

 公共无效BatchBulkCopy(DataTable中的dataTable,串DestinationTbl,整数BATCHSIZE)
{
    //获取数据表
    数据表dtInsertRows = dataTable的;

    使用(SqlBulkCopy的SBC =新SqlBulkCopy的(的connectionString,SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        //一气呵成要处理的记录数
        sbc.BatchSize = BATCHSIZE;

        //最后写入服务器
        sbc.WriteToServer(dtInsertRows);
    }
}
 
混世魔王 到学校就变纸老虎,注意这3点,防止孩子心理疾病

解决方案

您并不需要创建它的一个新的实例 - SqlBulkCopy类有一个属性,它是一个映射集合,你可以使用:

 公共无效BatchBulkCopy(DataTable中的dataTable,串DestinationTbl,整数BATCHSIZE)
{
    //获取数据表
    数据表dtInsertRows = dataTable的;

    使用(SqlBulkCopy的SBC =新SqlBulkCopy的(的connectionString,SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        //一气呵成要处理的记录数
        sbc.BatchSize = BATCHSIZE;

        //添加您的列映射在这里
        sbc.ColumnMappings.Add(字段1,FIELD3);
        sbc.ColumnMappings.Add(富,酒吧);

        //最后写入服务器
        sbc.WriteToServer(dtInsertRows);
    }
}
 

编辑:

根据该意见

,我们的目标是使一个通用的功能,例如:没有到c明确地在函数映射硬$ C $。由于ColumnMappingCollection不能被实例化,我会建议传递名单,其中,串> 或相似的包含列映射定义到函数。例如:

  VAR columnMapping =新的名单,其中,串>();
columnMapping.Add(字段1,FIELD3);
columnMapping.Add(富,巴);
 

然后重新定义该函数为

 公共无效BatchBulkCopy(DataTable中的dataTable,串DestinationTbl,整数BATCHSIZE,列表和LT;字符串> columnMapping)
{
    //获取数据表
    数据表dtInsertRows = dataTable的;

    使用(SqlBulkCopy的SBC =新SqlBulkCopy的(的connectionString,SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        //一气呵成要处理的记录数
        sbc.BatchSize = BATCHSIZE;

        //添加您的列映射在这里
        的foreach(在columnMapping VAR映射)
        {
            VAR分裂= mapping.Split(新[] {','});
            sbc.ColumnMappings.Add(split.First(),split.Last());
        }

        //最后写入服务器
        sbc.WriteToServer(dtInsertRows);
    }
}
 

I want to make one SqlBulkCopy method that I can use for all my bulk inserts by passing in specific data through the parameters.

Now I need to do mapping on some of them. I don't know how to make a SqlBulkCopyColumnMappingCollection since that was my plan to pass in the mapping collection in and use it. However I don't know how to make it. I can't make a new object of it.

This is what I have now. How can I add it do mapping put pass it in?

public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize)
{
    // Get the DataTable 
    DataTable dtInsertRows = dataTable;

    using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        // Number of records to be processed in one go
        sbc.BatchSize = batchSize;

        // Finally write to server
        sbc.WriteToServer(dtInsertRows);
    }
}

解决方案

You don't need to create a new instance of it - the SqlBulkCopy class has a property which is a mapping collection that you can use:

public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize)
{
    // Get the DataTable 
    DataTable dtInsertRows = dataTable;

    using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        // Number of records to be processed in one go
        sbc.BatchSize = batchSize;

        // Add your column mappings here
        sbc.ColumnMappings.Add("field1","field3");
        sbc.ColumnMappings.Add("foo","bar");

        // Finally write to server
        sbc.WriteToServer(dtInsertRows);
    }    
}

EDIT:

Based on the comments, the goal was to make a generic function, e.g. not have to hardcode the mapping explicitly in the function. Since the ColumnMappingCollection cannot be instantiated, I would recommend passing a List<string> or similar that contains the column mapping definition into the function. For example:

var columnMapping = new List<string>();
columnMapping.Add("field1,field3");
columnMapping.Add("foo,bar");

Then re-define the function as

public void BatchBulkCopy(DataTable dataTable, string DestinationTbl, int batchSize, List<string> columnMapping)
{
    // Get the DataTable 
    DataTable dtInsertRows = dataTable;

    using (SqlBulkCopy sbc = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
    {
        sbc.DestinationTableName = DestinationTbl;

        // Number of records to be processed in one go
        sbc.BatchSize = batchSize;

        // Add your column mappings here
        foreach(var mapping in columnMapping)
        {
            var split = mapping.Split(new[] { ',' });
            sbc.ColumnMappings.Add(split.First(), split.Last());
        }

        // Finally write to server
        sbc.WriteToServer(dtInsertRows);
    }
}