Exemplo n.º 1
0
        public static void BulkCopy <T>(this Table <T> table, SqlBulkCopyContext context, IEnumerable <T> records)
            where T : TableRecord, new()
        {
            var db        = GetSqlServerDatabase(table);
            var startTime = DateTimeOffset.Now;

            db.BulkCopy(context, new TableRecordReader(records));
            var endTime = DateTimeOffset.Now;
            //fire event
        }
Exemplo n.º 2
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));
            }
        }
Exemplo n.º 3
0
 private void CatchException(MethodName methodName, String connectionString, SqlBulkCopyContext context, Exception exception)
 {
     var e = new CommandErrorEventArgs(methodName, connectionString, exception, context);
     SqlServerDatabase.OnCommandError(e);
     if (e.ThrowException == true)
     {
         var ex = CreateException(e);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="methodName"></param>
 /// <param name="connectionString"></param>
 /// <param name="context"></param>
 public CommandExecutedEventArgs(MethodName methodName, String connectionString, DateTimeOffset startTime, DateTimeOffset endTime, SqlBulkCopyContext context)
     : this(methodName, connectionString, startTime, endTime)
 {
     this.Context = context;
 }
Exemplo n.º 5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="methodName"></param>
 /// <param name="connectionString"></param>
 /// <param name="context"></param>
 /// <param name="exception"></param>
 public CommandErrorEventArgs(MethodName methodName, String connectionString, Exception exception, SqlBulkCopyContext context)
     : this(methodName, connectionString, exception)
 {
     this.Context = context;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="methodName"></param>
 /// <param name="connectionString"></param>
 /// <param name="context"></param>
 public CommandExecutingEventArgs(MethodName methodName, String connectionString, SqlBulkCopyContext context)
     : this(methodName, connectionString)
 {
     Cancel = false;
     Context = context;
 }