简体   繁体   English

C#和SQL Server存储过程-必须声明标量变量@SellerLocationID

[英]C# & SQL Server stored procedure - Must Declare The Scalar Variable @SellerLocationID

My first attempt at creating a stored procedure to be run from C# has produced the following result. 我第一次创建要从C#运行的存储过程的尝试产生了以下结果。

However, on invocation, I get the error 但是,在调用时,出现错误

Must declare scalar @SellerLocationID 必须声明标量@SellerLocationID

I have tried including the statement 我尝试过包括以下声明

declare @SellerLocationID int = 0;

Both within and outside of the stored procedure. 在存储过程的内部和外部。

I receive the same error in either case. 无论哪种情况,我都会收到相同的错误。

What do I need to do to resolve this error? 我需要怎么做才能解决此错误?

C# (Invoking Code) C#(调用代码)

 var sqlUserName = new SqlParameter("@SellerLocationID", SellerLocationID);

var results = unitofwork.DBContext().Database
                                .SqlQuery<dynamic>
                                ("Exec GetClientOrdersForSupplier @SellerLocationID", SellerLocationID).ToList();

T-SQL: T-SQL:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetClientOrdersForSupplier]
    @SellerLocationID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        l.LocationName, u.UserID, u.FirstName, u.LastName, 
        MAX(o.OrderSubmittedDate) as LastOrderPlaced
    FROM
        sw.Locations l
    JOIN 
        sw.Users u ON u.locationid = l.locationid 
    LEFT JOIN
        sw.locationproviders lp ON u.LocationID = lp.BuyerLocationID 
    LEFT JOIN
        sw.orders o ON o.CreatedUserID = u.UserID
    WHERE
        lp.sellerlocationid = @SellerLocationID
    GROUP BY 
        l.LocationName, u.UserID, u.FirstName, u.LastName
    ORDER BY
        UserID
END

The call to .Query needs a SqlParameter object when you are specifying a named parameter: 指定命名参数时,对.Query的调用需要SqlParameter对象:

var sqlUserName = new SqlParameter("@SellerLocationID", SellerLocationID);

var results = unitofwork.DBContext().Database
                                .SqlQuery<dynamic>
                                ("Exec GetClientOrdersForSupplier @SellerLocationID", sqlUserName).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM