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

崔琦
2023-12-01

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

 类似资料: