I am currently learning the DataAccess block for Enterprise Library 6.
I am trying to understand the how to return a series of objects using the ExecuteSprocAccessor
This is the code that I am using:
using (DbCommand cmd = sqlServerDB.GetStoredProcCommand("SalesByCategory"))
{
//Pass parameters
cmd.Parameters.Clear();
sqlServerDB.AddInParameter(cmd,"CategoryName",DbType.String,"Beverages");
sqlServerDB.AddInParameter(cmd, "OrdYear", DbType.String, "1996");
IRowMapper <OrderName> resmapper = MapBuilder<OrderName>.MapAllProperties()
.Map(x => x.Name).ToColumn("ProductName")
.Map(x => x.PurchaseTotal).ToColumn("TotalPurchase")
.Build();
List<OrderName> procaccessor = sqlServerDB.ExecuteSprocAccessor<OrderName>("SalesByCategory", resmapper, cmd.Parameters).ToList<OrderName>();
}
When it runs I get a "The number of parameters does not match number of values for stored procedure." error. When I check the cmd.Parameters collection I do see the 2 parameters stated above.
I am pretty sure that the error is on the cmd.Parameters in the last instruction. According to the documentation I can use a object array. How can I use the DBParameter collection?
== Update 3-29-2014 ==
After looking for an answer for a while I just decided to try and create a method that would translate the DBParameter into an object array. I created this....
public static object[] ParametertoObj(DbCommand cmd)
{
object[] obj = new object[cmd.Parameters.Count];
int i = 0;
foreach (DbParameter item in cmd.Parameters)
{
obj[i] = item.Value;
i++;
}
return obj;
}
The important thing with this method is that the parameters have to be added to the collection in the same order as they are called in the stored procedure.
I am pretty sure that there must be an efficient way to do this.