I need to call a MySQL stored procedure. Can I do that with LINQ2DB or do I need to use BLToolkit for this? Any examples on how to do this? Thanks.
See DataConnection.QueryProc
MySQL T4 could generate them, but I have no idea how to get sproc metadata for MySQL.
I got it working:
using (var db = new Db())
{
var query = db.QueryProc<TheClassThatHoldsTheValues>("the_name_of_my_stored_procedure",
new DataParameter("param1", value1),
new DataParameter("param2", value2),
etc...
);
return query.ToList();
}
And in my BO class (TheClassThatHoldsTheValues in the example above) I was able to use [Column(Name = "actual_column_name_returned_by_stored_procedure")] which is pretty nice.
Thanks for your help.
BTW, if you guys need, I made an extension to write this with "less code"
using System.Collections.Generic;
using System.Linq;
namespace LinqToDB.Data
{
static class DataConnectionExtension
{
public static List<T> QueryProc<T>(this DataConnection db, string proc, object values)
{
List<DataParameter> parameters = new List<DataParameter>();
foreach (var prop in values.GetType().GetProperties())
{
var name = prop.Name;
var value = prop.GetValue(values);
parameters.Add(new DataParameter(name, value));
}
var result = db.QueryProc<T>(proc, parameters.ToArray());
return result.ToList();
}
}
}
So you can call this way:
var query = db.QueryProc<YourReturn>("your_proc_name", new
{
my1StParameterName = "my value 1",
otherNameYouWant = 123123
});
i think maybe we should generate a expression tree and compile it for each anonymous type and cache it in a dictionary, so reflection is only needed once. Or maybe we create a second function "queryprocwithcache"
Most helpful comment
I got it working:
And in my BO class (TheClassThatHoldsTheValues in the example above) I was able to use [Column(Name = "actual_column_name_returned_by_stored_procedure")] which is pretty nice.
Thanks for your help.