需要从c#中的存储过程返回两个值

时间:2012-02-04 13:37:36

标签: sql

我想调用一个存储过程,它接受两个参数(用户名和密码)并返回两个int类型的值('result'和'group')'result'来检查用户是否有效'' group'只显示客户的群组数量 有三种类型的群体 组#1:管理员用户 组#2:自定义用户 grpup#3:特殊用户

下面是我当前的代码,它只返回结果

ALTER PROCEDURE [dbo].[suserpass] 
@username Varchar(50), 
@password varchar(50), 
@result int OUTPUT,
@group int OUTPUT

AS
IF EXISTS (select [user]   from [userpassTable] where [user] = @username and [pass]=@password) 
  set @result=1  

else 
  set @result=0

感谢

3 个答案:

答案 0 :(得分:0)

设置两个输出参数。

你可以这样做:

IF EXISTS (SELECT [user] FROM [userpassTable] 
           WHERE [user] = @username AND [pass] = @password) 
BEGIN
  SELECT @result=1 , @group = group
  FROM [userpassTable] 
  WHERE [user] = @username
END
else 
BEGIN
  SELECT @result=0 , @group = 0 -- group 0 doesn't exist, used as user does exist
END

为了便于阅读,我添加了BEGIN/END块,并允许多个语句。

在您的C#中,@result@group都应设置为输出参数。

答案 1 :(得分:0)

你需要做一些如下所示的事情,你需要设置两个输出变量来完成你的任务......

  ALTER PROCEDURE [dbo].[suserpass] 
    @username Varchar(50), 
    @password varchar(50), 
    @result int OUTPUT,
    @group int OUTPUT

    AS
    IF EXISTS (select [user]   from [userpassTable] where [user] = @username and [pass]=@password) 
    begin
      select @result=1
      select @group = usergroup from [userpassTable] where [user] = @username and [pass]=@password  
    end 
    else
    begin 
      select @result=0
      select @group=0
    end

答案 2 :(得分:0)

我想,从概念上讲,你正在寻找类似的东西(尚未测试代码):

ALTER PROCEDURE [dbo].[suserpass] 
@username Varchar(50), 
@password varchar(50), 
@result int OUTPUT,
@group int OUTPUT

AS
IF EXISTS (select [user] from [userpassTable] where [user] = @username and [pass] = @password) 
  BEGIN
    set @result = 1
    set @group = (select (top 1) [groupType] from [userGroupTable] where [user] = @username)
  END
else     
  set @result=0