Dapper - Stored Procedure

Description

Using Stored Procedure in Dapper is very easy, you simply need to specify the command type

Execute Single

Execute a Stored Procedure a single time.

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

Execute Many

Execute a Stored Procedure multiple times.

  1. var sql = "Invoice_Insert";
  2. using (var connection = My.ConnectionFactory())
  3. {
  4. connection.Open();
  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. My.Result.Show(affectedRows);
  15. }