Dapper官方教程翻译2:Dapper方法之Execute(转)

Dapper官方教程翻译2:Dapper方法之Execute

Execute方法描述:

 

Execute是Dapper对数据库操作的一个扩展,可以由IDbConnection对象调用。它可以执行一条命令一或多次,返回类型是受影响的行数。这个方法通常用于执行:

 

该方法可传递的参数:

 

Execute方法参数说明
参数名参数含义
Sql可执行的数据库语句
param命令中的占位参数
transaction使用的事务
commandTimeout超时时长
commandType命令类型

 

示例:执行存储过程

 

执行一次存储过程:

  1.  
    string sql = "Invoice_Insert";
  2.  
     
  3.  
    using (var connection = My.ConnectionFactory())
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql,
  6.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Single_Insert_1"},
  7.  
    commandType: CommandType.StoredProcedure);
  8.  
     
  9.  
    My.Result.Show(affectedRows);
  10.  
    }

执行多次存储过程:

  1.  
    string sql = "Invoice_Insert";
  2.  
     
  3.  
    using (var connection = My.ConnectionFactory())
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql,
  6.  
    new[]
  7.  
    {
  8.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_1"},
  9.  
    new {Kind = InvoiceKind.WebInvoice, Code = "Many_Insert_2"},
  10.  
    new {Kind = InvoiceKind.StoreInvoice, Code = "Many_Insert_3"}
  11.  
    },
  12.  
    commandType: CommandType.StoredProcedure
  13.  
    );
  14.  
     
  15.  
    My.Result.Show(affectedRows);
  16.  
    }

 

示例:Execute执行插入语句

 

执行单条插入:

  1.  
    string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql, new {CustomerName = "Mark"});
  6.  
     
  7.  
    Console.WriteLine(affectedRows);
  8.  
     
  9.  
    var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
  10.  
     
  11.  
    FiddleHelper.WriteTable(customer);
  12.  
    }

执行多条插入:

  1.  
    string sql = "INSERT INTO Customers (CustomerName) Values (@CustomerName);";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    connection.Open();
  6.  
     
  7.  
    var affectedRows = connection.Execute(sql,
  8.  
    new[]
  9.  
    {
  10.  
    new {CustomerName = "John"},
  11.  
    new {CustomerName = "Andy"},
  12.  
    new {CustomerName = "Allan"}
  13.  
    }
  14.  
    );
  15.  
     
  16.  
    Console.WriteLine(affectedRows);

 

示例:Execute执行更新语句

 

执行单条更新:

  1.  
    string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql,new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"});
  6.  
     
  7.  
    Console.WriteLine(affectedRows);
  8.  
    }

执行多条更新:

  1.  
    string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql,
  6.  
    new[]
  7.  
    {
  8.  
    new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"},
  9.  
    new {CategoryID = 4, Description = "Cheeses and butters etc."}
  10.  
    }
  11.  
    );
  12.  
     
  13.  
    Console.WriteLine(affectedRows);

 

示例:Execute执行删除操作

 

执行单条删除:

  1.  
    string sql = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql, new {CustomerID = 1});
  6.  
     
  7.  
    Console.WriteLine(affectedRows);
  8.  
    }

执行多条删除:

  1.  
    string sql = "DELETE FROM OrderDetails WHERE OrderDetailID = @OrderDetailID";
  2.  
     
  3.  
    using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
  4.  
    {
  5.  
    var affectedRows = connection.Execute(sql,
  6.  
    new[]
  7.  
    {
  8.  
    new {OrderDetailID = 1},
  9.  
    new {OrderDetailID = 2},
  10.  
    new {OrderDetailID = 3}
  11.  
    }
  12.  
    );
  13.  
     
  14.  
    Console.WriteLine(affectedRows);

 

转载于:https://www.cnblogs.com/LiZhongZhongY/p/10991641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值