Пример #1
0
 private void OnError(DBTableWriterErrorEventArgs e)
 {
     if (this.ErrorOccured != null)
     {
         this.ErrorOccured(this, e);
     }
 }
Пример #2
0
        public long Write(IDataReader reader, int?timeoutMilliseconds, int batchSize)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var columnNames = GetDataReaderColumnNames(reader);
            var statement   = GenerateStatement(_tableName, columnNames);
            var timeout     = timeoutMilliseconds.HasValue ? timeoutMilliseconds.Value : 0;

            var recordIndex = 0L;
            var recordCount = 0L;

            using (var conn = _connection)
            {
                conn.Open();

                var           read = false;
                DbTransaction tran = null;

                try
                {
                    read = reader.Read();
                }
                catch (Exception exc)
                {
                    var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.ReadError, recordIndex, exc);
                    OnError(e);

                    if (!e.TrySkipError)
                    {
                        return(recordCount);
                    }
                }

                while (read)
                {
                    var batchRecordIndex = 0;

                    if (batchSize > 1)
                    {
                        tran = conn.BeginTransaction();
                    }

                    var command = _connection.CreateCommand();
                    command.Transaction    = tran;
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = statement;
                    command.CommandTimeout = timeout;

                    while (read)
                    {
                        try
                        {
                            recordIndex++;

                            AddParameters(command, reader);
                            command.ExecuteNonQuery();

                            recordCount++;
                            batchRecordIndex++;
                        }
                        catch (Exception exc)
                        {
                            var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.WriteError, recordIndex, exc);
                            OnError(e);

                            if (!e.TrySkipError)
                            {
                                if (tran != null)
                                {
                                    tran.Rollback();
                                    tran.Dispose();
                                }

                                return(recordCount);
                            }
                        }

                        try
                        {
                            read = reader.Read();
                        }
                        catch (Exception exc)
                        {
                            var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.ReadError, recordIndex, exc);
                            OnError(e);

                            if (!e.TrySkipError)
                            {
                                if (tran != null)
                                {
                                    tran.Rollback();
                                    tran.Dispose();
                                }

                                return(recordCount);
                            }
                        }

                        if (batchRecordIndex >= batchSize)
                        {
                            batchRecordIndex = 0;
                            break;
                        }
                    }

                    if (batchSize > 1)
                    {
                        tran.Commit();
                    }
                }
            }

            return(recordCount);
        }
        public long Write(IDataReader reader, int? timeoutMilliseconds, int batchSize)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var columnNames = GetDataReaderColumnNames(reader);
            var statement = GenerateStatement(_tableName, columnNames);
            var timeout = timeoutMilliseconds.HasValue ? timeoutMilliseconds.Value : 0;

            var recordIndex = 0L;
            var recordCount = 0L;

            using (var conn = _connection)
            {
                conn.Open();

                var read = false;
                DbTransaction tran = null;

                try
                {
                    read = reader.Read();
                }
                catch (Exception exc)
                {
                    var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.ReadError, recordIndex, exc);
                    OnError(e);

                    if (!e.TrySkipError)
                    {
                        return recordCount;
                    }
                }

                while (read)
                {
                    var batchRecordIndex = 0;

                    if (batchSize > 1)
                    {
                        tran = conn.BeginTransaction();
                    }

                    var command = _connection.CreateCommand();
                    command.Transaction = tran;
                    command.CommandType = CommandType.Text;
                    command.CommandText = statement;
                    command.CommandTimeout = timeout;

                    while (read)
                    {
                        try
                        {
                            recordIndex++;

                            AddParameters(command, reader);
                            command.ExecuteNonQuery();

                            recordCount++;
                            batchRecordIndex++;
                        }
                        catch (Exception exc)
                        {
                            var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.WriteError, recordIndex, exc);
                            OnError(e);

                            if (!e.TrySkipError)
                            {
                                if (tran != null)
                                {
                                    tran.Rollback();
                                    tran.Dispose();
                                }

                                return recordCount;
                            }
                        }

                        try
                        {
                            read = reader.Read();
                        }
                        catch (Exception exc)
                        {
                            var e = new DBTableWriterErrorEventArgs(DBTableWriterErrorFlags.ReadError, recordIndex, exc);
                            OnError(e);

                            if (!e.TrySkipError)
                            {
                                if (tran != null)
                                {
                                    tran.Rollback();
                                    tran.Dispose();
                                }

                                return recordCount;
                            }
                        }

                        if (batchRecordIndex >= batchSize)
                        {
                            batchRecordIndex = 0;
                            break;
                        }
                    }

                    if (batchSize > 1)
                    {
                        tran.Commit();
                    }
                }
            }

            return recordCount;
        }
 private void OnError(DBTableWriterErrorEventArgs e)
 {
     if (this.ErrorOccured != null)
     {
         this.ErrorOccured(this, e);
     }
 }