创建、更改和删除规则

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

请参阅

参考