Bagikan melalui


Properti Pengaturan - SMO

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

Properti adalah nilai yang menyimpan informasi deskriptif tentang objek. Misalnya, opsi konfigurasi Microsoft SQL Server diwakili oleh Configuration properti objek. Properti dapat diakses baik secara langsung maupun tidak langsung dengan menggunakan koleksi properti. Mengakses properti secara langsung menggunakan sintaks berikut:

objInstance.PropertyName

Nilai properti dapat dimodifikasi atau diambil tergantung pada apakah properti memiliki akses baca/tulis atau akses baca-saja. Anda juga perlu mengatur properti tertentu sebelum objek dapat dibuat. Untuk informasi selengkapnya, lihat referensi SMO untuk objek tertentu.

Note

Kumpulan objek anak muncul sebagai properti objek. Misalnya, koleksi Tabel adalah properti objek Server . Untuk informasi selengkapnya, lihat Menggunakan Koleksi.

Properti objek adalah anggota koleksi Properti. Koleksi Properti dapat digunakan untuk melakukan iterasi melalui setiap properti objek.

Terkadang properti tidak tersedia karena alasan berikut:

  • Versi server tidak mendukung properti , seperti jika Anda mencoba mengakses properti yang mewakili fitur SQL Server baru pada versi SQL Server yang lebih lama.

  • Server tidak menyediakan data untuk properti, seperti jika Anda mencoba mengakses properti yang mewakili komponen SQL Server yang tidak diinstal.

Anda dapat menangani keadaan ini dengan menangkap UnknownPropertyExceptionPropertyCannotBeRetrievedException dan pengecualian SMO.

Mengatur Bidang Inisialisasi Default

SMO melakukan pengoptimalan saat mengambil objek. Pengoptimalan meminimalkan jumlah properti yang dimuat dengan menskalakan secara otomatis antara status berikut:

  1. Dimuat sebagian. Ketika objek pertama kali dirujuk, objek memiliki properti minimum yang tersedia (seperti Nama dan Skema).

  2. Terisi penuh. Ketika properti apa pun dirujuk, properti yang tersisa yang cepat dimuat, diinisialisasi dan tersedia.

  3. Properti yang menggunakan banyak memori. Properti yang tersisa tidak tersedia menggunakan banyak memori dan memiliki Expensive nilai properti true (seperti DataSpaceUsage). Properti ini dimuat hanya jika direferensikan secara khusus.

Jika aplikasi Anda mengambil properti tambahan, selain yang disediakan dalam status dimuat sebagian, aplikasi mengirimkan kueri untuk mengambil properti tambahan ini dan meningkatkan skala ke status yang dimuat sepenuhnya. Ini dapat menyebabkan lalu lintas yang tidak perlu antara klien dan server. Lebih banyak pengoptimalan dapat dicapai dengan memanggil SetDefaultInitFields metode . Metode ini SetDefaultInitFields memungkinkan spesifikasi properti yang dimuat saat objek diinisialisasi.

Metode SetDefaultInitFields ini mengatur perilaku pemuatan properti untuk sisa aplikasi atau sampai diatur ulang. Anda dapat menyimpan perilaku asli dengan menggunakan GetDefaultInitFields metode dan memulihkannya sesuai kebutuhan.

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.

Mendapatkan dan Mengatur Properti di Visual Basic

Contoh kode ini menunjukkan cara mendapatkan Edition properti Information objek dan cara mengatur SqlExecutionModes properti ConnectionContext properti ke anggota ExecuteSql dari SqlExecutionModes jenis enumerasi.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Get a property.
Console.WriteLine(srv.Information.Version)
'Set a property.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql

Mendapatkan dan Mengatur Properti di Visual C#

Contoh kode ini menunjukkan cara mendapatkan Edition properti Information objek dan cara mengatur SqlExecutionModes properti ConnectionContext properti ke anggota ExecuteSql dari SqlExecutionModes jenis enumerasi.

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Get a property.   
Console.WriteLine(srv.Information.Version);   
//Set a property.   
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql;   
}  

Mengatur Berbagai Properti Sebelum Objek Dibuat di Visual Basic

Contoh kode ini menunjukkan cara langsung mengatur AnsiNullsStatus properti Table objek, dan cara membuat dan menambahkan kolom sebelum Anda membuat Table objek.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Create a new table in the AdventureWorks2022 database. 
Dim db As Database
db = srv.Databases("AdventureWorks2022")
Dim tb As Table
'Specify the parent database, table schema and the table name in the constructor.
tb = New Table(db, "Test_Table", "HumanResources")
'Add columns because the table requires columns before it can be created. 
Dim c1 As Column
'Specify the parent table, the column name and data type in the constructor.
c1 = New Column(tb, "ID", DataType.Int)
tb.Columns.Add(c1)
c1.Nullable = False
c1.Identity = True
c1.IdentityIncrement = 1
c1.IdentitySeed = 0
Dim c2 As Column
c2 = New Column(tb, "Name", DataType.NVarChar(100))
c2.Nullable = False
tb.Columns.Add(c2)
tb.AnsiNullsStatus = True
'Create the table on the instance of SQL Server.
tb.Create()

Mengatur Berbagai Properti Sebelum Objek Dibuat di Visual C#

Contoh kode ini menunjukkan cara langsung mengatur AnsiNullsStatus properti Table objek, dan cara membuat dan menambahkan kolom sebelum Anda membuat Table objek.

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Create a new table in the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
Table tb;   
//Specify the parent database, table schema, and the table name in the constructor.   
tb = new Table(db, "Test_Table", "HumanResources");   
//Add columns because the table requires columns before it can be created.   
Column c1;   
//Specify the parent table, the column name, and data type in the constructor.   
c1 = new Column(tb, "ID", DataType.Int);   
tb.Columns.Add(c1);   
c1.Nullable = false;   
c1.Identity = true;   
c1.IdentityIncrement = 1;   
c1.IdentitySeed = 0;   
Column c2;   
c2 = new Column(tb, "Name", DataType.NVarChar(100));   
c2.Nullable = false;   
tb.Columns.Add(c2);   
tb.AnsiNullsStatus = true;   
//Create the table on the instance of SQL Server.   
tb.Create();   
}  

Iterasi Melalui Semua Properti Objek di Visual Basic

Contoh kode ini berulang melalui objek dan menampilkannya di layar Output Visual Studio.

Dalam contoh, Property objek telah dimasukkan ke dalam tanda kurung persegi karena juga merupakan kata kunci Visual Basic.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Set properties on the uspGetEmployeeManagers stored procedure on the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
Dim sp As StoredProcedure
sp = db.StoredProcedures("uspGetEmployeeManagers")
sp.AnsiNullsStatus = False
sp.QuotedIdentifierStatus = False
'Iterate through the properties of the stored procedure and display.
'Note the Property object requires [] parentheses to distinguish it from the Visual Basic key word.
Dim p As [Property]
For Each p In sp.Properties
    Console.WriteLine(p.Name & p.Value)
Next

Iterasi Melalui Semua Properti Objek di Visual C#

Contoh kode ini berulang melalui objek dan menampilkannya di layar Output Visual Studio.

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Set properties on the uspGetEmployeeManagers stored procedure on the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
StoredProcedure sp;   
sp = db.StoredProcedures("uspGetEmployeeManagers");   
sp.AnsiNullsStatus = false;   
sp.QuotedIdentifierStatus = false;   
//Iterate through the properties of the stored procedure and display.   
  Property p;   
  foreach ( p in sp.Properties) {   
    Console.WriteLine(p.Name + p.Value);   
  }   
}  

Mengatur Bidang Inisialisasi Default di Visual Basic

Contoh kode ini menunjukkan cara meminimalkan jumlah properti objek yang diinisialisasi dalam program SMO. Anda harus menyertakan using System.Collections.Specializedpernyataan ; untuk menggunakan StringCollection objek .

SQL Server Profiler dapat digunakan untuk membandingkan pernyataan angka yang dikirim ke instans SQL Server dengan pengoptimalan ini.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Assign the Table object type to a System.Type object variable.
Dim tb As Table
Dim typ As Type
tb = New Table
typ = tb.GetType
'Assign the current default initialization fields for the Table object type to a 
'StringCollection object variable.
Dim sc As StringCollection
sc = srv.GetDefaultInitFields(typ)
'Set the default initialization fields for the Table object type to the CreateDate property.
srv.SetDefaultInitFields(typ, "CreateDate")
'Retrieve the Schema, Name, and CreateDate properties for every table in AdventureWorks2022.
'Note that the improvement in performance can be viewed in SQL Profiler.
For Each tb In db.Tables
    Console.WriteLine(tb.Schema + "." + tb.Name + " " + tb.CreateDate)
Next
'Set the default initialization fields for the Table object type back to the original settings.
srv.SetDefaultInitFields(typ, sc)

Mengatur Bidang Inisialisasi Default di Visual C#

Contoh kode ini menunjukkan cara meminimalkan jumlah properti objek yang diinisialisasi dalam program SMO. Anda harus menyertakan using System.Collections.Specializedpernyataan ; untuk menggunakan StringCollection objek .

SQL Server Profiler dapat digunakan untuk membandingkan pernyataan angka yang dikirim ke instans SQL Server dengan pengoptimalan ini.

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Reference the AdventureWorks2022 database.   
Database db;   
db = srv.Databases["AdventureWorks2022"];   
//Assign the Table object type to a System.Type object variable.   
Table tb;   
Type typ;   
tb = new Table();   
typ = tb.GetType;   
//Assign the current default initialization fields for the Table object type to a   
//StringCollection object variable.   
StringCollection sc;   
sc = srv.GetDefaultInitFields(typ);   
//Set the default initialization fields for the Table object type to the CreateDate property.   
srv.SetDefaultInitFields(typ, "CreateDate");   
//Retrieve the Schema, Name, and CreateDate properties for every table in AdventureWorks2022.   
   //Note that the improvement in performance can be viewed in SQL Server Profiler.   
foreach ( tb in db.Tables) {   
   Console.WriteLine(tb.Schema + "." + tb.Name + " " + tb.CreateDate);   
}   
//Set the default initialization fields for the Table object type back to the original settings.   
srv.SetDefaultInitFields(typ, sc);   
}