Aracılığıyla paylaş


Koleksiyonlar kullanma

Ki aynı nesne sınıfından inşa edilmiştir ve paylaşmak, aynı üst nesne nesneleri listesi topluluğudur. Derleme nesnesi, her zaman koleksiyonu sonekini nesne türünün adını içerir. Örneğin, belirli bir tablodaki sütunların erişmek için kullanın ColumnCollectionnesne türü. İçerdiği tüm Column, aynı ait nesneleri Tablenesnesini.

Microsoft  Visual Basic  For...EachDeyimi veya Microsoft  Visual C#  foreachdeyimi, her üye toplama yinelemek için kullanılabilir.

Örnekler

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için SQL Server Boks Online'da "How to: Create a Visual Basic SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual Basic SMO Projesi Oluşturma) veya "How to: Create a Visual C# SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual C# SMO Projesi Oluşturma) konularına bakın.

Nesne bir koleksiyon Visual Basic kullanarak başvurma

Bu kod örneği kullanarak bir sütun özelliği ayarlama gösterilmiştir Columns, Tables, ve DatabasesÖzellikler. Bu özellikler, topluluklar, nesnenin adını belirten bir parametre ile kullanıldığında, belirli bir nesneyi tanımlamak için kullanılan gösterir. Adı ve şema için gerekli Tableskoleksiyonu nesne özelliği.

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

Nesne bir koleksiyon Visual C# içinde kullanarak başvurma

Bu kod örneği kullanarak bir sütun özelliği ayarlama gösterilmiştir Columns, Tables, ve DatabasesÖzellikler. Bu özellikler, topluluklar, nesnenin adını belirten bir parametre ile kullanıldığında, belirli bir nesneyi tanımlamak için kullanılan gösterir. Adı ve şema için gerekli Tableskoleksiyonu nesne özelliği.

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

{ 
//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'te bir toplamanın üyeleri aracılığıyla yineleme

Bu kod örneği aracılığıyla sırayla dolaşır DatabasesKoleksiyon özelliği ve tüm bağlantıları örneği veritabanı görüntüler 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# bir toplamanın üyeleri aracılığıyla yineleme

Bu kod örneği aracılığıyla sırayla dolaşır DatabasesKoleksiyon özelliği ve tüm bağlantıları örneği veritabanı görüntüler 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); 
} 

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