Bagikan melalui


Menggunakan Koleksi

Berlaku untuk:SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse AnalyticsSQL database di Microsoft Fabric

Koleksi adalah daftar objek yang telah dibangun dari kelas objek yang sama dan yang berbagi objek induk yang sama. Objek koleksi selalu berisi nama tipe objek dengan akhiran Koleksi. Misalnya, untuk mengakses kolom dalam tabel tertentu, gunakan ColumnCollection jenis objek. Ini berisi semua Column objek yang termasuk dalam objek yang sama Table .

Microsoft Visual Basic untuk... Setiap pernyataan atau pernyataan microsoft C# foreach dapat digunakan untuk melakukan iterasi melalui setiap anggota koleksi.

Examples

Untuk menggunakan contoh kode apa pun yang disediakan, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.

Mereferensikan Objek dengan Menggunakan Koleksi di Visual Basic

Contoh kode ini menunjukkan cara mengatur properti kolom dengan menggunakan Columnsproperti , Tables, dan Databases . Properti ini mewakili koleksi, yang dapat digunakan untuk mengidentifikasi objek tertentu ketika digunakan dengan parameter yang menentukan nama objek. Nama dan skema diperlukan untuk Tables properti objek koleksi.

'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()

Mereferensikan Objek dengan Menggunakan Koleksi di Visual C#

Contoh kode ini menunjukkan cara mengatur properti kolom dengan menggunakan Columnsproperti , Tables, dan Databases . Properti ini mewakili koleksi, yang dapat digunakan untuk mengidentifikasi objek tertentu ketika digunakan dengan parameter yang menentukan nama objek. Nama dan skema diperlukan untuk Tables properti objek koleksi.

{   
//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();   
}  

Iterasi Melalui Anggota Koleksi di Visual Basic

Contoh kode ini berulang melalui Databases properti koleksi dan menampilkan semua koneksi database ke instans 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)

Iterasi Melalui Anggota Koleksi di Visual C#

Contoh kode ini berulang melalui Databases properti koleksi dan menampilkan semua koneksi database ke instans 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);   
}