Execute方法描述:
Execute是Dapper对数据库操作的一个扩展,可以由IDbConnection对象调用。它可以执行一条命令一或多次,返回类型是受影响的行数。这个方法通常用于执行:
- Stored Procedure (存储过程)
- INSERT statement (插入语句)
- UPDATE statement (更新语句)
- DELETE statement (删除语句)
该方法可传递的参数:
| 参数名 | 参数含义 |
| Sql | 可执行的数据库语句 |
| param | 命令中的占位参数 |
| transaction | 使用的事务 |
| commandTimeout | 超时时长 |
| commandType | 命令类型 |
示例:执行存储过程
执行一次存储过程:
string sql = \"Invoice_Insert\";
using (var connection = My.ConnectionFactory())
{
var affectedRows = connection.Execute(sql,
new {Kind = InvoiceKind.WebInvoice, Code = \"Single_Insert_1\"},
commandType: CommandType.StoredProcedure);
My.Result.Show(affectedRows);
}
执行多次存储过程:
string sql = \"Invoice_Insert\";
using (var connection = My.ConnectionFactory())
{
var affectedRows = connection.Execute(sql,
new[]
{
new {Kind = InvoiceKind.WebInvoice, Code = \"Many_Insert_1\"},
new {Kind = InvoiceKind.WebInvoice, Code = \"Many_Insert_2\"},
new {Kind = InvoiceKind.StoreInvoice, Code = \"Many_Insert_3\"}
},
commandType: CommandType.StoredProcedure
);
My.Result.Show(affectedRows);
}
示例:Execute执行插入语句
执行单条插入:
string sql = \"INSERT INTO Customers (CustomerName) Values (@CustomerName);\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
var affectedRows = connection.Execute(sql, new {CustomerName = \"Mark\"});
Console.WriteLine(affectedRows);
var customer = connection.Query<Customer>(\"Select * FROM CUSTOMERS WHERE CustomerName = \'Mark\'\").ToList();
FiddleHelper.WriteTable(customer);
}
执行多条插入:
string sql = \"INSERT INTO Customers (CustomerName) Values (@CustomerName);\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
connection.Open();
var affectedRows = connection.Execute(sql,
new[]
{
new {CustomerName = \"John\"},
new {CustomerName = \"Andy\"},
new {CustomerName = \"Allan\"}
}
);
Console.WriteLine(affectedRows);
示例:Execute执行更新语句
执行单条更新:
string sql = \"UPDATE Categories SET De ion = @De ion WHERE CategoryID = @CategoryID;\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
var affectedRows = connection.Execute(sql,new {CategoryID = 1, De ion = \"Soft drinks, coffees, teas, beers, mixed drinks, and ales\"});
Console.WriteLine(affectedRows);
}
执行多条更新:
string sql = \"UPDATE Categories SET De ion = @De ion WHERE CategoryID = @CategoryID;\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
var affectedRows = connection.Execute(sql,
new[]
{
new {CategoryID = 1, De ion = \"Soft drinks, coffees, teas, beers, mixed drinks, and ales\"},
new {CategoryID = 4, De ion = \"Cheeses and butters etc.\"}
}
);
Console.WriteLine(affectedRows);
示例:Execute执行删除操作
执行单条删除:
string sql = \"DELETE FROM Customers WHERE CustomerID = @CustomerID\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
var affectedRows = connection.Execute(sql, new {CustomerID = 1});
Console.WriteLine(affectedRows);
}
执行多条删除:
string sql = \"DELETE FROM OrderDetails WHERE OrderDetailID = @OrderDetailID\";
using (var connection = new SqlCeConnection(\"Data Source=SqlCe_W3Schools.sdf\"))
{
var affectedRows = connection.Execute(sql,
new[]
{
new {OrderDetailID = 1},
new {OrderDetailID = 2},
new {OrderDetailID = 3}
}
);
Console.WriteLine(affectedRows);
继续阅读与本文标签相同的文章
-
如何检测 Web 服务请求丢失问题
2026-05-18栏目: 教程
-
基于阿里云服务器ECS的建站过程
2026-05-18栏目: 教程
-
8分钟5个点让你彻底了解负载均衡
2026-05-18栏目: 教程
-
2019年9月16日,开发者社区奖励办法--8月评选结果正式公布
2026-05-18栏目: 教程
-
RocketMQ 主题扩分片后遇到的坑
2026-05-18栏目: 教程
