Monday, February 3, 2025
0 comments

Table-Valued Parameters (TVP) vs. SQLBulkCopy for Uploading Large CSV Files in C# and SQL Server

4:31 PM


 


Uploading large CSV files efficiently in SQL Server is a common requirement in enterprise applications. Two widely used approaches in C# are:

  1. Table-Valued Parameters (TVP)

  2. SQLBulkCopy

In this blog, we will compare both approaches, including benefits, limitations, best practices, real-world business cases, and final thoughts.

1. Understanding TVP and SQLBulkCopy

Table-Valued Parameters (TVP)

Table-Valued Parameters allow passing structured data (like a table) to a SQL Server stored procedure or function. It is useful for sending bulk data as a parameter instead of executing multiple insert statements.

Example Code for TVP Implementation

Step 1: Create a Table Type in SQL Server

CREATE TYPE CsvDataType AS TABLE
(
    ID INT,
    Name NVARCHAR(100),
    Age INT,
    Email NVARCHAR(255),
    PhoneNumber NVARCHAR(20),
    Address NVARCHAR(255),
    City NVARCHAR(100),
    State NVARCHAR(100),
    ZipCode NVARCHAR(20),
    Country NVARCHAR(100),
    CreatedDate DATETIME,
    IsActive BIT
);

Step 2: Create a Stored Procedure to Accept TVP

CREATE PROCEDURE InsertCsvData @CsvData CsvDataType READONLY
AS
BEGIN
    INSERT INTO CsvData (ID, Name, Age, Email, PhoneNumber, Address, City, State, ZipCode, Country, CreatedDate, IsActive)
    SELECT * FROM @CsvData;
END

Step 3: C# Code to Use TVP

public static void InsertCsvUsingTVP(DataTable csvTable)
{
    using (SqlConnection conn = new SqlConnection("YourConnectionString"))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("InsertCsvData", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter tvpParam = cmd.Parameters.AddWithValue("@CsvData", csvTable);
            tvpParam.SqlDbType = SqlDbType.Structured;
            cmd.ExecuteNonQuery();
        }
    }
}

SQLBulkCopy

SQLBulkCopy is a high-performance technique for inserting large volumes of data into SQL Server quickly. It leverages bulk insert operations to reduce the overhead of individual insert statements.

Example Code for SQLBulkCopy Implementation

public static void InsertCsvUsingSQLBulkCopy(DataTable csvTable)
{
    using (SqlConnection conn = new SqlConnection("YourConnectionString"))
    {
        conn.Open();
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
        {
            bulkCopy.DestinationTableName = "CsvData";
            bulkCopy.WriteToServer(csvTable);
        }
    }
}

2. Comparative Benefits and Limitations

Table-Valued Parameters (TVP)

FeatureBenefitsLimitations
PerformanceEfficient for medium-sized datasetsSlower than SQLBulkCopy for large datasets
Transaction ControlCan be wrapped in transactionsSome overhead due to parameter passing
FlexibilityAllows complex logic in stored proceduresNot optimized for raw bulk inserts
IndexingSupports indexes in the target tableNo direct indexing in TVP itself
Error HandlingMore granular control with stored proceduresMore processing needed in stored procedures

SQLBulkCopy

FeatureBenefitsLimitations
PerformanceBest for handling millions of recordsCannot perform transformations on the fly
Transaction ControlSupports batch commitsNo automatic rollback on failure
FlexibilityFastest for bulk inserts without transformationsLimited error handling
IndexingWorks well with indexed tablesRequires disabling constraints/indexes for maximum performance
Error HandlingCan be combined with try-catchHarder to debug issues due to lack of row-level control

3. Best Practices and Standards

For TVP:

  1. Use with small-to-medium-sized datasets (up to 100,000 records).

  2. Ensure table types match column definitions in SQL Server.

  3. Optimize stored procedures for efficient processing.

  4. Index tables properly to speed up insert operations.

  5. Avoid excessive logging to minimize performance impact.

For SQLBulkCopy:

  1. Use for high-volume inserts (millions of rows).

  2. Disable constraints and indexes before bulk insert, then re-enable them.

  3. Use batching (e.g., 10,000 rows per batch) to improve performance.

  4. Map column names correctly to avoid schema mismatch errors.

  5. Implement try-catch blocks to handle failures gracefully.


4. Real-World Business Cases

Case 1: Customer Data Import for CRM System

  • A CRM system requires batch importing customer records from CSV files.

  • TVP is used when new customers are inserted and updated in the same operation.

  • SQLBulkCopy is used for raw bulk insertion of new customer records.

Case 2: Financial Transactions Processing

  • A banking application needs to insert 5 million transactions daily.

  • SQLBulkCopy is chosen because of its high-speed insert capabilities.

  • TVP is used for inserting smaller transaction batches that require validation.

Case 3: Product Catalog Updates in E-Commerce

  • An e-commerce platform updates product inventory every hour.

  • TVP is used for inserting data along with price validation and stock updates.

  • SQLBulkCopy is used when only raw inventory data needs to be loaded quickly.


5. Final Thoughts

Both TVP and SQLBulkCopy have their own use cases:

  • Use TVP when you need flexibility, validation, and transactional control.

  • Use SQLBulkCopy when performance is the top priority and raw bulk insertion is needed.

For most applications, a combination of both approaches can be used strategically to balance speed, accuracy, and maintainability.


Which one should you choose?

  • If your operation involves transformations, validation, and moderate data volumes → Choose TVP.

  • If you need to push large amounts of data quickly with minimal processing → Choose SQLBulkCopy.

By understanding these trade-offs, you can optimize your data import strategies effectively.



0 comments:

 
Toggle Footer