参数化查询'(@ QuotationNo varchar(),@ AgencyName varchar(),@ ContractS'需要参数'@QuotationNo',这是未提供的

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

我,正在使用具有存储库模式的实体框架试图访问存储过程

通用存储库模式

public virtual IEnumerable<T> ExecuteStoreProcFunction(string query, params object[] parameters)
        {
            return _entities.Database.SqlQuery<T>(query, parameters).ToList();
        }

用下面的命令

var reportFilterDb =
                _iGenericReportProcRepository.ExecuteStoreProcFunction("StoreProcReport @QuotationNo, @AgencyName, @ContractStartDate, @ContractEndDate, @contractTerm",
                    new SqlParameter("QuotationNo", SqlDbType.VarChar) { Value = filter.QuotationNo },
                    new SqlParameter("AgencyName", SqlDbType.VarChar) { Value = filter.AgencyName },
                    new SqlParameter("ContractStartDate", SqlDbType.DateTime) { Value = filter.FContractStartDate },
                    new SqlParameter("ContractEndDate", SqlDbType.DateTime) { Value = filter.FContractEndDate },
                    new SqlParameter("contractTerm", SqlDbType.Int) { Value = filter.Term }
                    ).ToList();

这是存储过程结构

CREATE PROCEDURE [dbo].[StoreProcReport]
@QuotationNo varchar(max) null,
@AgencyName varchar(max) null,
@ContractStartDate datetime null,
@ContractEndDate datetime null,
@contractTerm int null

AS
 SET NOCOUNT ON;  
 SELECT con.Contract_AgencyName, con.Contract_EndDate, con.Contract_Id as 'ContractId', con.Contract_QuoteNo, con.Contract_StartDate,
 ConSer.ContractService_Id, ConSer.Description,ConSer.MonthlyUnitPrice, ConSer.OrderEndDate, ConSer.OrderStartDate,ConSer.OrderTermMonths,
 ConSer.Quantity, ConSer.TotalPrice
  FROM Contract AS con join ContractService as ConSer on con.Contract_Id = ConSer.Contract_Id
    WHERE (con.Contract_QuoteNo = @QuotationNo OR @QuotationNo IS NULL)
    AND (con.Contract_AgencyName = @AgencyName OR @AgencyName IS NULL)
    AND (con.Contract_StartDate = @ContractStartDate OR @ContractStartDate IS NULL)
    AND (con.Contract_EndDate = @ContractEndDate OR @ContractEndDate IS NULL)
    AND (ConSer.OrderTermMonths = @contractTerm OR @contractTerm IS NULL)
GO

我,我无法执行得到错误的结果

参数化查询'(@ QuotationNo varchar(),@ AgencyName varchar(),@ ContractS'需要参数'@QuotationNo',这是未提供的

c# sql asp.net entity-framework
2个回答
4
投票

如果filter.QuotationNonull,则不会添加参数,这是一种可怕的API体验。请尝试:.Value = (object)filter.QutationNo ?? DBNull.Value(所有参数等)。或者,考虑像Dapper这样的工具,它会自动执行此类操作:

_connection.Execute("StoreProcReport", new { // or Query<T> etc
    filter.QuotationNo,
    filter.AgencyName,
    ContractStartDate = filter.FContractStartDate,
    ContractEndDate = filter.FContractEndDate,
    contractTerm = filter.Term,
}, commandType: CommandType.StoredProcedure);

0
投票

这对我有用

var reportFilterDb =
                _iGenericReportProcRepository.ExecuteStoreProcFunction("EXEC StoreProcReport @QuotationNo, @AgencyName, @ContractStartDate, @ContractEndDate, @contractTerm",
                    new SqlParameter("@QuotationNo", SqlDbType.VarChar) { Value = (object)filter.QuotationNo ?? DBNull.Value },
                    new SqlParameter("@AgencyName", SqlDbType.VarChar) { Value = (object)filter.AgencyName ?? DBNull.Value },
                    new SqlParameter("@ContractStartDate", SqlDbType.DateTime) { Value = (object)filter.FContractStartDate ?? DBNull.Value },
                    new SqlParameter("@ContractEndDate", SqlDbType.DateTime) { Value = (object)filter.FContractEndDate ?? DBNull.Value  },
                    new SqlParameter("@contractTerm", SqlDbType.Int) { Value = (object)filter.Term ?? DBNull.Value  }
                    ).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.