共用方式為


使用集合

適用於:SQL ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse Analytics

集合是已從相同物件類別建構且共用相同父對象的物件清單。 集合物件一律包含具有集合後綴的物件類型名稱。 例如,若要存取指定數據表中的數據行,請使用 ColumnCollection 物件類型。 它包含屬於相同Table物件的所有Column物件。

Microsoft Visual Basic For...每個 語句或Microsoft C# foreach 語句都可以用來逐一查看集合的每個成員。

範例

若要使用提供的任何程式代碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在Visual Studio .NET 中建立Visual C# SMO 專案。

在 Visual Basic 中使用集合參考物件

此程式代碼範例示範如何使用、 TablesDatabases 屬性來設定資料行屬性Columns。 這些屬性代表集合,當集合與指定物件名稱的參數搭配使用時,可用來識別特定物件。 集合物件屬性需要 Tables 名稱和架構。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Modify a property using the Databases, Tables, and Columns collections to reference a column.
srv.Databases("AdventureWorks2022").Tables("Person", "Person").Columns("ModifiedDate").Nullable = True
'Call the Alter method to make the change on the instance of SQL Server.
srv.Databases("AdventureWorks2022").Tables("Person", "Person").Columns("ModifiedDate").Alter()

在 Visual C 中使用集合參考物件#

此程式代碼範例示範如何使用、 TablesDatabases 屬性來設定資料行屬性Columns。 這些屬性代表集合,當集合與指定物件名稱的參數搭配使用時,可用來識別特定物件。 集合物件屬性需要 Tables 名稱和架構。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Modify a property using the Databases, Tables, and Columns collections to reference a column.   
srv.Databases["AdventureWorks2022"].Tables["Person", "Person"].Columns["LastName"].Nullable = true;   
//Call the Alter method to make the change on the instance of SQL Server.   
srv.Databases["AdventureWorks2022"].Tables["Person", "Person"].Columns["LastName"].Alter();   
}  

逐一查看 Visual Basic 中集合的成員

此程式代碼範例會逐一查看集合屬性, Databases 並顯示 SQL Server 實例的所有資料庫連線。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
Dim count As Integer
Dim total As Integer
'Iterate through the databases and call the GetActiveDBConnectionCount method.
Dim db As Database
For Each db In srv.Databases
    count = srv.GetActiveDBConnectionCount(db.Name)
    total = total + count
    'Display the number of connections for each database.
    Console.WriteLine(count & " connections on " & db.Name)
Next
'Display the total number of connections on the instance of SQL Server.
Console.WriteLine("Total connections =" & total)

逐一查看 Visual C 中集合的成員#

此程式代碼範例會逐一查看集合屬性, Databases 並顯示 SQL Server 實例的所有資料庫連線。

//Connect to the local, default instance of SQL Server.   
{   
Server srv = default(Server);   
srv = new Server();   
int count = 0;   
int total = 0;   
//Iterate through the databases and call the GetActiveDBConnectionCount method.   
Database db = default(Database);   
foreach ( db in srv.Databases) {   
  count = srv.GetActiveDBConnectionCount(db.Name);   
  total = total + count;   
  //Display the number of connections for each database.   
  Console.WriteLine(count + " connections on " + db.Name);   
}   
//Display the total number of connections on the instance of SQL Server.   
Console.WriteLine("Total connections =" + total);   
}