private void FillTable(ISql sql, TablePrescription tablePrescription) { _Owner._Logger.LogInformation(Messages.LOG_INF_STARTED_1, nameof(FillTable), tablePrescription.TableDescription.Name); var columns = tablePrescription.ColumnPrescriptions.Select(_ => _.ColumnDescription.Name).ToArray(); var tableName = tablePrescription.TableName(); using var recordGenerator = _Owner._RecordGeneratorFactory.Create(tablePrescription, tablePrescription.Rows); using var connection = sql.CreateConnection(); connection.Open(); using var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, null) { BatchSize = BULKCOPY_BATCHSIZE, BulkCopyTimeout = 0, DestinationTableName = tableName, EnableStreaming = true, }; // skipping DF1, DF2. for (var i = 0; i < recordGenerator.FieldCount; i++) { _ = bulkCopy.ColumnMappings.Add(i, i + 2); } bulkCopy.WriteToServer(recordGenerator); _Owner._Logger.LogInformation(Messages.LOG_INF_FINISHED_1, nameof(FillTable), tablePrescription.TableDescription.Name); }
private static string CreateTableDefinition(TablePrescription tablePrescription) => new StringBuilder() .AppendFormatInvariant("CREATE TABLE {0} (", tablePrescription.TableName()) .AppendFormatInvariant("[@DF1] [BIGINT] IDENTITY(1, 1) NOT NULL PRIMARY KEY, ") .AppendFormatInvariant("[@DF2] [BIGINT] NULL, ") .AppendJoin(", ", tablePrescription .ColumnPrescriptions .Select(columnPrescription => columnPrescription.ColumnDescription) .Where(columnDescription => columnDescription.IsWritable()) .Select(columnDescription => columnDescription.SqlDefinition())) .AppendFormatInvariant(");") .ToString();