5

我必须调用存储过程,但将数据表(iEnumerable)作为参数传递。

我在 SQL 服务器上的 SP 将此参数设为:

@LIST_USERS dbo.LIST_USERINFO_TYPE READONLY

类型定义为:

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

然后,在调用方我以这​​种方式创建参数:

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
    });

当我启动项目时,它实际上停止了KeyNotFoundException尝试

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

ServiceStackExtension.cs

我怎样才能做到这一点?

4

3 回答 3

7

我使用简单的数据表自己找到了答案:

        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;
于 2013-11-18T09:36:52.160 回答
2

建立在 Pizzaboys 的答案...看起来你不能在 ExecuteReader() 之后使用 ConvertToList 和更新版本的 servicestack

我不得不这样做:

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>(); 
});
于 2016-04-14T15:21:35.647 回答
1

基于首先创建 DataTable 并填充的 PizzaBoy 答案。出于我的目的(没有返回记录),以下内容与 ServiceStack 的 v4 一起使用。

        //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();
        }
于 2016-08-31T02:08:40.467 回答