使用 iBatis.NET 获取存储过程的返回值

发布于 2024-08-25 02:50:14 字数 748 浏览 3 评论 0原文

如何使用 iBatis.NET 检索存储过程的返回值?以下代码成功调用存储过程,但 QueryForObject调用返回 0。

SqlMap

<procedure id="MyProc" parameterMap="MyProcParameters" resultClass="int">
    MyProc
</procedure>

<parameterMap id="MyProcParameters">
    <parameter property="num"/>
</parameterMap>

C# 代码

public int RunMyProc( string num )
{
    return QueryForObject < int > ( "MyProc", new Hashtable { { "num", num } } );
}

存储过程

create procedure MyProc
    @num nvarchar(512)
as
begin
    return convert(int, @num)
end

仅供参考,我使用的是 iBatis 1.6.1.0、.NET 3.5 和 SQL Server 2008。

How can I retrieve the return value of a stored procedure using iBatis.NET? The below code successfully calls the stored procedure, but the QueryForObject<int> call returns 0.

SqlMap

<procedure id="MyProc" parameterMap="MyProcParameters" resultClass="int">
    MyProc
</procedure>

<parameterMap id="MyProcParameters">
    <parameter property="num"/>
</parameterMap>

C# code

public int RunMyProc( string num )
{
    return QueryForObject < int > ( "MyProc", new Hashtable { { "num", num } } );
}

Stored Procedure

create procedure MyProc
    @num nvarchar(512)
as
begin
    return convert(int, @num)
end

FYI, I'm using iBatis 1.6.1.0, .NET 3.5, and SQL Server 2008.

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

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

发布评论

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

评论(4

ㄟ。诗瑗 2024-09-01 02:50:15

存储过程没有像函数那样的返回值。
所以,我认为这不会起作用。尝试改用输出参数。

Stored procedures don't have a return value like functions.
So, I don't think that will work. Try using output parameters instead.

情魔剑神 2024-09-01 02:50:15

我不确定你的应用程序逻辑,但你的程序会更好,如下所示:

create procedure MyProc
    @num nvarchar(512)
as
begin
    DECLARE @ReturnValue int
    BEGIN TRY
        SET @ReturnValue=convert(int, @num)
    END TRY
    BEGIN CATCH
        SET @ReturnValue=0 --procedures can not return null, so set some default here
    END CATCH
    return @ReturnValue
end

I'm not sure about your application logic, but your procedure would be better like this:

create procedure MyProc
    @num nvarchar(512)
as
begin
    DECLARE @ReturnValue int
    BEGIN TRY
        SET @ReturnValue=convert(int, @num)
    END TRY
    BEGIN CATCH
        SET @ReturnValue=0 --procedures can not return null, so set some default here
    END CATCH
    return @ReturnValue
end
最佳男配角 2024-09-01 02:50:14

它并不漂亮,但可以工作:

SqlMap

<statement id="MyProc" parameterClass="string" resultClass="int">
    declare @num int
    exec @num = MyProc #value#
    select @num
</statement>

C# 代码

public int RunMyProc( string num )
{
    return QueryForObject < int > ( "MyProc", num );
}

It's not pretty, but this works:

SqlMap

<statement id="MyProc" parameterClass="string" resultClass="int">
    declare @num int
    exec @num = MyProc #value#
    select @num
</statement>

C# code

public int RunMyProc( string num )
{
    return QueryForObject < int > ( "MyProc", num );
}
扶醉桌前 2024-09-01 02:50:14

您可能想查看以下文章
http://www.barebonescoder.com/2010/ 04/ibatis-net-stored-procedures-return-values/ 了解如何检索返回值。

我在 QueryForObject 和 Insert 场景中使用了它,其中最后一个语句是存储过程中的 return 语句。

请特别注意“parameterMap”元素上的 class 属性。它比上面的答案漂亮得多,我相信它更符合 IBatis.Net 的使用方式。

You might want to check out the following article
http://www.barebonescoder.com/2010/04/ibatis-net-stored-procedures-return-values/ on how to retrieve return values.

I've used it in QueryForObject and Insert scenarios where the last statement is a return statement in the stored procedure.

Pay particular attention to the class attribute on the "parameterMap" element. It's a lot prettier than the answer above and I believe it's more inline with the way IBatis.Net was intended to be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文