Insert many rows with Dapper

CSharpBeginner

what's the best solution to insert many rows into the SQL Server with Dapper? When I have got about 8k records my databaseconnection timeouts and fails.

In my application at the end stage I have got lets say a list of tables, where each table got list of rows.

What i do is creating transaction and foreaching on each table and then foreaching on each row and conn.CreateCommand, filling parameters and executeNonQuery. So if I got 9k records I am actually doing 9k executeNonQuery operations.

Is there any better option for me?

JFM

Agree with DaniCE, SqlBulkCopy is the way to go here. Been in that situation lately where I did most of the data work with dapper but with larger amounts of data, in my case millions of records, Dapper was for once not my best friend.

    private void BulkCopy(SqlConnection sqlConnection,string tableName, DataTable dataTable)
    {
        using (var bulkCopy = new SqlBulkCopy(sqlConnection))
        {
            bulkCopy.DestinationTableName = tableName;
            bulkCopy.BatchSize = 50000;
            bulkCopy.BulkCopyTimeout = 60; //seconds

            bulkCopy.WriteToServer(dataTable);
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Insert many rows in Gnumeric

Fastest way to insert many rows

Can I Use Query Method in Dapper To Insert And Update Rows?

Dapper to insert multiple rows into two tables using stored procedure

Insert many rows based on JOIN with static list

Python pandas how many rows insert into DB

How insert to MySQL many rows PHP

Laravel how many rows inserted in an insert query?

Trigger to insert multiple rows based on many-to-many relationship

Entity Framework 6 Many-to-many wants to insert duplicate rows

Many to Many in Linq using Dapper

Simple INSERT with Dapper on Oracle

How many rows to insert per notification in a notification system?

How to use python mysqldb to insert many rows at once

How to use python mysqldb to insert many rows at once

Insert with execute many, skipping rows that fail foreign key constraints

Insert API response into many rows at once - psycopg2

Insert/Update/Index many rows (10 billion) numbers as values

C# Mysql Insert Many Rows, perfomance problem

Insert many rows from a table into one unique row in another table

PHP Form with 'Quantity' dropdown to insert that many rows of the data entered into form

Dapper One to Many Mapping Logic

Multiple results with many-to-many relationship dapper

Insert a few columns and rows from one dataframe into another dataframe with many more columns and rows

Dapper insert into database using list

Insert data for "Many to Many"

Spring crud repository - save() tries to insert new rows for many-to-one relationship in child property

How to (streaming-) insert many small rows (few bytes per row) into BigQuery cost-efficiently?

I want to find the difference between each row in a column and store that as a variable and then insert that many blank rows if the value is > 1