Esempio n. 1
0
        private static void LoadTempTables(SqlConnection conn, IEnumerable <TempTableData> tempTableData)
        {
            foreach (var data in tempTableData)
            {
                using var cmd = conn.CreateCommand();
                // Note: If there is ever a need for letting users configure the command,
                // do not use the configureCmd parameter passed to methods on this class,
                // which also sets any parameters.
                cmd.CommandText = data.Definition;
                cmd.ExecuteNonQuery();

                using var bulkCopy = new SqlBulkCopy(conn)
                      {
                          DestinationTableName = data.DestinationTableName
                      };
                data.ConfigureBulkCopy(bulkCopy);
                var reader = new TempTableLoader(data.NumFields, data.Data);
                bulkCopy.WriteToServer(reader);
            }
        }
Esempio n. 2
0
        private static async Task LoadTempTablesAsync(SqlConnection conn, IEnumerable <TempTableData> tempTableData, SqlTransaction?tran, CancellationToken ct)
        {
            foreach (var data in tempTableData)
            {
                await using var cmd = conn.CreateCommand();
                if (tran != null)
                {
                    cmd.Transaction = tran;
                }
                // Note: If there is ever a need for letting users configure the command,
                // do not use the configureCmd parameter passed to methods on this class,
                // which also sets any parameters.
                cmd.CommandText = data.Definition;
                await cmd.ExecuteNonQueryAsync(ct);

                using var bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran)
                      {
                          DestinationTableName = data.DestinationTableName
                      };
                data.ConfigureBulkCopy(bulkCopy);
                var reader = new TempTableLoader(data.NumFields, data.Data);
                await bulkCopy.WriteToServerAsync(reader, ct);
            }
        }