Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跳过SqlBulkInsert中的某些列_C#_Sql Server - Fatal编程技术网

C# 跳过SqlBulkInsert中的某些列

C# 跳过SqlBulkInsert中的某些列,c#,sql-server,C#,Sql Server,我想将数据表大容量插入SQL Server。问题是我想将特殊列从datatable批量插入SQL Server 我该怎么办 SqlConnection con = new SqlConnection(connectionazmoonak); SqlBulkCopy bulk = new SqlBulkCopy(con); bulk.DestinationTableName = "targetTable"; DataSet ds = new DataSet(); ds.Tables.Add(t

我想将数据表大容量插入SQL Server。问题是我想将特殊列从datatable批量插入SQL Server

我该怎么办

SqlConnection con = new SqlConnection(connectionazmoonak);

SqlBulkCopy bulk = new SqlBulkCopy(con);
bulk.DestinationTableName = "targetTable";

DataSet ds = new DataSet();
ds.Tables.Add(targetTable);

foreach (DataColumn col in ds.Tables[0].Columns)
    bulk.ColumnMappings.Add(col.ColumnName, col.ColumnName);

con.Open();
bulk.WriteToServer(ds.Tables[0]);
con.Close();

此代码可以工作,但它会插入所有列。请帮帮我。

是的,是的,您需要将所有内容批量插入表暂存表中,然后从那里将所需内容加载到最终生产表中。看看下面的链接


这应该会让您了解如何让它按自己的喜好工作。

我在过去两天一直在处理批量插入,这里是一个通用的批量插入类,该类允许您排除一些列:

那么,它必须像这样使用:

var myEntityBulk = new BulkInsert<MyEntity>(
    _mYConnectionString,
     "MyEntities", 
      myEntities, 
      new[] { "ObjectState","NavigationPropertyOne", "NavigationPropertyTwo" }
);
myEntityBulk.Insert();
我希望它会有帮助,我很肯定它会


注意:对于我来说,下一步是定制映射,例如从DbGeography到SqlGeography,因为在正常执行期间,我的一些实体由EF存储库和我的域类别中的I DbGeography属性管理,但SqlBulkCopy只管理SqlGeography属性。我会考虑一下,也许我会更新我的答案。

这就是批量插入的工作方式-通过设计。它可以插入所有内容,但插入速度非常快。如果您需要更改某些内容—将大容量插入到临时表中,然后将数据加载到SQL Server中后,您可以删除或忽略列、修改数据等—大容量插入针对快速插入而不是灵活插入进行了优化。
 /// <summary>
/// This class is intended to perform a bulk insert of a list of elements into a table in a Database. 
/// This class also allows you to use the same domain classes that you were already using because you
/// can include not mapped properties into the field excludedPropertyNames.
/// </summary>
/// <typeparam name="T">The class that is going to be mapped.</typeparam>
public class BulkInsert<T> where T : class
{
    #region Fields

    private readonly LoggingService _logger = new LoggingService(typeof(BulkInsert<T>));
    private string _connectionString;
    private string _tableName;
    private IEnumerable<string> _excludedPropertyNames;
    private int _batchSize;
    private IEnumerable<T> _data;
    private DataTable _dataTable;

    #endregion

    #region Constructor

    /// <summary>
    /// Initializes a new instance of the <see cref="BulkInsert{T}"/> class.
    /// </summary>
    /// <param name="connectionString">The connection string.</param>
    /// <param name="tableName">Name of the table.</param>
    /// <param name="data">The data.</param>
    /// <param name="excludedPropertyNames">The excluded property names.</param>
    /// <param name="batchSize">Size of the batch.</param>
    public BulkInsert(
        string connectionString,
        string tableName,
        IEnumerable<T> data,
        IEnumerable<string> excludedPropertyNames,
        int batchSize = 1000)
    {
        if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(nameof(connectionString));
        if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException(nameof(tableName));
        if (data == null) throw new ArgumentNullException(nameof(data));
        if (batchSize <= 0) throw new ArgumentOutOfRangeException(nameof(batchSize));

        _connectionString = connectionString;
        _tableName = tableName;
        _batchSize = batchSize;
        _data = data;
        _excludedPropertyNames = excludedPropertyNames == null ? new List<string>() : excludedPropertyNames;
        _dataTable = CreateCustomDataTable();
    }

    #endregion

    #region Public Methods

    /// <summary>
    /// Inserts the data with a bulk copy inside a transaction. 
    /// </summary>
    public void Insert()
    {
        using (var connection = new SqlConnection(_connectionString))
        {
            connection.Open();
            SqlTransaction transaction = connection.BeginTransaction();

            using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default | SqlBulkCopyOptions.KeepIdentity, transaction))
            {
                bulkCopy.BatchSize = _batchSize;
                bulkCopy.DestinationTableName = _tableName;

                // Let's fix tons of mapping issues by
                // Setting the column mapping in SqlBulkCopy instance:
                foreach (DataColumn dataColumn in _dataTable.Columns)
                {
                    bulkCopy.ColumnMappings.Add(dataColumn.ColumnName, dataColumn.ColumnName);
                }

                try
                {
                    bulkCopy.WriteToServer(_dataTable);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                    transaction.Rollback();
                    connection.Close();
                }
            }

            transaction.Commit();
        }
    }

    #endregion

    #region Private Helper Methods

    /// <summary>
    /// Creates the custom data table.
    /// </summary>
    private DataTable CreateCustomDataTable()
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        var table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
        {
            // Just include the not excluded columns
            if (_excludedPropertyNames.All(epn => epn != prop.Name))
            {
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }
        }
        foreach (T item in _data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
                // Just include the values in not excluded properties 
                if (_excludedPropertyNames.All(epn => epn != prop.Name))
                {
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                }
            }
            table.Rows.Add(row);
        }
        return table;
    }

    #endregion

}
var myEntityBulk = new BulkInsert<MyEntity>(
    _mYConnectionString,
     "MyEntities", 
      myEntities, 
      new[] { "ObjectState","NavigationPropertyOne", "NavigationPropertyTwo" }
);
myEntityBulk.Insert();