共用方式為


授與、撤銷和拒絕權限

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(""); 
}