컬렉션 사용

컬렉션은 동일한 개체 클래스에서 구성되고 동일한 부모 개체를 공유하는 개체 목록입니다. 컬렉션 개체에는 항상 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, TablesDatabases 속성을 사용하여 열 속성을 설정하는 방법을 보여 줍니다. 이러한 속성은 개체 이름을 지정하는 매개 변��와 함께 사용할 경우 특정 개체를 식별할 수 있는 컬렉션을 나타냅니다. 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, TablesDatabases 속성을 사용하여 열 속성을 설정하는 방법을 보여 줍니다. 이러한 속성은 개체 이름을 지정하는 매개 변��와 함께 사용할 경우 특정 개체를 식별할 수 있는 컬렉션을 나타냅니다. 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); 
}