Home > Database > Mysql Tutorial > How Can I Efficiently Bulk Insert a DataTable into a SQL Database Using C#?

How Can I Efficiently Bulk Insert a DataTable into a SQL Database Using C#?

Patricia Arquette
Release: 2025-01-07 22:13:43
Original
485 people have browsed it

How Can I Efficiently Bulk Insert a DataTable into a SQL Database Using C#?

Optimizing Data Insertion with SqlBulkCopy in C#

For large datasets, inserting data row by row into a SQL Server database is inefficient. The SqlBulkCopy class in C# provides a significantly faster alternative for bulk insertion operations.

Leveraging SqlBulkCopy for Bulk Inserts

SqlBulkCopy is designed to transfer large volumes of data to a database table in a single, optimized transaction. Here's how to implement it:

<code class="language-csharp">using System.Data.SqlClient;

// Establish connection and create SqlBulkCopy object
using (var bulkCopy = new SqlBulkCopy(_connection.ConnectionString, SqlBulkCopyOptions.KeepIdentity))
{
    // Map DataTable columns to database columns
    foreach (DataColumn column in table.Columns)
    {
        bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
    }

    // Specify destination table
    bulkCopy.DestinationTableName = destinationTableName;

    // Set timeout (in seconds) to prevent timeouts on large datasets
    bulkCopy.BulkCopyTimeout = 600;

    // Perform the bulk copy operation
    bulkCopy.WriteToServer(table);
}</code>
Copy after login

Explanation:

  • _connection.ConnectionString: Your database connection string.
  • table: The DataTable containing the data to be inserted.
  • destinationTableName: The name of the target table in your SQL Server database.

Important Considerations:

  • Column Mapping: Ensure your DataTable column names precisely match the database column names. If they differ, adjust the ColumnMappings accordingly.
  • Identity Column: SqlBulkCopyOptions.KeepIdentity preserves identity column values from your DataTable.
  • Timeout: BulkCopyTimeout prevents operation timeouts, especially crucial for substantial datasets. Adjust this value as needed based on your data volume and network conditions.

This method dramatically improves database insertion speed compared to individual row insertions, significantly enhancing application performance when handling large datasets.

The above is the detailed content of How Can I Efficiently Bulk Insert a DataTable into a SQL Database Using C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template