从SqlQuery数据读取器问题执行存储过程

时间:2017-07-09 17:53:20

标签: c# sql-server

所以我试图执行我的存储过程,如下所示

            String sql =
        "SET NOCOUNT ON; " +
        "DECLARE @id INT, @itemnumber nvarchar(20); " +
        "SELECT @id= '" + car.ID + "'" +
        "EXEC [file].[usp_iudCar] " +
        "@p_ID=@id OUTPUT," +
        "@p_Location= '" + location + "', " +
        "@p_ItemNumber=@itemnumber OUTPUT, " +
        "@p_DoerTicket= '" + userToken + "' " +
        "SELECT @id AS id, @itemnumber AS itemNumber; ";

            QueryParamCollection queryParams = new QueryParamCollection();
            queryParams.AddInt32Param(QueryParamName.CarID, (object)car.ID);
            queryParams.AddStringParam(QueryParamName.Location, (object)location);
            queryParams.AddStringParam(QueryParamName.DoerTicket, (object)userToken);

            var update = context.Database.SqlQuery<Int32>(sql).FirstOrDefault<Int32>();

            return update;

类似于我从服务器上的查询中执行此操作

   DECLARE @return_value int,
            @p_ID int,
            @p_ItemNumber nvarchar(20)

    SELECT  @p_ID = 1783999

    EXEC    @return_value = [file].[usp_iudCar]
            @p_ID = @p_ID OUTPUT,
            @p_ItemNumber = @p_ItemNumber OUTPUT,
            @p_Location = N'test',
            @p_DoerTicket = N'0x0100000057065fc3a91f34c3f1f9cad41e2f5889bac6a68d3eab408dddc1cd54e57ce240565294f481f4f248bca4fb772d38fd737a6448dcbbfd9d58'

    SELECT  @p_ID as N'@p_ID',
            @p_ItemNumber as N'@p_ItemNumber'

    SELECT  'Return Value' = @return_value

    GO

结果集 enter image description here

但是我得到了一个execption

  

“ExceptionMessage”:“数据读取器有多个字段。多个字段对EDM原语或枚举类型无效。”,

所以我该如何解决这个问题?或者正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

此错误:

  

“ExceptionMessage”:“数据读取器有多个字段。多个字段对EDM原语或枚举类型无效。”,

表示您的存储过程返回的不仅仅是一个字段。这就是你告诉EF的事情:

    $(document).ready(function(){
    var arr = ["2","4","6","8","10"];
    var index =  ["1","5","9"];
    var arrIndex = index.map(function(value){
             return arr.indexOf(value);
    })

    console.log(arrIndex);
    })

查询返回单个字段,但显然不是这种情况,或者您不会收到该错误。

这也不是如何使用EF数据库第一种方法调用存储过程。请参阅this问题和接受的答案,了解如何以正确的方式做您想做的事情。