Beispiel #1
0
        void IWriter.WriteToServer(string connectionString, string tableName, DbDataReader reader, List <ColumnMapping> mappingColumNames)
        {
            bool isFinished = false;

            using (var destinationConn = new SqlConnection(connectionString))
            {
                destinationConn.Open();

                using (var bulkCopy = new SqlBulkCopy(destinationConn, SqlBulkCopyOptions.TableLock, null))
                {
                    _mapper.AddColumnMapping(bulkCopy, mappingColumNames);

                    _customDataReader.DataReader     = reader;
                    _customDataReader.ColumnMappings = mappingColumNames;
                    _customDataReader.BatchSize      = 500;
                    _customDataReader.CreateCacheTable();
                    bulkCopy.DestinationTableName = tableName;
                    bulkCopy.BulkCopyTimeout      = 0;
                    while (!isFinished)
                    {
                        try
                        {
                            bulkCopy.WriteToServer(_customDataReader);
                            isFinished = true;
                        }
                        catch (Exception exp)
                        {
                            var dtCacheData = _customDataReader.GetCachedData();
                            _errorHandler.AddRow(bulkCopy, dtCacheData, 200);
                            _customDataReader.ClearCacheTable();
                        }
                    }
                }
                destinationConn.Close();
            }
        }