Dapper - Parameter Anonymous

Description

Dapper make it simple & safe (SQL Injection) to use parameter by supporting anonymous type.

Single

Execute a single time a SQL Command.

  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. // Only for see the Insert.
  7. var customer = connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerName = 'Mark'").ToList();
  8. FiddleHelper.WriteTable(customer);
  9. }

Try it: .NET Core | .NET Framework

Many

Execute many times a SQL Command

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

Try it: .NET Core | .NET Framework