コレクションの使用

コレクションとは、同じオブジェクト クラスから作成された、同じ親オブジェクトを持つオブジェクトのリストのことです。 コレクション オブジェクトには、Collection サフィックス付きのオブジェクトの種類の名前が必ず含まれています。 たとえば、指定されたテーブル内の列にアクセスするには、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 でのコレクションを使用したオブジェクトの参照

このコード例では、Columns プロパティ、Tables プロパティ、および Databases プロパティを使用して、列プロパティを設定する方法を示します。 これらのプロパティはコレクションを表現しており、オブジェクトの名前を指定するパラメーターと共に使用すれば、特定のオブジェクトを識別するために使用できます。 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("AdventureWorks2012").Tables("Person", "Person").Columns("ModifiedDate").Nullable = True
'Call the Alter method to make the change on the instance of SQL Server.
srv.Databases("AdventureWorks2012").Tables("Person", "Person").Columns("ModifiedDate").Alter()

Visual C# でのコレクションを使用したオブジェクトの参照

このコード例では、Columns プロパティ、Tables プロパティ、および Databases プロパティを使用して、列プロパティを設定する方法を示します。 これらのプロパティはコレクションを表現しており、オブジェクトの名前を指定するパラメーターと共に使用すれば、特定のオブジェクトを識別するために使用できます。 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 のインスタンスへのすべてのデータベース接続を表示します。

'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); 
}