Passing a DataTable to a SP with ServiceStack ORMLite
Asked Answered
T

3

5

I have to call a Stored Procedure but passing a datatable (an iEnumerable) as parameter.

My SP on the SQL server takes this parameter as:

@LIST_USERS dbo.LIST_USERINFO_TYPE READONLY

and the type is defined as:

CREATE TYPE [dbo].[LIST_USERINFO_TYPE] AS TABLE(
   [ID_USER] [int] NOT NULL,
   [ID_DATA] [int] NOT NULL,
   [HEADER_TXT] [varchar](100) NULL)

Then, on the caller side i create the parameters in this way:

list.Add(new UserInfoItem { IdUser = 401, IdData = 3, HeaderTxt = "" });
list.Add(new UserInfoItem { IdUser= 402, IdData= 2, HeaderTxt= "gotcha" });
list.Add(new UserInfoItem { IdUser= 403, IdData= 1, HeaderTxt= "pacific rim" });

dbConn.StoredProcedure(sp,
  new
    {
      LISTA_QUESTIONARIO = DomandeRisposteList
    });

When i launch the project actually it stops with a KeyNotFoundException trying to

name.DbType = OrmLiteConfig.DialectProvider.GetColumnDbType(propertyInfo.PropertyType);

in ServiceStackExtension.cs

How can I achieve this?

Turpin answered 13/11, 2013 at 12:2 Comment(0)
T
7

I've found the answer on my own using simple Datatables:

        DataTable dataTableTmp = new DataTable();
        dataTableTmp.Columns.Add("ID_USER", typeof(Int32));
        dataTableTmp.Columns.Add("ID_DATA", typeof(Int32));
        dataTableTmp.Columns.Add("HEADER_TXT", typeof(string));


        foreach (var r in DomandeRisposteList)
        {
            DataRow ro = dataTableTmp.NewRow();
            ro[0] = r.IdUser;
            ro[1] = r.IdData ;
            ro[2] = r.HeaderTxt ;

            dataTableTmp.Rows.Add(ro);
        }

        var dbConn = dbFactory.OpenDbConnection();

        var res = dbConn.Exec(dbCmd =>
        {
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.Add(new SqlParameter("@LISTA_QUESTIONARIO", dataTableTmp));
            dbCmd.CommandText = "IF_SP_QUESTIONARIO_INSERT_TEST";
            return dbCmd.ExecuteReader().ConvertToList<DomandeRisposteItem>(); 
        });

        return res;
Turpin answered 18/11, 2013 at 9:36 Comment(0)
S
2

Building on Pizzaboys answer... looks like you can't use ConvertToList after ExecuteReader() with a newer version of servicestack

I had to do this:

var res = dbConn.Exec(dbCmd =>
{
    dbCmd.CommandType = CommandType.StoredProcedure;
    dbCmd.Parameters.Add(new SqlParameter("@LISTA_QUESTIONARIO", dataTableTmp));
    dbCmd.CommandText = "IF_SP_QUESTIONARIO_INSERT_TEST";
    rtnList = dbCmd.ConvertToList<DomandeRisposteItem>(); 
});
Sisely answered 14/4, 2016 at 15:21 Comment(0)
A
1

Based on PizzaBoy answer of first creating a DataTable and populating. For my purpose (with no returned records) the following worked with v4 of ServiceStack.

        //var Db = DbFactory.OpenDbConnection(); // I already have Db as IDbConnection from 'RepositoryBase'
        using (var dbCmd = Db.OpenCommand())
        {
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.CommandText = "dbo.StoredProcName";
            dbCmd.Parameters.Add(new SqlParameter("@tvpName", dataTableTmp));
            dbCmd.ExecuteNonQuery();
        }
Amendatory answered 31/8, 2016 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.