如何在mvc c#List中获取存储过程的类型值 类型公共函数[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我试图获取“@TotalRecordsCount”中的记录总数,这些记录将在应用我的所有搜索参数后返回。但它总是给我空值。我尝试了其他匹配选项并尝试修复它,但我的代码以不同的方式执行基于存储过程的查询。下面给出的代码是向写入访问数据库的代码发送请求

代码#1

 public List<T> GetAllUsedCarsSidSearchFiltered<T>(string vehicleTypeIDs, string makeIDs, string modelIDs, int yearsStarting, int yearsEnding,
        int MileageStarting, int MileageEnding, string transmissionIDs, string noOfDoors, string fuleTypes, string bodyTypesIDs, string carsColors
        , string pcpfilterValue, string hpfilterValue, int totalRecordsCount, string storedProcedureName)
    {
        try
        {
            using (var connection = DbConnection.GetOpenConnectionKapMotorsContextMultiSelect())
            {
                return connection.Query<T>(storedProcedureName, new
                {
                    vehicleTypeIDs,
                    makeIDs,
                    modelIDs,
                    yearsStarting,
                    yearsEnding,
                    MileageStarting,
                    MileageEnding,
                    transmissionIDs,
                    noOfDoors,
                    fuleTypes,
                    bodyTypesIDs,
                    carsColors,
                    pcpfilterValue,
                    hpfilterValue,
                    totalRecordsCount
                }, commandType: CommandType.StoredProcedure).ToList();
            }
        }
        catch (Exception exception)
        {
            throw exception;
        }
    }

下面给出的代码是向数据库发送请求。

代码#2

 public static SqlConnection GetOpenConnectionKapMotorsContextMultiSelect()
    {
        var connection = new SqlConnection(builder.ProviderConnectionString);

        SqlCommand cmd = new SqlCommand("SideSearchBar_GetUsedCarsBySearchFilters", connection);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter outputParam = cmd.Parameters.Add("@TotalRecordsCount", SqlDbType.Int);
        outputParam.Direction = ParameterDirection.Output;

        connection.Open();

        //cmd.ExecuteNonQuery();

        int TotalRecordsCount = Convert.ToInt32(cmd.Parameters["@TotalRecordsCount"].Value);
        return connection;
    }

当我从“执行过程”运行此存储过程时,它返回所需的结果和TotalRecordCount值。但不是在c#代码中。请指导我

c# sql-server model-view-controller sqlconnection sqlcommand
1个回答
0
投票

这可能是因为你根本没有执行查询,如你发布的代码中所示,这行评论为//cmd.ExecuteNonQuery();

© www.soinside.com 2019 - 2024. All rights reserved.