Udostępnij za pośrednictwem


Przy użyciu kolekcji

Kolekcja jest listą obiektów skonstruowane z tej samej klasy obiektów i które współużytkują ten sam obiekt nadrzędny.Obiekt kolekcja zawsze zawiera nazwę typu obiektu z sufiksem kolekcja.Na przykład, uzyskać dostęp do kolumn w określonej tabela, należy użyć ColumnCollection typu obiektu.Zawiera wszystkie Column obiektów, które należą do tej samej Table obiektu.

The Microsoft Visual Basic For...Each statement or the Microsoft Visual C# foreach statement can be used to iterate through each member of the collection.

Przykłady

Aby używać dostarczonych przykładów kodu źródłowego, należy wybrać środowisko, szablon oraz język programowania, które będą używane do tworzenia aplikacji. Aby uzyskać więcej informacji, zobacz temat „Jak utworzyć projekt SMO języka Visual Basic w programie Visual Studio .NET” lub „Jak utworzyć projekt SMO języka Visual C# w programie Visual Studio .NET” w dokumentacji SQL Server — książki online.

Odwołanie do obiektu przy użyciu kolekcji w programie Visual Basic

Poniższy przykład kodu pokazuje jak zestaw właściwość kolumna za pomocą Columns, Tables, i Databases właściwość.Właściwości te reprezentują kolekcje, które mogą być używane do identyfikowania określonego obiektu, gdy są one używane przez parametr, który określa nazwę obiektu.Nazwa i schematu są wymagane dla Tables kolekcja właściwość obiektu.

'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("AdventureWorks2008R2").Tables("Person", "Person").Columns("ModifiedDate").Nullable = True
'Call the Alter method to make the change on the instance of SQL Server.
srv.Databases("AdventureWorks2008R2").Tables("Person", "Person").Columns("ModifiedDate").Alter()

Odwołanie do obiektu przy użyciu kolekcji Visual C#

Poniższy przykład kodu pokazuje jak zestaw właściwość kolumna za pomocą Columns, Tables, i Databases właściwość.Właściwości te reprezentują kolekcje, które mogą być używane do identyfikowania określonego obiektu, gdy są one używane przez parametr, który określa nazwę obiektu.Nazwa i schematu są wymagane dla Tables kolekcja właściwość obiektu.

{ 
//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("AdventureWorks2008R2").Tables("Person", "Person").Columns("LastName").Nullable = true; 
//Call the Alter method to make the change on the instance of SQL Server. 
srv.Databases("AdventureWorks2008R2").Tables("Person", "Person").Columns("LastName").Alter(); 
}

Iteracja członków kolekcji w programie Visual Basic

Poniższy przykład kodu iterację Databases kolekcja właściwość i wyświetla wszystkie połączenia z bazą danych do wystąpienie 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)

Iteracja członków kolekcji w programie Visual C#

Poniższy przykład kodu iterację Databases kolekcja właściwość i wyświetla wszystkie połączenia z bazą danych do wystąpienie 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); 
}