ALTER PROCEDURE [dbo].[UserLogin] @userid int output, @LoginName nvarchar(50), @Password nvarchar(50) ASBEGIN SET NOCOUNT ON; DECLARE @count int --查询指定表,返回符合条件的个数 SELECT @count=count(*) FROM UserManager WHERE LoginName=@LoginName AND Password=@Password -- PRINT '行数:'+convert(varchar(10),@count) --判断是否有用户,如果有则返回用户ID,否则返回0 IF (@count!=0) begin select @userid=UserId FROM UserManager WHERE LoginName=@LoginName AND Password=@Password END ELSE BEGIN SET @userid=0 END END
业务层的代码:
/// /// 用户登录,返回符合登录名和密码的用户的条数 /// /// 登录名 /// 登录密码 /// int public int UserLogin(string LoginName, string Pwd) { SqlParameter []pars = new SqlParameter[]{ new SqlParameter("@userid",SqlDbType.Int), new SqlParameter("@LoginName",LoginName), new SqlParameter("@Password",Pwd) }; //指定为输出参数 pars[0].Direction = ParameterDirection.Output; DataBaseHelper.SelectSQLReturnObject("UserLogin", CommandType.StoredProcedure, pars); //object count = DataBaseHelper.SelectSQLReturnObject("UserLogin", CommandType.StoredProcedure, pars); //if (count != null) //{ // return int.Parse(count.ToString()); //} //return 0; if (pars[0].Value.ToString() != "0") { return int.Parse(pars[0].Value.ToString()); } else { return 0; } }