다음을 통해 공유


컬렉션 사용

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

컬렉션은 동일한 개체 클래스에서 생성되고 동일한 부모 개체를 공유하는 개체 목록입니다. 컬렉션 개체에는 항상 Collection 접미사가 있는 개체 유형의 이름이 포함됩니다. 예를 들어 지정된 테이블의 열에 액세스하려면 개체 형식을 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);   
}