SQL SMO - 检查,如果用户是服务器管理员 [英] SQL SMO - checking if a user is a server admin
问题描述
我要检查,如果连接的用户是一个SQL Server管理员(在sysadmin角色),并想知道如果这是,这是做到这一点的最好方法是什么?
I want to check if the connected user is a SQL Server admin (in the sysadmin role) and wondering if this is this is the best way to do it?
private static bool IsUserAdmin(Server server)
{
Login login = server.Logins[server.ConnectionContext.Login];
return login.IsMember("sysadmin");
}
在SQL DMO(基于该COM predecessor到SMO)有挂过叫 SQLDMO.SQLServer
对象属性 IsServerAdmin
。我不知道是否有在SMO类似的东西,但我只是一直没能找到它?
In SQL DMO (the COM based predecessor to SMO) there was a property that hung off the SQLDMO.SQLServer
object called IsServerAdmin
. I'm wondering if there's something similar in SMO but that I just haven't been able to find it?
推荐答案
是的,其实我觉得你是来检查当前登录名是SQL Server管理员的最佳方法(即再是其成员系统管理员
角色)。
Yes, actually I think yours is the best way to check if the current login is a SQL Server administrator (that then is a member of sysadmin
role).
我有SMO的一些经验,我发现没有其他办法比这更好的,也因为没有财产类似于 IsServerAdmin
中的 SMO文件相关的登录
对象。
I have some experience with SMO and I found no other way better than this, also because there's no property similar to IsServerAdmin
listed in the SMO documentation related to the Login
object.
如果你愿意,你可以执行一个T-SQL查询来了解哪些SQL Server登录系统管理员的成员
的角色(这将使你的C#$同样的结果C $ C):
If you want, you can execute a T-SQL query to know which SQL Server Logins are members of sysadmin
role (that will give you the same results of C# code):
SELECT p.name AS [Name] ,r.type_desc,r.is_disabled,r.create_date , r.modify_date,r.default_database_name
FROM
sys.server_principals r
INNER JOIN sys.server_role_members m ON r.principal_id = m.role_principal_id
INNER JOIN sys.server_principals p ON
p.principal_id = m.member_principal_id
WHERE r.type = 'R' and r.name = N'sysadmin'
这篇关于SQL SMO - 检查,如果用户是服务器管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!