共用方式為


建立、改變和移除規則

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

請參閱

參考