bulkinsertdapper-plus

Dapper-Plus BulkInsert - How to return number of rows affected?


In Dapper-Plus, is there a way to return the number of rows affected in the database? This is my code:

using (SqlConnection connection = new SqlConnection(Environment.GetEnvironmentVariable("sqldb_connection")))
{
    connection.BulkInsert(myList);
}

I see you can do it for inserting a single row, but can't find functionality on the dapper plus bulk insert.


Solution

  • Since Dapper Plus allow to chain multiple methods, the method doesn't directly return this value.

    However, you can do it with the following code:

    var resultInfo = new Z.BulkOperations.ResultInfo();
    
    connection.UseBulkOptions(options => {
        options.UseRowsAffected = true;
        options.ResultInfo = resultInfo;
    }).BulkInsert(orders);
    
    // Show RowsAffected
    Console.WriteLine("Rows Inserted: " + resultInfo.RowsAffectedInserted);
    Console.WriteLine("Rows Affected: " + resultInfo.RowsAffected);
    

    Fiddle: https://dotnetfiddle.net/mOMNng

    Keep in mind that using that option will slightly make the bulk operations slower.

    EDIT: Answer comment

    will it make it as slow as using the regular dapper insert method or is this way still faster?

    It will still be way faster than regular Insert.