Пример #1
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        /// <exception cref="SqlBulkToolsException"></exception>
        /// <exception cref="IdentityException"></exception>
        public async Task <int> CommitAsync(SqlConnection connection)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }

            if (!_deleteWhenNotMatchedFlag && _deletePredicates.Count > 0)
            {
                throw new SqlBulkToolsException($"{BulkOperationsHelper.GetPredicateMethodName(PredicateType.Delete)} only usable on BulkInsertOrUpdate " +
                                                $"method when 'DeleteWhenNotMatched' is set to true.");
            }

            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _updatePredicates);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _excludeFromUpdate);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync();
            }

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);

            try
            {
                SqlCommand command = connection.CreateCommand();

                command.Connection     = connection;
                command.CommandTimeout = _sqlTimeout;

                _nullableColumnDic = BulkOperationsHelper.GetNullableColumnDic(dtCols);

                //Creating temp table on database
                command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                await command.ExecuteNonQueryAsync();

                BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopySettings);

                string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                           OperationType.InsertOrUpdate, _identityColumn);

                if (!string.IsNullOrWhiteSpace(comm))
                {
                    command.CommandText = comm;
                    await command.ExecuteNonQueryAsync();
                }

                comm = GetCommand(connection);

                command.CommandText = comm;

                if (_parameters.Count > 0)
                {
                    command.Parameters.AddRange(_parameters.ToArray());
                }

                affectedRows = await command.ExecuteNonQueryAsync();

                if (_outputIdentity == ColumnDirectionType.InputOutput)
                {
                    await BulkOperationsHelper.LoadFromTmpOutputTableAsync(command, _identityColumn, _outputIdentityDic, OperationType.InsertOrUpdate, _list);
                }

                return(affectedRows);
            }
            catch (SqlException e)
            {
                for (int i = 0; i < e.Errors.Count; i++)
                {
                    // Error 8102 is identity error.
                    if (e.Errors[i].Number == 8102)
                    {
                        // Expensive but neccessary to inform user of an important configuration setup.
                        throw new IdentityException(e.Errors[i].Message);
                    }
                }

                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        /// <exception cref="SqlBulkToolsException"></exception>
        /// <exception cref="IdentityException"></exception>
        public async Task <int> CommitAsync(SqlConnection connection)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }

            if (!_deleteWhenNotMatchedFlag && _deletePredicates.Count > 0)
            {
                throw new SqlBulkToolsException($"{BulkOperationsHelper.GetPredicateMethodName(PredicateType.Delete)} only usable on BulkInsertOrUpdate " +
                                                $"method when 'DeleteWhenNotMatched' is set to true.");
            }

            base.IndexCheck();
            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_columns, _customColumnMappings, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(dt, _list, _columns, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _updatePredicates);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync();
            }

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);

            try
            {
                SqlCommand command = connection.CreateCommand();

                command.Connection     = connection;
                command.CommandTimeout = _sqlTimeout;

                //Creating temp table on database
                command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                await command.ExecuteNonQueryAsync();

                BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopyEnableStreaming, _bulkCopyBatchSize,
                                                      _bulkCopyNotifyAfter, _bulkCopyTimeout, _sqlBulkCopyOptions, _bulkCopyDelegates);

                if (_disableIndexList != null && _disableIndexList.Any())
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Disable, _tableName,
                                                                                     _schema, connection, _disableIndexList, _disableAllIndexes);
                    await command.ExecuteNonQueryAsync();
                }

                string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                           OperationType.InsertOrUpdate, _identityColumn);

                if (!string.IsNullOrWhiteSpace(comm))
                {
                    command.CommandText = comm;
                    await command.ExecuteNonQueryAsync();
                }

                comm =
                    "MERGE INTO " + BulkOperationsHelper.GetFullQualifyingTableName(connection.Database, _schema, _tableName) +
                    " WITH (HOLDLOCK) AS Target " +
                    "USING " + Constants.TempTableName + " AS Source " +
                    BulkOperationsHelper.BuildJoinConditionsForInsertOrUpdate(_matchTargetOn.ToArray(),
                                                                              Constants.SourceAlias, Constants.TargetAlias, base._collationColumnDic) +
                    "WHEN MATCHED " + BulkOperationsHelper.BuildPredicateQuery(_matchTargetOn.ToArray(), _updatePredicates, Constants.TargetAlias, base._collationColumnDic) +
                    "THEN UPDATE " +
                    BulkOperationsHelper.BuildUpdateSet(_columns, Constants.SourceAlias, Constants.TargetAlias, _identityColumn, _excludeFromUpdate) +
                    "WHEN NOT MATCHED BY TARGET THEN " +
                    BulkOperationsHelper.BuildInsertSet(_columns, Constants.SourceAlias, _identityColumn) +
                    (_deleteWhenNotMatchedFlag ? " WHEN NOT MATCHED BY SOURCE " + BulkOperationsHelper.BuildPredicateQuery(_matchTargetOn.ToArray(),
                                                                                                                           _deletePredicates, Constants.TargetAlias, base._collationColumnDic) +
                     "THEN DELETE " : " ") +
                    BulkOperationsHelper.GetOutputIdentityCmd(_identityColumn, _outputIdentity, Constants.TempOutputTableName,
                                                              OperationType.InsertOrUpdate) + "; " +
                    "DROP TABLE " + Constants.TempTableName + ";";

                command.CommandText = comm;

                if (_parameters.Count > 0)
                {
                    command.Parameters.AddRange(_parameters.ToArray());
                }

                affectedRows = await command.ExecuteNonQueryAsync();

                if (_disableIndexList != null && _disableIndexList.Any())
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Rebuild,
                                                                                     _tableName, _schema, connection, _disableIndexList);
                    await command.ExecuteNonQueryAsync();
                }

                if (_outputIdentity == ColumnDirectionType.InputOutput)
                {
                    await BulkOperationsHelper.LoadFromTmpOutputTableAsync(command, _identityColumn, _outputIdentityDic, OperationType.InsertOrUpdate, _list);
                }

                return(affectedRows);
            }

            catch (SqlException e)
            {
                for (int i = 0; i < e.Errors.Count; i++)
                {
                    // Error 8102 is identity error.
                    if (e.Errors[i].Number == 8102)
                    {
                        // Expensive but neccessary to inform user of an important configuration setup.
                        throw new IdentityException(e.Errors[i].Message);
                    }
                }

                throw;
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="credentials"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        async Task <int> ITransaction.CommitTransactionAsync(string connectionName, SqlCredential credentials, SqlConnection connection)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }
            base.IndexCheck();
            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_columns, _customColumnMappings, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(dt, _list, _columns, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);


            using (SqlConnection conn = BulkOperationsHelper.GetSqlConnection(connectionName, credentials, connection))
            {
                await conn.OpenAsync();

                var dtCols = BulkOperationsHelper.GetDatabaseSchema(conn, _schema, _tableName);

                using (SqlTransaction transaction = conn.BeginTransaction())
                {
                    try
                    {
                        SqlCommand command = conn.CreateCommand();
                        command.Connection     = conn;
                        command.Transaction    = transaction;
                        command.CommandTimeout = _sqlTimeout;

                        //Creating temp table on database
                        command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                        await command.ExecuteNonQueryAsync();

                        await BulkOperationsHelper.InsertToTmpTableAsync(conn, transaction, dt, _bulkCopyEnableStreaming, _bulkCopyBatchSize,
                                                                         _bulkCopyNotifyAfter, _bulkCopyTimeout, _sqlBulkCopyOptions, _bulkCopyDelegates);

                        if (_disableIndexList != null && _disableIndexList.Any())
                        {
                            command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(IndexOperation.Disable, _tableName,
                                                                                             _schema, conn, _disableIndexList, _disableAllIndexes);
                            await command.ExecuteNonQueryAsync();
                        }

                        string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                                   OperationType.InsertOrUpdate, _identityColumn);

                        if (!string.IsNullOrWhiteSpace(comm))
                        {
                            command.CommandText = comm;
                            command.ExecuteNonQuery();
                        }

                        // Updating destination table, and dropping temp table
                        comm = "MERGE INTO " + BulkOperationsHelper.GetFullQualifyingTableName(conn.Database, _schema, _tableName) + " WITH (HOLDLOCK) AS Target " +
                               "USING " + Constants.TempTableName + " AS Source " +
                               BulkOperationsHelper.BuildJoinConditionsForUpdateOrInsert(_matchTargetOn.ToArray(),
                                                                                         Constants.SourceAlias, Constants.TargetAlias) +
                               "WHEN MATCHED " + BulkOperationsHelper.BuildPredicateQuery(_matchTargetOn.ToArray(), _deletePredicates, Constants.TargetAlias) +
                               "THEN DELETE " +
                               BulkOperationsHelper.GetOutputIdentityCmd(_identityColumn, _outputIdentity, Constants.TempOutputTableName,
                                                                         OperationType.Delete) + "; " +
                               "DROP TABLE " + Constants.TempTableName + ";";
                        command.CommandText = comm;

                        if (_parameters.Count > 0)
                        {
                            command.Parameters.AddRange(_parameters.ToArray());
                        }

                        affectedRows = await command.ExecuteNonQueryAsync();

                        if (_disableIndexList != null && _disableIndexList.Any())
                        {
                            command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(IndexOperation.Rebuild, _tableName,
                                                                                             _schema, conn, _disableIndexList);
                            await command.ExecuteNonQueryAsync();
                        }

                        if (_outputIdentity == ColumnDirection.InputOutput)
                        {
                            await
                            BulkOperationsHelper.LoadFromTmpOutputTableAsync(command, _identityColumn, _outputIdentityDic,
                                                                             OperationType.Delete, _list);
                        }

                        transaction.Commit();
                        return(affectedRows);
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
Пример #4
0
        int ITransaction.CommitTransaction(string connectionName, SqlCredential credentials, SqlConnection connection)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }

            base.IndexCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_columns, _customColumnMappings, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(dt, _list, _columns);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);

            using (SqlConnection conn = BulkOperationsHelper.GetSqlConnection(connectionName, credentials, connection))
            {
                conn.Open();
                DataTable dtCols = null;
                if (_outputIdentity == ColumnDirection.InputOutput)
                {
                    dtCols = BulkOperationsHelper.GetDatabaseSchema(conn, _schema, _tableName);
                }

                using (SqlTransaction transaction = conn.BeginTransaction())
                {
                    //Bulk insert into temp table
                    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn, _sqlBulkCopyOptions, transaction))
                    {
                        try
                        {
                            bulkcopy.DestinationTableName = BulkOperationsHelper.GetFullQualifyingTableName(conn.Database, _schema, _tableName);
                            BulkOperationsHelper.MapColumns(bulkcopy, _columns, _customColumnMappings);

                            BulkOperationsHelper.SetSqlBulkCopySettings(bulkcopy, _bulkCopyEnableStreaming, _bulkCopyBatchSize,
                                                                        _bulkCopyNotifyAfter, _bulkCopyTimeout, _bulkCopyDelegates);

                            SqlCommand command = conn.CreateCommand();
                            command.Connection  = conn;
                            command.Transaction = transaction;

                            if (_disableAllIndexes || (_disableIndexList != null && _disableIndexList.Any()))
                            {
                                command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(IndexOperation.Disable, _tableName,
                                                                                                 _schema, conn, _disableIndexList, _disableAllIndexes);
                                command.ExecuteNonQuery();
                            }

                            // If InputOutput identity is selected, must use staging table.
                            if (_outputIdentity == ColumnDirection.InputOutput && dtCols != null)
                            {
                                command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                                command.ExecuteNonQuery();

                                BulkOperationsHelper.InsertToTmpTable(conn, transaction, dt, _bulkCopyEnableStreaming, _bulkCopyBatchSize,
                                                                      _bulkCopyNotifyAfter, _bulkCopyTimeout, _sqlBulkCopyOptions, _bulkCopyDelegates);

                                command.CommandText = BulkOperationsHelper.GetInsertIntoStagingTableCmd(command, conn, _schema, _tableName,
                                                                                                        _columns, _identityColumn, _outputIdentity);
                                command.ExecuteNonQuery();

                                BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic, OperationType.Insert, _list);
                            }

                            else
                            {
                                bulkcopy.WriteToServer(dt);
                            }

                            if (_disableAllIndexes || (_disableIndexList != null && _disableIndexList.Any()))
                            {
                                command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(IndexOperation.Rebuild, _tableName,
                                                                                                 _schema, conn, _disableIndexList, _disableAllIndexes);
                                command.ExecuteNonQuery();
                            }

                            transaction.Commit();
                            bulkcopy.Close();

                            affectedRows = dt.Rows.Count;
                            return(affectedRows);
                        }

                        catch (Exception)
                        {
                            transaction.Rollback();
                            throw;
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Commits a transaction to database. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        /// <exception cref="IdentityException"></exception>
        public int Commit(SqlConnection connection, SqlTransaction transaction)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }

            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _updatePredicates);

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(bulk, connection, _schema, _tableName);

            try
            {
                SqlCommand command = connection.CreateCommand();
                command.Connection     = connection;
                command.CommandTimeout = _sqlTimeout;
                command.Transaction    = transaction;

                _nullableColumnDic = BulkOperationsHelper.GetNullableColumnDic(dtCols);

                //Creating temp table on database
                command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                command.ExecuteNonQuery();

                //Bulk insert into temp table
                BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopySettings, transaction);

                string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                           OperationType.InsertOrUpdate, _identityColumn);

                if (!string.IsNullOrWhiteSpace(comm))
                {
                    command.CommandText = comm;
                    command.ExecuteNonQuery();
                }

                comm = GetCommand(connection);

                command.CommandText = comm;

                if (_parameters.Count > 0)
                {
                    command.Parameters.AddRange(_parameters.ToArray());
                }

                affectedRows = command.ExecuteNonQuery();

                if (_outputIdentity == ColumnDirectionType.InputOutput)
                {
                    BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic, OperationType.InsertOrUpdate, _list);
                }

                return(affectedRows);
            }
            catch (SqlException e)
            {
                for (int i = 0; i < e.Errors.Count; i++)
                {
                    // Error 8102 is identity error.
                    if (e.Errors[i].Number == 8102)
                    {
                        // Expensive call but neccessary to inform user of an important configuration setup.
                        throw new IdentityException(e.Errors[i].Message);
                    }
                }
                throw;
            }
        }
Пример #6
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        /// <exception cref="IdentityException"></exception>
        public async Task <int> CommitAsync(SqlConnection connection, int commandTimeout = 0)
        {
            try
            {
                int affectedRows = 0;
                if (!_list.Any())
                {
                    return(affectedRows);
                }

                base.MatchTargetCheck();

                DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);
                dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic, _outputIdentityDic);

                // Must be after ToDataTable is called.
                BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);
                BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _updatePredicates);

                if (connection.State == ConnectionState.Closed)
                {
                    await connection.OpenAsync();
                }

                BulkOperationsHelper.ValidateMsSqlVersion(connection, OperationType.Update);

                var dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);


                SqlCommand command = connection.CreateCommand();
                command.Connection     = connection;
                command.CommandTimeout = commandTimeout;

                //Creating temp table on database
                var schemaDetail = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                command.CommandText = schemaDetail.BuildCreateTableQuery;
                await command.ExecuteNonQueryAsync();

                _nullableColumnDic = schemaDetail.NullableDic;

                //Insert into temp table
                if (BulkOperationsHelper.GetBulkInsertStrategyType(dt, _columns) ==
                    BulkInsertStrategyType.MultiValueInsert)
                {
                    var tempTableSetup = BulkOperationsHelper.BuildInsertQueryFromDataTable(_customColumnMappings, dt, _identityColumn, _columns,
                                                                                            _bulkCopySettings, schemaDetail, Constants.TempTableName, keepIdentity: true, keepInternalId: true);
                    command.CommandText = tempTableSetup.InsertQuery;
                    command.Parameters.AddRange(tempTableSetup.SqlParameterList.ToArray());
                    await command.ExecuteNonQueryAsync();

                    command.Parameters.Clear();
                }
                else
                {
                    await BulkOperationsHelper.InsertToTmpTableWithBulkCopyAsync(connection, dt, _bulkCopySettings);
                }

                string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                           OperationType.Update, _identityColumn);

                if (!string.IsNullOrWhiteSpace(comm))
                {
                    command.CommandText = comm;
                    await command.ExecuteNonQueryAsync();
                }

                comm = GetCommand(connection);

                command.CommandText = comm;

                if (_parameters.Count > 0)
                {
                    command.Parameters.AddRange(_parameters.ToArray());
                }

                affectedRows = await command.ExecuteNonQueryAsync();

                if (_outputIdentity == ColumnDirectionType.InputOutput)
                {
                    await BulkOperationsHelper.LoadFromTmpOutputTableAsync(command, _identityColumn, _outputIdentityDic, OperationType.InsertOrUpdate, _list);
                }

                return(affectedRows);
            }

            catch (SqlException e)
            {
                for (int i = 0; i < e.Errors.Count; i++)
                {
                    // Error 8102 is identity error.
                    if (e.Errors[i].Number == 544 || e.Errors[i].Number == 8102)
                    {
                        // Expensive call but neccessary to inform user of an important configuration setup.
                        throw new IdentityException(e.Errors[i].Message);
                    }
                }
                throw;
            }
        }
Пример #7
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        public async Task <int> CommitAsync(SqlConnection connection, SqlTransaction transaction)
        {
            int affectedRows = 0;

            if (!_list.Any())
            {
                return(affectedRows);
            }

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync();
            }

            DataTable dtCols = null;

            if (_outputIdentity == ColumnDirectionType.InputOutput)
            {
                dtCols = BulkOperationsHelper.GetDatabaseSchema(bulk, connection, _schema, _tableName);
            }

            using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection, _bulkCopySettings.SqlBulkCopyOptions, transaction))
            {
                bulkcopy.DestinationTableName = BulkOperationsHelper.GetFullQualifyingTableName(connection.Database, _schema, _tableName);
                BulkOperationsHelper.MapColumns(bulkcopy, _columns, _customColumnMappings);

                BulkOperationsHelper.SetSqlBulkCopySettings(bulkcopy, _bulkCopySettings);

                SqlCommand command = connection.CreateCommand();
                command.Connection     = connection;
                command.CommandTimeout = _sqlTimeout;
                command.Transaction    = transaction;

                if (_disableAllIndexes)
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Disable, _tableName,
                                                                                     _schema, connection);
                    await command.ExecuteNonQueryAsync();
                }

                // If InputOutput identity is selected, must use staging table.
                if (_outputIdentity == ColumnDirectionType.InputOutput && dtCols != null)
                {
                    command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
                    await command.ExecuteNonQueryAsync();

                    BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopySettings, transaction);

                    command.CommandText = BulkOperationsHelper.GetInsertIntoStagingTableCmd(connection, _schema, _tableName,
                                                                                            _columns, _identityColumn, _outputIdentity);
                    await command.ExecuteNonQueryAsync();

                    BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic, OperationType.Insert, _list);
                }
                else
                {
                    await bulkcopy.WriteToServerAsync(dt);
                }

                if (_disableAllIndexes)
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Rebuild, _tableName,
                                                                                     _schema, connection);
                    await command.ExecuteNonQueryAsync();
                }

                bulkcopy.Close();

                affectedRows = dt.Rows.Count;
                return(affectedRows);
            }
        }
Пример #8
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public async Task <int> CommitAsync(SqlConnection connection, SqlTransaction transaction)
        {
            var affectedRecords = 0;

            if (!_list.Any())
            {
                return(affectedRecords);
            }

            MatchTargetCheck();

            var dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn,
                                                              _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync();
            }

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(_bulk, connection, _schema, _tableName);

            var command = connection.CreateCommand();

            command.Connection     = connection;
            command.CommandTimeout = _sqlTimeout;
            command.Transaction    = transaction;

            _nullableColumnDic = BulkOperationsHelper.GetNullableColumnDic(dtCols);

            //Creating temp table on database
            command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
            await command.ExecuteNonQueryAsync();

            BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopySettings, transaction);

            var comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                    OperationType.InsertOrUpdate, _identityColumn);

            if (!string.IsNullOrWhiteSpace(comm))
            {
                command.CommandText = comm;
                await command.ExecuteNonQueryAsync();
            }

            comm = GetCommand(connection);

            command.CommandText = comm;

            if (_parameters.Count > 0)
            {
                command.Parameters.AddRange(_parameters.ToArray());
            }

            affectedRecords = command.ExecuteNonQuery();

            if (_outputIdentity == ColumnDirectionType.InputOutput)
            {
                await BulkOperationsHelper.LoadFromTmpOutputTableAsync(command, _identityColumn, _outputIdentityDic, OperationType.Delete, _list);
            }

            return(affectedRecords);
        }
Пример #9
0
        /// <summary>
        /// Commits a transaction to database asynchronously. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <returns></returns>
        public async Task <int> CommitAsync(SqlConnection connection)
        {
            int affectedRecords = 0;

            if (!_list.Any())
            {
                return(affectedRecords);
            }

            base.IndexCheck();
            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_columns, _customColumnMappings, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(dt, _list, _columns, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync();
            }

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);

            SqlCommand command = connection.CreateCommand();

            command.Connection     = connection;
            command.CommandTimeout = _sqlTimeout;

            //Creating temp table on database
            command.CommandText = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);
            await command.ExecuteNonQueryAsync();

            BulkOperationsHelper.InsertToTmpTable(connection, dt, _bulkCopyEnableStreaming,
                                                  _bulkCopyBatchSize, _bulkCopyNotifyAfter, _bulkCopyTimeout, _sqlBulkCopyOptions, _bulkCopyDelegates);

            if (_disableIndexList != null && _disableIndexList.Any())
            {
                command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Disable, _tableName,
                                                                                 _schema, connection, _disableIndexList, _disableAllIndexes);
                await command.ExecuteNonQueryAsync();
            }

            string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                       OperationType.InsertOrUpdate, _identityColumn);

            if (!string.IsNullOrWhiteSpace(comm))
            {
                command.CommandText = comm;
                await command.ExecuteNonQueryAsync();
            }

            comm = "MERGE INTO " + BulkOperationsHelper.GetFullQualifyingTableName(connection.Database, _schema, _tableName) + " WITH (HOLDLOCK) AS Target " +
                   "USING " + Constants.TempTableName + " AS Source " +
                   BulkOperationsHelper.BuildJoinConditionsForInsertOrUpdate(_matchTargetOn.ToArray(),
                                                                             Constants.SourceAlias, Constants.TargetAlias, base._collationColumnDic) +
                   "WHEN MATCHED " + BulkOperationsHelper.BuildPredicateQuery(_matchTargetOn.ToArray(), _deletePredicates, Constants.TargetAlias, base._collationColumnDic) +
                   "THEN DELETE " +
                   BulkOperationsHelper.GetOutputIdentityCmd(_identityColumn, _outputIdentity, Constants.TempOutputTableName,
                                                             OperationType.Delete) + "; " +
                   "DROP TABLE " + Constants.TempTableName + ";";
            command.CommandText = comm;

            if (_parameters.Count > 0)
            {
                command.Parameters.AddRange(_parameters.ToArray());
            }

            affectedRecords = command.ExecuteNonQuery();

            if (_disableIndexList != null && _disableIndexList.Any())
            {
                command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Rebuild, _tableName,
                                                                                 _schema, connection, _disableIndexList);
                await command.ExecuteNonQueryAsync();
            }

            if (_outputIdentity == ColumnDirectionType.InputOutput)
            {
                BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic, OperationType.Delete, _list);
            }

            return(affectedRecords);
        }
Пример #10
0
        /// <summary>
        /// Commits a transaction to database. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public int Commit(SqlConnection connection, int commandTimeout = 0)
        {
            try
            {
                int affectedRows = 0;

                if (!_list.Any())
                {
                    return(affectedRows);
                }

                DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);
                dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic);

                // Must be after ToDataTable is called.
                BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns, _matchTargetOn);

                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }

                BulkOperationsHelper.ValidateMsSqlVersion(connection, OperationType.Insert);

                DataTable dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);

                string destinationTableName = BulkOperationsHelper.GetFullQualifyingTableName(connection.Database, _schema, _tableName);
                var    schemaDetail         = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);

                SqlCommand command = connection.CreateCommand();
                command.Connection     = connection;
                command.CommandTimeout = commandTimeout;

                if (_disableAllIndexes)
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Disable, _tableName,
                                                                                     _schema, connection);
                    command.ExecuteNonQuery();
                }

                // If InputOutput identity is selected, must use staging table.
                if (_outputIdentity == ColumnDirectionType.InputOutput && dtCols != null)
                {
                    command.CommandText = schemaDetail.BuildCreateTableQuery;
                    command.ExecuteNonQuery();

                    if (BulkOperationsHelper.GetBulkInsertStrategyType(dt, _columns) ==
                        BulkInsertStrategyType.MultiValueInsert)
                    {
                        var tempTableSetup = BulkOperationsHelper.BuildInsertQueryFromDataTable(_customColumnMappings, dt, _identityColumn,
                                                                                                _columns, _bulkCopySettings, schemaDetail, Constants.TempTableName, keepIdentity: true, keepInternalId: true);
                        command.CommandText = tempTableSetup.InsertQuery;
                        command.Parameters.AddRange(tempTableSetup.SqlParameterList.ToArray());
                        command.ExecuteNonQuery();
                        command.Parameters.Clear();
                    }
                    else
                    {
                        BulkOperationsHelper.InsertToTmpTableWithBulkCopy(connection, dt, _bulkCopySettings);
                    }

                    command.CommandText = BulkOperationsHelper.GetInsertIntoStagingTableCmd(command, connection, _schema,
                                                                                            _tableName,
                                                                                            _columns, _identityColumn, _outputIdentity);
                    command.ExecuteNonQuery();

                    BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic,
                                                                OperationType.Insert, _list);
                }

                else if (BulkOperationsHelper.GetBulkInsertStrategyType(dt, _columns) ==
                         BulkInsertStrategyType.MultiValueInsert)
                {
                    var tableSetup = BulkOperationsHelper.BuildInsertQueryFromDataTable(_customColumnMappings, dt, _identityColumn,
                                                                                        _columns, _bulkCopySettings, schemaDetail, destinationTableName);
                    command.CommandText  = GetSetIdentityCmd(on: true);
                    command.CommandText += tableSetup.InsertQuery;
                    command.CommandText += " " + GetSetIdentityCmd(on: false);
                    command.Parameters.AddRange(tableSetup.SqlParameterList.ToArray());
                    command.ExecuteNonQuery();
                    command.Parameters.Clear();
                }

                else
                {
                    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection, _bulkCopySettings.SqlBulkCopyOptions, null))
                    {
                        bulkcopy.DestinationTableName = destinationTableName;
                        BulkOperationsHelper.MapColumns(bulkcopy, _columns, _customColumnMappings);

                        BulkOperationsHelper.SetSqlBulkCopySettings(bulkcopy, _bulkCopySettings);
                        bulkcopy.WriteToServer(dt);

                        bulkcopy.Close();
                    }
                }

                if (_disableAllIndexes)
                {
                    command.CommandText = BulkOperationsHelper.GetIndexManagementCmd(Constants.Rebuild, _tableName,
                                                                                     _schema, connection);
                    command.ExecuteNonQuery();
                }



                affectedRows = dt.Rows.Count;
                return(affectedRows);
            }

            catch (SqlException e)
            {
                for (int i = 0; i < e.Errors.Count; i++)
                {
                    // Error 8102 is identity error.
                    if (e.Errors[i].Number == 544 || e.Errors[i].Number == 8102)
                    {
                        // Expensive but neccessary to inform user of an important configuration setup.
                        throw new IdentityException(e.Errors[i].Message);
                    }
                }

                throw;
            }
        }
Пример #11
0
        /// <summary>
        /// Commits a transaction to database. A valid setup must exist for the operation to be
        /// successful.
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        public int Commit(SqlConnection connection, int commandTimeout = 0)
        {
            int affectedRecords = 0;

            if (!_list.Any())
            {
                return(affectedRecords);
            }

            base.MatchTargetCheck();

            DataTable dt = BulkOperationsHelper.CreateDataTable <T>(_propertyInfoList, _columns, _customColumnMappings, _ordinalDic, _matchTargetOn, _outputIdentity);

            dt = BulkOperationsHelper.ConvertListToDataTable(_propertyInfoList, dt, _list, _columns, _ordinalDic, _outputIdentityDic);

            // Must be after ToDataTable is called.
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _columns);
            BulkOperationsHelper.DoColumnMappings(_customColumnMappings, _deletePredicates);

            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            BulkOperationsHelper.ValidateMsSqlVersion(connection, OperationType.Delete);

            var dtCols = BulkOperationsHelper.GetDatabaseSchema(connection, _schema, _tableName);

            SqlCommand command = connection.CreateCommand();

            command.Connection     = connection;
            command.CommandTimeout = commandTimeout;

            //Creating temp table on database
            var schemaDetail = BulkOperationsHelper.BuildCreateTempTable(_columns, dtCols, _outputIdentity);

            command.CommandText = schemaDetail.BuildCreateTableQuery;
            command.ExecuteNonQuery();

            _nullableColumnDic = schemaDetail.NullableDic;

            if (BulkOperationsHelper.GetBulkInsertStrategyType(dt, _columns) ==
                BulkInsertStrategyType.MultiValueInsert)
            {
                var tempTableSetup = BulkOperationsHelper.BuildInsertQueryFromDataTable(_customColumnMappings, dt, _identityColumn, _columns,
                                                                                        _bulkCopySettings, schemaDetail, Constants.TempTableName, keepIdentity: true, keepInternalId: true);
                command.CommandText = tempTableSetup.InsertQuery;
                command.Parameters.AddRange(tempTableSetup.SqlParameterList.ToArray());
                command.ExecuteNonQuery();
                command.Parameters.Clear();
            }
            else
            {
                BulkOperationsHelper.InsertToTmpTableWithBulkCopy(connection, dt, _bulkCopySettings);
            }

            string comm = BulkOperationsHelper.GetOutputCreateTableCmd(_outputIdentity, Constants.TempOutputTableName,
                                                                       OperationType.Delete, _identityColumn);

            if (!string.IsNullOrWhiteSpace(comm))
            {
                command.CommandText = comm;
                command.ExecuteNonQuery();
            }

            comm = GetCommand(connection);

            command.CommandText = comm;

            if (_parameters.Count > 0)
            {
                command.Parameters.AddRange(_parameters.ToArray());
            }

            affectedRecords = command.ExecuteNonQuery();

            if (_outputIdentity == ColumnDirectionType.InputOutput)
            {
                BulkOperationsHelper.LoadFromTmpOutputTable(command, _identityColumn, _outputIdentityDic, OperationType.Delete, _list);
            }

            return(affectedRecords);
        }