如何在C#中从sp和catch / handle返回输出参数? [英] How to return output parameter from sp and catch/handle in C#?

查看:79
本文介绍了如何在C#中从sp和catch / handle返回输出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql server 2012中创建了sp .Sp的输出类型为varchar(15)。



I have created sp in sql server 2012.Sp have output para of type varchar(15).

create procedure [dbo].[Proc_Authenticate]

    (
           @username int,   
           @password varchar(25),
           @Error varchar(15) output
    )

AS

    BEGIN 

        Declare @count int

        set @count = (select count(*) from loginmaster where userid = @userid and password = @password)

        if(@count = 0)

        begin
            Set @Error = 'Error1'
        end

    END





当我从sql运行sp时,它工作正常但是当我使用/调用sp时C#,在executecalar的帮助下,没有按预期工作.Executescalar总是返回null。如果我使用ExecuteNonquery做同样的事情,它正常工作.Executenonquery用于插入/更新.Exeutenonquery返回行受影响的数量.ie是int va略。 Int rowaffected = cmd.ExecuteNonquery()通常我检查rowaffected是否大于0,取值输出para。





when I run sp from sql,it is working properly but when I using/calling sp from C#,with the help of executescalar,not working as expected.Executescalar always return null.If the same same thing I do with ExecuteNonquery,it is working properly.Executenonquery is used for insertion/updation.Exeutenonquery return number of row affected.i.e is int value. Int rowaffected = cmd.ExecuteNonquery() Normally I check if rowaffected is greater than 0,take value output para.

if(rowaffected  > 0)
{
  string s = sqlparameterError.value.Tostring()
}





这里我怎么检查,rowaffected总是-1。这是检查它的正确方法吗?



我尝试了什么:





Here How I check,rowaffected is always -1.Is it right way to check it?

What I have tried:

create procedure [dbo].[Proc_Authenticate]

    (
           @username int,   
           @password varchar(25),
           @Error varchar(15) output
    )

AS

    BEGIN 

        Declare @count int

        set @count = (select count(*) from loginmaster where userid = @userid and password = @password)

        if(@count = 0)

        begin
            Set @Error = 'Error1'
        end

    END

推荐答案

在.net应用中lication你必须设置参数方向,有些像cmd.Parameters [ParamName]。Direction = ParameterDirection.Output;



你可以看看如何返回输出使用C#和VB.Net在ASP.Net中存储过程的参数 [ ^ ]
In your .net application you will have to set parameter direction, some thing like cmd.Parameters["ParamName"].Direction = ParameterDirection.Output;

You can have look at How to return Output parameter from Stored Procedure in ASP.Net using C# and VB.Net[^]


这篇关于如何在C#中从sp和catch / handle返回输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆