集合是已從相同物件類別建構且共用相同父對象的物件清單。 集合物件一律包含具有集合後綴的物件類型名稱。 例如,若要存取指定數據表中的數據行,請使用 ColumnCollection 物件類型。 它包含屬於相同Table物件的所有Column物件。
Microsoft Visual Basic For...Each 語句或 Microsoft Visual C# foreach 語句可用來逐一查看集合的每個成員。
範例
若要使用提供的任何程式代碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱《SQL Server 在線叢書》中的<如何:在Visual Studio .NET 中建立Visual Basic SMO 專案>或<如何:在Visual Studio .NET 中建立 Visual C# SMO 專案>。
在 Visual Basic 中使用集合參考物件
此程式代碼範例示範如何使用、 Tables和 Databases 屬性來設定資料行屬性Columns。 這些屬性代表集合,當集合與指定物件名稱的參數搭配使用時,可用來識別特定物件。 集合物件屬性需要 Tables 名稱和架構。
在 Visual C 中使用集合參考物件#
此程式代碼範例示範如何使用、 Tables和 Databases 屬性來設定資料行屬性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("AdventureWorks2012").Tables("Person", "Person").Columns("LastName").Nullable = true;
//Call the Alter method to make the change on the instance of SQL Server.
srv.Databases("AdventureWorks2012").Tables("Person", "Person").Columns("LastName").Alter();
}
逐一查看 Visual Basic 中集合的成員
此程式代碼範例會逐一查看集合屬性, Databases 並顯示 SQL Server 實例的所有資料庫連線。
逐一查看 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);
}