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