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:
- Procedure the Stored (stored procedures)
- Of Statement INSERT (insert statement)
- Of Statement UPDATE (update statement)
- Of Statement DELETE (delete statement)
The method may pass parameters:
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:
-
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);
-
}
Perform multiple stored procedure:
-
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);
-
}
Example: Execute execute insert statement
Execute a single insert:
-
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);
-
}
Execute multiple Insert:
-
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);
Example: Execute the update statement
Execute a single update:
-
string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
-
-
using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
-
{
-
var affectedRows = connection.Execute(sql,new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"});
-
-
Console.WriteLine(affectedRows);
-
}
Executing multiple update:
-
string sql = "UPDATE Categories SET Description = @Description WHERE CategoryID = @CategoryID;";
-
-
using (var connection = new SqlCeConnection("Data Source=SqlCe_W3Schools.sdf"))
-
{
-
var affectedRows = connection.Execute(sql,
-
new[]
-
{
-
new {CategoryID = 1, Description = "Soft drinks, coffees, teas, beers, mixed drinks, and ales"},
-
new {CategoryID = 4, Description = "Cheeses and butters etc."}
-
}
-
);
-
-
Console.WriteLine(affectedRows);
Example: Execute a delete operation
Execute a single deleted:
-
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);
-
}
Execute multiple deleted:
-
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);