管理使用者、角色和登入

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

在 SMO 中,登入是由 物件表示 Login 。 當登入存在於 SQL Server 中時,可以將它新增至伺服器角色。 伺服器角色是由 ServerRole 物件表示。 資料庫角色是由 DatabaseRole 物件表示,而應用程式角色是由 ApplicationRole 物件表示。

與伺服器層級相關聯的許可權會列為 物件的屬性 ServerPermission 。 伺服器層級許可權可以授與、拒絕或撤銷個別登入帳戶。

每個 Database 物件都有 一個 UserCollection 物件,指定資料庫中的所有使用者。 每個使用者都會與登入相關聯。 一個登入可以與多個資料庫中的使用者相關聯。 物件的 LoginEnumDatabaseMappings 方法可用來列出與登入相關聯之每個資料庫中的所有使用者。 或者, User 物件的 Login 屬性會指定與使用者相關聯的登入。

SQL Server 資料庫也有角色,可指定一組資料庫層級許可權,讓使用者執行特定工作。 不同于伺服器角色,資料庫角色不會修正。 您可以建立、修改及移除它們。 許可權和使用者可以指派給資料庫角色以進行大量管理。

範例

針對下列程式碼範例,您必須選取程式設計環境、程式設計範本和程式設計語言,才能建立您的應用程式。 如需詳細資訊,請參閱 在 Visual Studio .NET 中建立 Visual C# SMO 專案。

列舉 Visual C 中的登入和相關聯的使用者#

資料庫中的每個使用者都會與登入相關聯。 登入可以與多個資料庫中的使用者相關聯。 程式碼範例示範如何呼叫 EnumDatabaseMappings 物件的 方法 Login ,以列出與登入相關聯的所有資料庫使用者。 此範例會在 AdventureWorks2022 資料庫中建立登入和使用者,以確定有要列舉的對應資訊。

{   
Server srv = new Server();   
//Iterate through each database and display.   
  
foreach ( Database db in srv.Databases) {   
   Console.WriteLine("========");   
   Console.WriteLine("Login Mappings for the database: " + db.Name);   
   Console.WriteLine(" ");   
   //Run the EnumLoginMappings method and return details of database user-login mappings to a DataTable object variable.   
   DataTable d;  
   d = db.EnumLoginMappings();   
   //Display the mapping information.   
   foreach (DataRow r in d.Rows) {   
      foreach (DataColumn c in r.Table.Columns) {   
         Console.WriteLine(c.ColumnName + " = " + r[c]);   
      }   
      Console.WriteLine(" ");   
   }   
}   
}  

列舉 PowerShell 中的登入和相關聯的使用者

資料庫中的每個使用者都會與登入相關聯。 登入可以與多個資料庫中的使用者相關聯。 程式碼範例示範如何呼叫 EnumDatabaseMappings 物件的 方法 Login ,以列出與登入相關聯的所有資料庫使用者。 此範例會在 AdventureWorks2022 資料庫中建立登入和使用者,以確定有要列舉的對應資訊。

# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\Default\Databases

#Iterate through all databases 
foreach ($db in Get-ChildItem)
{
  "====="
  "Login Mappings for the database: "+ $db.Name

  #get the datatable containing the mapping from the smo database oject
  $dt = $db.EnumLoginMappings()

  #display the results
  foreach($row in $dt.Rows)
  {
     foreach($col in $row.Table.Columns)
     {
       $col.ColumnName + "=" + $row[$col]
     }
   }
 }

管理角色和使用者

此範例示範如何管理角色和使用者。 若要執行此範例,您必須參考下列元件:

  • Microsoft.SqlServer.Smo.dll

  • Microsoft.SqlServer.Management.Sdk.Sfc.dll

  • Microsoft.SqlServer.ConnectionInfo.dll

  • Microsoft.SqlServer.SqlEnum.dll

using Microsoft.SqlServer.Management.Smo;  
using System;  
  
public class A {  
   public static void Main() {  
      Server svr = new Server();  
      Database db = new Database(svr, "TESTDB");  
      db.Create();  
  
      // Creating Logins  
      Login login = new Login(svr, "Login1");  
      login.LoginType = LoginType.SqlLogin;  
      login.Create("password@1");  
  
      Login login2 = new Login(svr, "Login2");  
      login2.LoginType = LoginType.SqlLogin;  
      login2.Create("password@1");  
  
      // Creating Users in the database for the logins created  
      User user1 = new User(db, "User1");  
      user1.Login = "Login1";  
      user1.Create();  
  
      User user2 = new User(db, "User2");  
      user2.Login = "Login2";  
      user2.Create();  
  
      // Creating database permission Sets  
      DatabasePermissionSet dbPermSet = new DatabasePermissionSet(DatabasePermission.AlterAnySchema);  
      dbPermSet.Add(DatabasePermission.AlterAnyUser);  
  
      DatabasePermissionSet dbPermSet2 = new DatabasePermissionSet(DatabasePermission.CreateType);  
      dbPermSet2.Add(DatabasePermission.CreateSchema);  
      dbPermSet2.Add(DatabasePermission.CreateTable);  
  
      // Creating Database roles  
      DatabaseRole role1 = new DatabaseRole(db, "Role1");  
      role1.Create();  
  
      DatabaseRole role2 = new DatabaseRole(db, "Role2");  
      role2.Create();  
  
      // Granting Database Permission Sets to Roles  
      db.Grant(dbPermSet, role1.Name);  
      db.Grant(dbPermSet2, role2.Name);  
  
      // Adding members (Users / Roles) to Role  
      role1.AddMember("User1");  
  
      role2.AddMember("User2");  
  
      // Role1 becomes a member of Role2  
      role2.AddMember("Role1");  
  
      // Enumerating through explicit permissions granted to Role1  
      // enumerates all database permissions for the Grantee  
      DatabasePermissionInfo[] dbPermsRole1 = db.EnumDatabasePermissions("Role1");     
      foreach (DatabasePermissionInfo dbp in dbPermsRole1) {  
         Console.WriteLine(dbp.Grantee + " has " + dbp.PermissionType.ToString() + " permission.");  
      }  
      Console.WriteLine(" ");  
   }  
}