ルールの作成、変更、および削除

SMO では、Rule オブジェクトでルールが表現されます。ルールは、TextBody プロパティによって定義されます。このプロパティは、IN、LIKE、または BETWEEN などの演算子や述語を使用した条件式を格納したテキスト文字列です。ルールは、列や他のデータベース オブジェクトを参照することはできません。データベース オブジェクトを参照しない組み込み関数はルールに含めることができます。

TextBody プロパティでの定義には、入力されたデータ値を参照する変数が含まれている必要があります。ルールを作成するときは、任意の名前または記号を使用してこの値を表すことができますが、最初の文字は @ 記号にする必要があります。

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

Visual Basic でのルールの作成、変更、および削除

このコード例では、ルールの作成、作成したルールの列へのアタッチ、Rule オブジェクトのプロパティの修正、列からのデタッチ、および削除を行う方法を示します。

Rule オブジェクトの Dim ステートメントを完全なアセンブリ パスで指定して、System.Data アセンブリの Rule オブジェクトと明確に区別します。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Declare a Table object variable and reference the Product table.
Dim tb As Table
tb = db.Tables("Product", "Production")
'Define a Rule object variable by supplying the parent database, name and schema in the constructor. 
'Note that the full namespace must be given for the Rule type to differentiate it from other Rule types.
Dim ru As Microsoft.SqlServer.Management.Smo.Rule
ru = New Rule(db, "TestRule", "Production")
'Set the TextHeader and TextBody properties to define the rule.
ru.TextHeader = "CREATE RULE [Production].[TestRule] AS"
ru.TextBody = "@value BETWEEN GETDATE() AND DATEADD(year,4,GETDATE())"
'Create the rule on the instance of SQL Server.
ru.Create()
'Bind the rule to a column in the Product table by supplying the table, schema, and 
'column as arguments in the BindToColumn method.
ru.BindToColumn("Product", "SellEndDate", "Production")
'Unbind from the column before removing the rule from the database.
ru.UnbindFromColumn("Product", "SellEndDate", "Production")
ru.Drop()

Visual C# でのルールの作成、変更、および削除

このコード例では、ルールの作成、作成したルールの列へのアタッチ、Rule オブジェクトのプロパティの修正、列からのデタッチ、および削除を行う方法を示します。

Rule オブジェクトの Dim ステートメントを完全なアセンブリ パスで指定して、System.Data アセンブリの Rule オブジェクトと明確に区別します。

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db; 
db = srv.Databases("AdventureWorks"); 
//Declare a Table object variable and reference the Product table. 
Table tb; 
tb = db.Tables("Product", "Production"); 
//Define a Rule object variable by supplying the parent database, name and schema in the constructor. 
//Note that the full namespace must be given for the Rule type to differentiate it from other Rule types. 
Microsoft.SqlServer.Management.Smo.Rule ru; 
ru = new Rule(db, "TestRule", "Production"); 
//Set the TextHeader and TextBody properties to define the rule. 
ru.TextHeader = "CREATE RULE [Production].[TestRule] AS"; 
ru.TextBody = "@value BETWEEN GETDATE() AND DATEADD(year,4,GETDATE())"; 
//Create the rule on the instance of SQL Server. 
ru.Create(); 
//Bind the rule to a column in the Product table by supplying the table, schema, and 
//column as arguments in the BindToColumn method. 
ru.BindToColumn("Product", "SellEndDate", "Production"); 
//Unbind from the column before removing the rule from the database. 
ru.UnbindFromColumn("Product", "SellEndDate", "Production"); 
ru.Drop();
}

関連項目

参照