Passing query parameters in Dapper using OleDb

Endy Tjahjono picture Endy Tjahjono · Sep 17, 2013 · Viewed 8.9k times · Source

This query produces an error No value given for one or more required parameters:

using (var conn = new OleDbConnection("Provider=..."))
{
  conn.Open();
  var result = conn.Query(
    "select code, name from mytable where id = ? order by name",
    new { id = 1 });
}

If I change the query string to: ... where id = @id ..., I will get an error: Must declare the scalar variable "@id".

How do I construct the query string and how do I pass the parameter?

Answer

Marc Gravell picture Marc Gravell · Sep 23, 2014

The following should work:

var result = conn.Query(
"select code, name from mytable where id = ?id? order by name",
new { id = 1 });