プロパティの設定 - SMO

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

プロパティとは、オブジェクトに関する説明情報を格納する値のことです。 たとえば、Microsoft SQL Server構成オプションは、オブジェクトのプロパティによってConfiguration表されます。 プロパティは、直接、またはプロパティ コレクションを使用して間接的にアクセスすることができます。 プロパティへの直接アクセス時には、次の構文を使用します。

objInstance.PropertyName

プロパティ値は、そのプロパティが読み取り/書き込みアクセスまたは読み取り専用アクセスのどちらであるかに応じて、変更または取得を行うことができます。 また、オブジェクトを作成する前に、特定のプロパティを設定する必要もあります。 詳細については、SMO 参考資料で特定のオブジェクトを参照してください。

Note

子オブジェクトのコレクションは、オブジェクトのプロパティとして表現されます。 たとえば、 Tables コレクションは、 Server オブジェクトのプロパティとなります。 詳しくは、「 Using Collections」をご覧ください。

オブジェクトのプロパティは、Properties コレクションのメンバーです。 Properties コレクションを使用して、オブジェクトの各プロパティを反復処理することができます。

次の理由によって、プロパティを使用できない場合があります。

  • 古いバージョンのSQL Serverで新しいSQL Server機能を表すプロパティにアクセスしようとした場合など、サーバーのバージョンでは プロパティはサポートされていません。

  • インストールされていないSQL Server コンポーネントを表すプロパティにアクセスしようとした場合など、サーバーは プロパティのデータを提供しません。

UnknownPropertyException および PropertyCannotBeRetrievedException の SMO 例外をキャッチすることによって、これらの状況に対処することができます。

既定の初期化フィールドの設定

SMO は、オブジェクトの取得時に最適化を行います。 最適化によって、次の状態の間で自動的にスケーリングを行うことによって読み込まれるプロパティ数を最小限に抑えることができます。

  1. 部分的読み込み。 オブジェクトへの最初の参照時には、最少限のプロパティ (Name や Schema など) のみが使用可能になります。

  2. 完全読み込み。 いずれかのプロパティが参照されたとき、すぐに読み込まれた残りのプロパティが初期化されて使用可能になります。

  3. 大量のメモリを使用するプロパティ。 これ以外の、多くのメモリを使用し、Expensive プロパティ値 (DataSpaceUsage など) の値が true であるプロパティは読み込まれません。 これらのプロパティは、明示的に参照された場合にのみ読み込まれます。

部分的に読み込まれた状態で提供されたプロパティだけでなく、それ以外のプロパティもアプリケーションによってフェッチされる場合、これらの追加のプロパティを取得するクエリが送信され、完全に読み込まれた状態にスケール アップされます。 これにより、クライアントとサーバーの間に不要なトラフィックが発生する場合があります。 SetDefaultInitFields メソッドを呼び出すことにより、さらに最適化を行うことができます。 SetDefaultInitFields メソッドを使用すると、オブジェクトの初期化時に読み込まれるプロパティを指定することができます。

SetDefaultInitFields メソッドでは、アプリケーションの残り部分に対して、またはリセットされるまでの、プロパティの読み込み動作を設定することができます。 GetDefaultInitFields メソッドを使用して、元の動作を保存し、必要に応じて復元することができます。

提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual C# SMO プロジェクトを作成する」を参照してください。

Visual Basic でのプロパティの取得および設定

このコード例では、 オブジェクトの Information プロパティをEdition取得する方法と、 プロパティの プロパティをSqlExecutionModes列挙型の ConnectionContextExecuteSql メンバーに設定する方法をSqlExecutionModes示します。

'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

Visual C# でのプロパティの取得および設定

このコード例では、 オブジェクトの Information プロパティをEdition取得する方法と、 プロパティの プロパティをSqlExecutionModes列挙型の ConnectionContextExecuteSql メンバーに設定する方法をSqlExecutionModes示します。

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

Visual Basic でのオブジェクト作成前のさまざまなプロパティの設定

このコード例では、AnsiNullsStatus オブジェクトを作成する前に、Table オブジェクトの Table プロパティを直接設定する方法、および列の作成と追加を行う方法を示します。

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

Visual C# でのオブジェクト作成前のさまざまなプロパティの設定

このコード例では、AnsiNullsStatus オブジェクトを作成する前に、Table オブジェクトの Table プロパティを直接設定する方法、および列の作成と追加を行う方法を示します。

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

Visual Basic でのオブジェクトのすべてのプロパティの反復処理

このコード例では、オブジェクトの Properties コレクションを StoredProcedure 反復処理し、Visual Studio の [出力] 画面に表示します。

この例では、 Property オブジェクトは 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

Visual C# でのオブジェクトのすべてのプロパティの反復処理

このコード例では、オブジェクトの Properties コレクションを StoredProcedure 反復処理し、Visual Studio の [出力] 画面に表示します。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Set properties on the uspGetEmployeedManagers 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);   
  }   
}  

Visual Basic での既定の初期化フィールドの設定

このコード例では、SMO プログラムで初期化されるオブジェクト プロパティの数を最小にする方法を示します。 StringCollection オブジェクトを使用するには、using System.Collections.Specialized ステートメントを含める必要があります。

SQL Server Profilerを使用して、SQL Serverのインスタンスに送信された number ステートメントをこの最適化と比較できます。

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

Visual C# での既定の初期化フィールドの設定

このコード例では、SMO プログラムで初期化されるオブジェクト プロパティの数を最小にする方法を示します。 StringCollection オブジェクトを使用するには、using System.Collections.Specialized ステートメントを含める必要があります。

SQL Server Profilerを使用して、SQL Serverのインスタンスに送信された number ステートメントをこの最適化と比較できます。

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