我需要根据用户的角色检查用户是否具有访问权限。 (ASP.NET和SQL Server)
SELECT rf.ReadAccess
FROM aspnet_Users as u INNER JOIN aspnet_Roles as r ON r.UserID=u.UserID
INNER JOIN RolesByForm as rf ON rf.RoleID=r.RoleID
WHERE u.UserID=@UserID
所以这让我很亲近,但这才是我真正想要的。我希望默认情况下可以看到表单,所以如果RolesByForm中没有记录,那么它应该是“True”。但如果有记录,它应该基于ReadAccess字段返回。
如何让查询返回布尔值?
答案 0 :(得分:1)
尝试
SELECT case count(rf.ReadAccess) when 0 then 'True' else 'False' end ReadAccess
FROM aspnet_Users as u INNER JOIN aspnet_Roles as r ON r.UserID=u.UserID
INNER JOIN RolesByForm as rf ON rf.RoleID=r.RoleID
WHERE u.UserID=@UserID
答案 1 :(得分:0)
在.NET中,Sql Server字段BIT为true或false,因此:
CAST ( rf.ReadAccess as BIT)
或者,如果ReadAccess是varchar:
CASE ReadAccess
WHEN 'Something' THEN CAST(1 as bit)
ELSE CAST (0 as bit)
END
答案 2 :(得分:0)
SELECT ReadAccess=CASE rf.ReadAccess WHEN 1 THEN 'True' ELSE 'False' END
FROM aspnet_Users as u INNER JOIN aspnet_Roles as r ON r.UserID=u.UserID
INNER JOIN RolesByForm as rf ON rf.RoleID=r.RoleID
WHERE u.UserID=@UserID
在你的.net代码中写下如下内容:
bool result;
bool myBoolean=Boolean.TryParse(dataset["ReadAccess"], out result);
答案 3 :(得分:0)
假设rf.ReadAccess
是位字段(1或0),那么您希望外连接捕获不存在的rolesbyform记录。
或许这样的事情?
SELECT distinct isnull(rf.ReadAccess, 1) as ReadAccess
FROM aspnet_Users as u INNER JOIN aspnet_Roles as r ON r.UserID=u.UserID
LEFT OUTER JOIN RolesByForm as rf ON rf.RoleID=r.RoleID
WHERE u.UserID=@UserID
答案 4 :(得分:0)
您可以返回0(假)和1(真)。试试这个例子:
CREATE PROCEDURE CheckReadAccess
@UserID INT
AS
BEGIN
DECLARE @rdAxs VARCHAR(255)
SELECT @rdAxs = rf.ReadAccess
FROM aspnet_Users as u INNER JOIN aspnet_Roles as r ON r.UserID=u.UserID
INNER JOIN RolesByForm as rf ON rf.RoleID=r.RoleID
WHERE u.UserID=@UserID
IF @rdAxs IS NULL
RETURN 0
ELSE
RETURN 1
END