Udostępnij za pośrednictwem


Tworzenie, zmienianie i usuwanie wyzwalaczy

W obiekty SMO, wyzwalacze są reprezentowane za pomocą Trigger obiekt. The Transact-SQL code that runs when the trigger that is fired is zestaw by the TextBody() właściwość of the Trigger object. Typ wyzwalacza zestaw za pomocą innych właściwości Trigger obiekt, taki jak Update() Właściwość. Jest to właściwość logiczna, która określa, czy wyzwalacz jest uruchamiany przez UPDATE rekordów w tabela nadrzędnej.

The Trigger object represents traditional, język edycji danych (DML) triggers. W SQL Server 2008, są również obsługiwane wyzwalacze (DDL) języka definicja danych. Wyzwalacze DDL są reprezentowane przez DatabaseDdlTrigger obiekt a ServerDdlTrigger obiekt.

Przykład

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see "How to: Create a Visual Basic SMO Project in Visual Studio .NET" or "How to: Create a Visual C# SMO Project in Visual Studio .NET" in SQL Server Books Online.

Tworzenie, zmienianie i usuwanie wyzwalacza w języku Visual Basic

W tym przykładzie kodu pokazano, jak utworzyć i Wstaw wyzwalacz aktualizacji w istniejącej tabela o nazwie Sprzedaż, w AdventureWorks Baza danych. Wyzwalacz wysyła komunikat monitu w tabela jest aktualizacji lub wstawieniu nowego rekordu.

Tworzenie, zmienianie i usuwanie wyzwalacza w środowisku Visual C#

W tym przykładzie kodu pokazano, jak utworzyć i Wstaw wyzwalacz aktualizacji w istniejącej tabela o nazwie Sprzedaż, w AdventureWorks Baza danych. Wyzwalacz wysyła komunikat monitu w tabela jest aktualizacji lub wstawieniu nowego rekordu.

{ 
//Connect to the local, default instance of SQL Server. 
Server mysrv; 
mysrv = new Server(); 
//Reference the AdventureWorks database. 
Database mydb; 
mydb = mysrv.Databases("AdventureWorks"); 
//Declare a Table object variable and reference the Customer table. 
Table mytab; 
mytab = mydb.Tables("Customer", "Sales"); 
//Define a Trigger object variable by supplying the parent table, schema ,and name in the constructor. 
Trigger tr; 
tr = new Trigger(mytab, "Sales"); 
//Set TextMode property to False, then set other properties to define the trigger. 
tr.TextMode = false; 
tr.Insert = true; 
tr.Update = true; 
tr.InsertOrder = Agent.ActivationOrder.First; 
string stmt; 
stmt = " RAISERROR('Notify Customer Relations',16,10) "; 
tr.TextBody = stmt; 
tr.ImplementationType = ImplementationType.TransactSql; 
//Create the trigger on the instance of SQL Server. 
tr.Create(); 
//Remove the trigger. 
tr.Drop(); 
}