Freigeben über


Erstellen, Ändern und Löschen von Regeln

In SMO werden Regeln durch das Rule-Objekt dargestellt. Die Regel wird durch die TextBody-Eigenschaft definiert, wobei es sich um eine Textzeichenfolge handelt, die einen Bedingungsausdruck enthält, der Operatoren oder Prädikate wie IN, LIKE oder BETWEEN verwendet. Eine Regel kann nicht auf Spalten oder andere Datenbankobjekte verweisen. Integrierte Funktionen, die nicht auf Datenbankobjekte verweisen, dürfen in einer Regel eingeschlossen sein.

Die Definition in der TextBody-Eigenschaft muss eine Variable enthalten, die auf den eingegebenen Datenwert verweist. Wenn Sie die Regel erstellen, können Sie den Wert durch einen beliebigen Namen bzw. ein Symbol darstellen; das erste Zeichen muss jedoch ein @-Zeichen sein.

Beispiel

Um die bereitgestellten Codebeispiele verwenden zu können, müssen Sie die Programmierumgebung, die Programmiervorlage und die Programmiersprache wählen, in der die Anwendung erstellt werden soll. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines Visual Basic-SMO-Projekts in Visual Studio .NET oder Vorgehensweise: Erstellen eines Visual C#-SMO-Projekts in Visual Studio .NET.

Erstellen, Ändern und Entfernen einer Regel in Visual Basic

Dieses Codebeispiel zeigt, wie eine Regel erstellt und an eine Spalte angefügt wird, wie Eigenschaften des Rule-Objekts geändert werden, die Regel von der Spalte getrennt und anschließend gelöscht wird.

Die Dim-Anweisung für das Rule-Objekt wird mit dem vollständigen Assemblypfad angegeben, um Mehrdeutigkeit in Bezug auf ein Rule-Objekt in der System.Data-Assembly zu vermeiden.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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()

Erstellen, Ändern und Löschen einer Regel in Visual C#

Dieses Codebeispiel zeigt, wie eine Regel erstellt und an eine Spalte angefügt wird, wie Eigenschaften des Rule-Objekts geändert werden, die Regel von der Spalte getrennt und anschließend gelöscht wird.

Die Dim-Anweisung für das Rule-Objekt wird mit dem vollständigen Assemblypfad angegeben, um Mehrdeutigkeit in Bezug auf ein Rule-Objekt in der System.Data-Assembly zu vermeiden.

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


        }

Erstellen, Ändern und Löschen einer Regel in PowerShell

Dieses Codebeispiel zeigt, wie eine Regel erstellt und an eine Spalte angefügt wird, wie Eigenschaften des Rule-Objekts geändert werden, die Regel von der Spalte getrennt und anschließend gelöscht wird.

Die Dim-Anweisung für das Rule-Objekt wird mit dem vollständigen Assemblypfad angegeben, um Mehrdeutigkeit in Bezug auf ein Rule-Objekt in der System.Data-Assembly zu vermeiden.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2008R2
CD \sql\localhost\default\databases
$db = get-item Adventureworks2008R2

# Define a Rule object variable by supplying the parent database, name and schema in the constructor. 
$ru = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Rule `
-argumentlist $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()

Siehe auch

Verweis