Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="reader"></param>
        public void BulkCopy(SqlBulkCopyContext context, IDataReader reader)
        {
            var            dr        = reader;
            var            state     = ConnectionState;
            DateTimeOffset?startTime = null;
            DateTimeOffset?endTime   = null;

            if (context.Connection == null)
            {
                if (this.ConnectionString != context.ConnectionString)
                {
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (this.Connection != context.Connection)
                {
                    throw new InvalidOperationException();
                }
            }
            if (context.Transaction != null && this.Transaction != context.Transaction)
            {
                throw new InvalidOperationException();
            }

            try
            {
                var e = SqlServerDatabase.OnCommandExecuting(new CommandExecutingEventArgs(MethodName.BulkCopy, ConnectionString, context));
                if (e != null && e.Cancel == true)
                {
                    return;
                }

                Open();
                var bc = context.SqlBulkCopy;
                startTime = DateTimeOffset.Now;
                bc.WriteToServer(dr);
                endTime = DateTimeOffset.Now;
                dr.Close();
            }
            catch (Exception exception)
            {
                this.CatchException(MethodName.BulkCopy, this.ConnectionString, context, exception);
            }
            finally
            {
                if (state == ConnectionState.Closed)
                {
                    this.Close();
                }
                ((IDisposable)context.SqlBulkCopy).Dispose();
            }
            if (startTime.HasValue == true && endTime.HasValue == true)
            {
                SqlServerDatabase.OnCommandExecuted(new CommandExecutedEventArgs(MethodName.BulkCopy, this.ConnectionString, startTime.Value, endTime.Value, context));
            }
        }