Dapper/StackExchange.Dapper是否支持单查询批量插入多行数据?
Great question! Let's break this down clearly: Yes, both Dapper and StackExchange.Dapper (the official, maintained version of Dapper) fully support inserting multiple rows in a single query—including the exact INSERT ... VALUES (...), (...), (...) syntax you mentioned. What's more, you can achieve this efficiently using a List (or any collection) without having to loop through individual inserts.
1. Directly Using Multi-Row VALUES Syntax
The SQL you provided works perfectly with Dapper. You can execute it directly just like any other query:
var insertSql = "INSERT INTO MyTable (a, b, c) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);"; connection.Execute(insertSql);
This runs as a single database query, which is far more efficient than multiple individual inserts, especially with larger datasets.
2. Parameterized Multi-Row Insert with a List
For dynamic data (like a list of objects you want to insert), Dapper's parameterization support makes this safe and straightforward. It automatically converts your collection into the multi-row VALUES syntax behind the scenes—no manual loops required.
First, define your entity class (if you haven't already):
public class MyTableRecord { public int A { get; set; } public int B { get; set; } public int C { get; set; } }
Then, create your list of records and execute the insert:
var recordsToInsert = new List<MyTableRecord> { new MyTableRecord { A = 1, B = 2, C = 3 }, new MyTableRecord { A = 4, B = 5, C = 6 }, new MyTableRecord { A = 7, B = 8, C = 9 } }; var parameterizedSql = "INSERT INTO MyTable (a, b, c) VALUES (@A, @B, @C)"; connection.Execute(parameterizedSql, recordsToInsert);
Dapper will translate this into a single multi-row insert query, not a loop of individual inserts. This gives you the performance benefit of a single query plus the security of parameterized SQL (avoiding SQL injection risks).
A Quick Note on Older Examples
You might have seen examples that use loops for inserts—these are usually either outdated (from before Dapper added robust collection support) or written by developers unaware of this feature. Modern Dapper handles this scenario seamlessly.
内容的提问来源于stack exchange,提问作者Joe Phillips

