如何使用 Nettiers 从存储过程中获取标量值

发布于 2024-08-31 21:22:52 字数 699 浏览 11 评论 0原文

我有一个非常简单的存储过程,如下所示:

CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID
(
 @VisitorGUID AS UNIQUEIDENTIFIER
)

AS

DECLARE @VisitorID AS bigint

SELECT @VisitorID = VisitorID FROM dbo.Visitor WHERE VisitorGUID = @VisitorGUID

--Here's what I've tried 
RETURN @VisitorID 'Returns an IDataReader
SELECT @VisitorID 'Returns an IDataReader
--I've also set it up with a single output
--parameter, but that means I need to pass
--the long in by ref and that's hideous to me

我试图让 nettiers 生成具有此签名的方法:

public long VisitorService.GetVisitorIDByVisitorGUID(GUID visitorGUID);

基本上我希望 Nettiers 调用 ExecuteScalar 而不是 ExecuteReader。我做错了什么?

I have a really simple stored procedure that looks like this:

CREATE PROCEDURE _Visitor_GetVisitorIDByVisitorGUID
(
 @VisitorGUID AS UNIQUEIDENTIFIER
)

AS

DECLARE @VisitorID AS bigint

SELECT @VisitorID = VisitorID FROM dbo.Visitor WHERE VisitorGUID = @VisitorGUID

--Here's what I've tried 
RETURN @VisitorID 'Returns an IDataReader
SELECT @VisitorID 'Returns an IDataReader
--I've also set it up with a single output
--parameter, but that means I need to pass
--the long in by ref and that's hideous to me

I'm trying to get nettiers to generate a method with this signature:

public long VisitorService.GetVisitorIDByVisitorGUID(GUID visitorGUID);

Basically I want Nettiers to call ExecuteScalar instead of ExecuteReader. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-09-07 21:22:54

为什么不使用自定义数据访问功能通过 ExecuteScalar 调用存储过程? )

var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");

(http://nettiers.com/DataLayer.ashx#Read_Methods:_4 存储过程的主体应如下所示:

Select VisitorID 
FROM dbo.Visitor 
WHERE VisitorGUID = @VisitorGUID

Return

Declare @VisitorId bigint
Set @VisitorId = (
                Select VisitorId
                From dbo.Visitor
                Where VisitorGuid = @VisitorGUID
                )

Select @VisitorId

Return

Why not use the custom data access functionality to call your stored proc with ExecuteScalar? (http://nettiers.com/DataLayer.ashx#Read_Methods:_4)

var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");

The body of the stored proc should look like:

Select VisitorID 
FROM dbo.Visitor 
WHERE VisitorGUID = @VisitorGUID

Return

or

Declare @VisitorId bigint
Set @VisitorId = (
                Select VisitorId
                From dbo.Visitor
                Where VisitorGuid = @VisitorGUID
                )

Select @VisitorId

Return
蓝天 2024-09-07 21:22:54

对于那些想要以编程方式执行此操作的人,上面的示例将不起作用,因为无法使用该方法的任何覆盖来传递必要的参数值。

var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");

我发现实现此目的的唯一方法是将 DbCommand 作为单独的参数传递给 ExecuteScalar 方法:

int myReturnID = 0;
using (SqlCommand cmd = new SqlCommand()) {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_MyStoredProcedure";
    cmd.Parameters.Add(new SqlParameter("MyParameter", myParameter));
    myReturnID = (Int32)DataRepository.Provider.ExecuteScalar(cmd);
}

For those wanting to do this programatically, the above example will not work as there is no way to pass the necessary parameter value using any of that method's overrides.

var vistorId = (long)DataRepository.Provider.ExecuteScalar(CommandType.StoredProcedure, "_Visitor_GetVisitorIDByVisitorGUID");

The only way I've found to accomplish this is by passing a DbCommand as a lone argument to the ExecuteScalar method:

int myReturnID = 0;
using (SqlCommand cmd = new SqlCommand()) {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_MyStoredProcedure";
    cmd.Parameters.Add(new SqlParameter("MyParameter", myParameter));
    myReturnID = (Int32)DataRepository.Provider.ExecuteScalar(cmd);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文