SqlBulkCopy WriteToServerAsync

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Description

The asynchronous version of WriteToServer. Copy all rows from the source to the destination table. It accepts an array of DataRows or DataTable or IDataReader.

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();
    var sqlBulk = new SqlBulkCopy(connection);

    CancellationTokenSource cancelationTokenSource = new CancellationTokenSource();
    CancellationToken cancellationToken = cancelationTokenSource.Token;
    sqlBulk.DestinationTableName = "Customers";

    // Write from the source to the destination asynchronously.
    var task = sqlBulk.WriteToServerAsync(dt, cancellationToken);
}

Try it

WriteToServerAsync(DataRow[])

The asynchronous version of WriteToServer(DataRow[]), which copies all rows from the supplied DataRow array to a destination table asynchronously.

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();
    var sqlBulk = new SqlBulkCopy(connection);

    DataRow[] dataRows = dt.Select();
    sqlBulk.DestinationTableName = "Customers";

    // Write from the source to the destination asynchronously.
    Task.Run(async () => { await sqlBulk.WriteToServerAsync(dataRows); }).Wait();
    sqlBulk.Close();
}

Try it

WriteToServerAsync(DataTable)

The asynchronous version of WriteToServer(DataTable), which copies all rows in the supplied DataTable to a destination table asynchronously.

using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();
    var sqlBulk = new SqlBulkCopy(connection);

    sqlBulk.DestinationTableName = "Customers";
    
    // Write from the source to the destination asynchronously.
    Task.Run(async () => { await sqlBulk.WriteToServerAsync(dt); }).Wait();
    sqlBulk.Close();
}

Try it

WriteToServerAsync(IDataReader)

The asynchronous version of WriteToServer(IDataReader), which copies all rows in the supplied IDataReader to a destination table asynchronously.

using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
    connection.Open();

    // Get data from the source table as a SqlDataReader.
    SqlCommand commandSourceData = new SqlCommand(
        "SELECT * FROM Customers_Archive;", connection);

    SqlDataReader reader =
        commandSourceData.ExecuteReader();

    using (var dConnection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
    {
        dConnection.Open();
        using (var sqlBulk = new SqlBulkCopy(dConnection))
        {
            sqlBulk.DestinationTableName = "Customers";
            try
            {
                // Write from the source to the destination asynchronously.
                Task.Run(async () => { await sqlBulk.WriteToServerAsync(reader); }).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                sqlBulk.Close();
                reader.Close();
            }
        }
    }
}

Try it



Got any SqlBulkCopy Question?