Skip to content

How to export large SQL Server data table into a local CSV file without reading the entire table into memory and without external libraries?

License

Notifications You must be signed in to change notification settings

matjazbravc/Export-SqlDataTable-To-CompressedCsv-Demo

Repository files navigation

Export Sql DataTable to compressed CSV file Demo

How to export large SQL Server data table into a local CSV file without reading the entire table into memory and without external libraries? It is not so hard with using combination of DbCommand.ExecuteReader and StreamWriter.

For a reliable export from Azure SQL, I've also implemented ReliableSqlDmConnection and ReliableSqlDbCommand which uses Polly retry Policy for handling transient exceptions:

public SqlDbRetryPolicy()
{
    _sqlRetryPolicy = Policy
        .Handle<TimeoutException>()
        .Or<SqlException>(AnyRetryableError)
        .WaitAndRetry(RETRY_COUNT, ExponentialBackoff, (exception, attempt) =>
        {
            // Capture some info for logging/telemetry.
            Console.WriteLine($"Execute: Retry {attempt} due to {exception}.");
        });
}

After successful export, CSV file is compressed withing origin directory using GZipStream.

Prerequisites

Tags & Technologies

Enjoy!

Licence

Licenced under MIT. Contact me on LinkedIn

About

How to export large SQL Server data table into a local CSV file without reading the entire table into memory and without external libraries?

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages