Dapper - Execute

Description

Execute is an extension method that can be called from any object of type IDbConnection. It can execute a command one or multiple times and return the number of affected rows. This method is usually used to execute:

Parameters

The following table shows the different parameters of an Execute method.

NameDescription
sqlThe command text to execute.
paramThe command parameters (default = null).
transactionThe transaction to use (default = null).
commandTimeoutThe command timeout (default = null)
commandTypeThe command type (default = null)

Example - Execute Stored Procedure

Single

Execute the Stored Procedure a single time.

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

Stored Procedure Single

Many

Execute the Stored Procedure multiple times. Once for every object in the array list.

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

Example - Execute INSERT

Single

Execute the INSERT Statement a single time.

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

Try it: .NET Core | .NET Framework

Many

Execute the INSERT Statement multiple times. Once for every object in the array list.

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

Try it: .NET Core | .NET Framework

Example - Execute UPDATE

Single

Execute the UPDATE Statement a single time.

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

Try it: .NET Core | .NET Framework

Many

Execute the UPDATE Statement multiple times. Once for every object in the array list.

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

Try it: .NET Core | .NET Framework

Example - Execute DELETE

Single

Execute the DELETE Statement a single time.

  1. string sql = "DELETE FROM Customers WHERE CustomerID = @CustomerID";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var affectedRows = connection.Execute(sql, new {CustomerID = 1});
  5. Console.WriteLine(affectedRows);
  6. }

Try it: .NET Core | .NET Framework

Many

Execute the DELETE Statement multiple times. Once for every object in the array list.

  1. string sql = "DELETE FROM OrderDetails WHERE OrderDetailID = @OrderDetailID";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var affectedRows = connection.Execute(sql,
  5. new[]
  6. {
  7. new {OrderDetailID = 1},
  8. new {OrderDetailID = 2},
  9. new {OrderDetailID = 3}
  10. }
  11. );
  12. Console.WriteLine(affectedRows);

Try it: .NET Core | .NET Framework