次の方法で共有


権限の許可、取り消し、および拒否

ServerPermissionSet オブジェクトに対して権限のセットや個別のサーバー権限を割り当てるには、ServerPermission オブジェクトを使用します。サーバー レベルの権限については、ログオンが参照されます。Windows によって認証されたログオンは、Windows ユーザー名としてリストされます。このコード例を実行すると、権限付与対象ユーザーから権限が取り消され、EnumServerPermissions メソッドを使用してこの権限が削除されたことが確認されます。

データベース権限およびデータベース オブジェクト権限は、DatabasePermissionSet オブジェクトおよび ObjectPermissionSet オブジェクトを使用して、同じように割り当てることができます。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」または「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください。

Visual Basic でのサーバー権限の付与

このコード例では、指定されたログインへの Create Endpoint 権限および Alter Any Endpoint 権限を許可し、これらの権限の列挙および表示を行います。これらの権限のうちの 1 つが呼び出されて、権限が再び列挙されます。この例では、指定されたログインには、当初に必要な指定の権限があることを前提としています。

'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 権限を許可し、これらの権限の列挙および表示を行います。これらの権限のうちの 1 つが呼び出されて、権限が再び列挙されます。この例では、指定されたログインには、当初に必要な指定の権限があることを前提としています。

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