On every ADO.NET connector we have AddWithValue
and hence adding parameters was a breeze.
I am surprised that that overload is not available on IDataParameterCollection
or even on DbParameterCollection
. How can I achieve the same with minimal fuss (from both caller and callee side) when writing a vendor independent layer (coding against interface)?
Right now I do:
public void Execute(string query, Action<IDbCommand> parameterizer)
Execute(query, cmd =>
{
var param = cmd.CreateParameter();
param.ParameterName = "user_id";
param.Value = 1;
cmd.Parameters.Add(param);
param = cmd.CreateParameter();
param.ParameterName = "mf";
param.Value = "6CE71037";
cmd.Parameters.Add(param);
});
That's too much work isnt it. Otherwise I can change signature:
public void Execute(string query, IEnumerable<KeyValuePair<string, object>> parameters)
var parameters = new Dictionary<string, object>
{ { "user_id", 1 }, { "mf", "6CE71037" } };
Execute(query, parameters);
This approach requires I write another loop inside the callee (Execute
method). This is almost what I want but just seeing if there is a more concise approach. For instance like this:
public void Execute(string query, Action<IDataParameterCollection> parameterizer)
Execute(query, p =>
{
p["user_id"] = 1;
p["mf"] = "6CE71037";
});
This gives a runtime exception saying Unable to cast object of type 'System.Int32' to type 'System.Data.Common.DbParameter
. I understand the error. Just wondering if there is a more elegant pattern..