首页 新闻 会员 周边 捐助

SQL 返回值

0
悬赏园豆:10 [待解决问题]

SET NOCOUNT ON;
IF (@UserName IS NULL) or (@LoginPassword IS NULL)
RETURN -1
DECLARE @COUNT int
SET @COUNT = 0
BEGIN TRANSACTION
-- 验证用户登录密码
DECLARE @UserID int,@UserPassword varchar(50)
SELECT @UserID = UserID, @UserPassword = [Password]
FROM dbo._5rUsers
WHERE (UserName = @UserName)
IF (@LoginPassword = @UserPassword)
SET @COUNT = 1
-- 回写用户登录信息
UPDATE dbo._5rUsers
SET LastLoginTime = getdate(),
LastLoginIP
= @LoginIP,
LoginTimes
= LoginTimes + 1
WHERE (UserID = @UserID)
IF (@@ERROR <> 0)
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
-- 写入用户登录日志
INSERT INTO dbo._sgUserLoginLog(UserID, LoginUrl, Module, LoginTime, LoginIP, [Description])
VALUES(@UserID, @LoginUrl, @Module, getdate(), @LoginIP, @Description)
IF (@@ERROR <> 0)
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
COMMIT TRANSACTION
RETURN @COUNT  

问题补充: asp.net 方法中取得@COUNT这个值
SmallMax的主页 SmallMax | 初学一级 | 园豆:190
提问于:2011-02-24 13:09
< >
分享
所有回答(4)
0

你这些是存储过程吗?

like%'远远'% | 园豆:635 (小虾三级) | 2011-02-24 13:28
0

存储过程:

Create Proc MyProc
(
@UserName nvarchar(20),
@LoginPassword nvarchar(20),
---其他参数
@COUNT int output
)
as
----excute some sql
set @COUNT=1
go

  在c#中用sqlcommand调用想得到返回的s值:

 

sqlcommand1.parameters["@UserName"].value ="someOne";
sqlcommand1.parameters[
"@LoginPassword"].value ="pwd";
sqlcommand1.executereader();
int count = Convert.ToInt32(sqlcommand1.parameters["@COUNT"].value);
邀月 | 园豆:25475 (高人七级) | 2011-02-24 13:53
0

可以在存储过程最后加一句 select @count 程序里面读取 SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "存储过程名",null);

 public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] cmdParams)
        {
            ReadConnect();
            object reValue;
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    PrepareCommand(cmd, cmdType, cmdText, cmdParams);
                    conn.Open();
                    reValue = cmd.ExecuteScalar();
                }
            }
            return reValue;
        }

Dispose | 园豆:210 (菜鸟二级) | 2011-02-24 21:25
0

首先,针对@COUNT这个变量,可以在SQL里面做一个标量型的用户自定义函数,在SQL级别获取@COUNT值并return,
然后,在SQL里面做一个存储过程,在存储过程内部select针对标量函数的执行结果,对外返回,用户定义函数和存储过程是否有参数不重要,
最后,在C#程序中,制作SqlCommand,为其绑定必要的SqlParameter,调用其ExecuteScalar方法,并将其返回值转化为C#中的int类型即可。
收工!

zhaohua_wang | 园豆:255 (菜鸟二级) | 2011-03-09 16:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册