I am using service stack ORMLIte for my DAL and so far its working great for basic CRUDS. However, I do have a special case where I have to call stored procedure with parameter type of Table variable. Now, I know that ADO.NET supports this with c# calling the DB with DataTable type. However, I do not know if ORMLite supports it since I did not see any samples in the documentation and if it does, are there any samples out there?
1 Answers
3
votes
Yes it does, here is a sample function that updated to work with TVPs (gets generated using the OrmLite T4 template initially then I modded), hope this helps. I am passing a HashSet in to use to build DataTable to pass as a TVP Parameter:
public static OrmLiteSPStatement CallMyStoredProcTVP(this IDbConnection db, HashSet<int> userids)
{
// need to create a datatable to pass as a parameter to your tvp SP
DataTable dataTableUserIds = new DataTable();
dataTableUserIds.Columns.Add("Value", typeof(Int32));
foreach (int id in userids)
{
DataRow ro = dataTableUserIds.NewRow();
ro[0] = id;
dataTableUserIds.Rows.Add(ro);
}
// create db command
var dbCmd = (DbCommand)OrmLiteConfig.ExecFilter.CreateCommand(db).ToDbCommand();
// set name of SP
dbCmd.CommandText = "MyStoredProcTVP";
dbCmd.CommandType = CommandType.StoredProcedure;
// add new parameter but I leave off the Type
dbCmd.Parameters.Add(CreateNewParameter(dbCmd, "UserIDs", dataTableUserIds, ParameterDirection.Input));
return new OrmLiteSPStatement(db, dbCmd);
}
private static DbParameter CreateNewParameter(DbCommand dbCommand, string paramName, object paramValue, ParameterDirection paramDirection)
{
DbParameter param = dbCommand.CreateParameter();
param.Direction = paramDirection;
param.ParameterName = paramName;
param.Value = paramValue;
return param;
}