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?
Important: see newer answer
In the current build, the answer to that would be "no", for two reasons:
@id
,:id
or?id
in the sqlThe good news is that both of these are fixable
Making those changes to my local clone, the following now passes:
Note that I'm currently using
DynamicParameters
here to avoid adding even more overloads toQuery
/Query<T>
- because this would need to be added to a considerable number of methods. Adding it toDynamicParameters
solves it in one place.I'm open to feedback before I push this - does that look usable to you?
Edit: with the addition of a funky
smellsLikeOleDb
(no, not a joke), we can now do this even more directly:I've trialing use of Dapper within my software product which is using odbc connections (at the moment). However one day I intend to move away from odbc and use a different pattern for supporting different RDBMS products. However, my problem with solution implementation is 2 fold:
In the mean time I have hacked together a solution that allows me to do this with Dapper. Essentially I have a routine that replaces the named parameters with ? and also rebuilds the parameter object making sure the parameters are in the correct order. However looking at the Dapper code, I can see that I've repeated some of what dapper is doing anyway, effectively it each parameter value is now visited once more than what would be necessary. This becomes more of an issue for bulk updates/inserts. But at least it seems to work for me o.k...
I borrowed a bit of code from here to form part of my solution...
The current source code (not yet released to NuGet) addresses this issue; the following should work: