授予、撤消和拒绝权限
ServerPermission 对象用于将一组权限或单个服务器权限分配给 ServerPermissionSet 对象。对于服务器级权限,被授权者指登录名。由 Windows 进行身份验证的登录名以 Windows 用户名的形式列出。当此代码示例运行时,它会撤消被授权者的权限并确认已使用 EnumServerPermissions 方法删除该被授权者。
可以使用 DatabasePermissionSet 对象和 ObjectPermissionSet 对象以类似方式分配数据库权限和数据库对象权限。
示例
若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。有关详细信息,请参阅如何在 Visual Studio .NET 中创建 Visual Basic SMO 项目或如何在 Visual Studio .NET 中创建 Visual C# SMO 项目。
在 Visual Basic 中授予服务器权限
此代码示例将 Create Endpoint 和 Alter Any Endpoint 权限授予指定的登录名,然后枚举并显示权限。将撤消其中一个权限,然后再次枚举权限。此示例假定指定的登录名具有指定的起始操作权限。
'Connect to the local, default instance of SQL Server.
Dim svr As Server
svr = New Server()
'Define a ServerPermissionSet that contains permission to Create Endpoint and Alter Any Endpoint.
Dim sps As ServerPermissionSet
sps = New ServerPermissionSet(ServerPermission.CreateEndpoint)
sps.Add(ServerPermission.AlterAnyEndpoint)
'This sample assumes that the grantee already has permission to Create Endpoints.
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
Dim spis As ServerPermissionInfo()
spis = svr.EnumServerPermissions(vGrantee, sps)
Dim spi As ServerPermissionInfo
Console.WriteLine("=================Before revoke===========================")
For Each spi In spis
Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine(" ")
'Remove a permission from the set.
sps.Remove(ServerPermission.CreateEndpoint)
'Revoke the create endpoint permission from the grantee.
svr.Revoke(sps, vGrantee)
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps)
Console.WriteLine("=================After revoke============================")
For Each spi In spis
Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine(" ")
'Grant the Create Endpoint permission to the grantee.
svr.Grant(sps, vGrantee)
'Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps)
Console.WriteLine("=================After grant=============================")
For Each spi In spis
Console.WriteLine(spi.Grantee & " has " & spi.PermissionType.ToString & " permission.")
Next
Console.WriteLine("")
在 Visual C# 中授予服务器权限
此代码示例将 Create Endpoint 和 Alter Any Endpoint 权限授予指定的登录名,然后枚举并显示权限。将撤消其中一个权限,然后再次枚举权限。此示例假定指定的登录名具有指定的起始操作权限。
//Connect to the local, default instance of SQL Server.
{
Server svr = default(Server);
svr = new Server();
//Define a ServerPermissionSet that contains permission to Create Endpoint and Alter Any Endpoint.
ServerPermissionSet sps = default(ServerPermissionSet);
sps = new ServerPermissionSet(ServerPermission.CreateEndpoint);
sps.Add(ServerPermission.AlterAnyEndpoint);
//This sample assumes that the grantee already has permission to Create Endpoints.
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
ServerPermissionInfo[] spis = null;
spis = svr.EnumServerPermissions(vGrantee, sps);
ServerPermissionInfo spi = default(ServerPermissionInfo);
Console.WriteLine("===========Before revoke==================");
foreach ( spi in spis) {
Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission.");
}
Console.WriteLine(" ");
//Remove a permission from the set.
sps.Remove(ServerPermission.CreateEndpoint);
//Revoke the create endpoint permission from the grantee.
svr.Revoke(sps, vGrantee);
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps);
Console.WriteLine("=========After revoke================");
foreach ( spi in spis) {
Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission.");
}
Console.WriteLine(" ");
//Grant the Create Endpoint permission to the grantee.
svr.Grant(sps, vGrantee);
//Enumerate and display the server permissions in the set for the grantee specified in the vGrantee string variable.
spis = svr.EnumServerPermissions(vGrantee, sps);
Console.WriteLine("=========After grant===============");
foreach ( spi in spis) {
Console.WriteLine(spi.Grantee + " has " + spi.PermissionType.ToString + " permission.");
}
Console.WriteLine("");
}