プロパティの設定

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

objInstance.PropertyName

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

注意

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

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

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

  • SQL Server の以前のバージョンで、新しい SQL Server 機能を表すプロパティにアクセスしようとするなど、サーバーのバージョンがプロパティをサポートしていない場合。

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

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

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

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

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

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

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

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

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

使用例

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

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

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

'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 プロパティを取得する方法と、ConnectionContext プロパティの SqlExecutionModes プロパティを SqlExecutionModes 列挙型の ExecuteSql メンバーに設定する方法を示します。

{ 
//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 でのオブジェクト作成前のさまざまなプロパティの設定

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

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Create a new table in the AdventureWorks2012 database. 
Dim db As Database
db = srv.Databases("AdventureWorks2012")
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# でのオブジェクト作成前のさまざまなプロパティの設定

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

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Create a new table in the AdventureWorks2012 database. 
Database db; 
db = srv.Databases("AdventureWorks2012"); 
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 でのオブジェクトのすべてのプロパティの反復処理

このコード例では、StoredProcedure オブジェクトの Properties コレクションが反復処理され、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 AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
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# でのオブジェクトのすべてのプロパティの反復処理

このコード例では、StoredProcedure オブジェクトの Properties コレクションが反復処理され、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 AdventureWorks2012 database. 
Database db; 
db = srv.Databases("AdventureWorks2012"); 
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 のインスタンスに送信されるステートメントの数を比較することができます。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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 AdventureWorks2012.
'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 のインスタンスに送信されるステートメントの数を比較することができます。

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks2012 database. 
Database db; 
db = srv.Databases("AdventureWorks2012"); 
//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 AdventureWorks2012. 
   //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); 
}