Dapper official translation Tutorial 2: Execute Dapper method of (turn)

Dapper official translation Tutorial 2: Execute Dapper method of

Execute methods are described:

 

Execute Dapper is an extension of the operation of the database, it can be called by IDbConnection object. It can execute a command one or more times, the return type is the number of rows affected. This method is typically used to perform:

 

The method may pass parameters:

 

Execute method Parameter Description
parameter name Parameter Meaning
Sql Executable database statements
param Placeholder parameters command
transaction Office work
commandTimeout Timeout period
commandType Command Type

 

Example: execute stored procedure

 

Execute a stored procedure:

  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.  
    }

Perform multiple stored procedure:

  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.  
    }

 

Example: Execute execute insert statement

 

Execute a single insert:

  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.  
    }

Execute multiple Insert:

  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);

 

Example: Execute the update statement

 

Execute a single update:

  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.  
    }

Executing multiple update:

  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);

 

Example: Execute a delete operation

 

Execute a single deleted:

  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.  
    }

Execute multiple deleted:

  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);

 

Guess you like

Origin www.cnblogs.com/LiZhongZhongY/p/10991641.html