ARTICLE

Writing Managed Triggers using VB.NET

Posted by Laishram Priyokumar Articles | ADO.NET in VB.NET July 04, 2007
This article is trying to explain the simple and required steps that are require starting the creation of Manage Triggers using VB.NET
 
Reader Level:

Introduction

With the integration of the CLR with SQL Server 2005, we can create database objects using modern object-oriented languages like VB.NET.

This article is trying to explain the simple and required steps that are require starting the creation of Manage Triggers using VB.NET.

The Project

We will create a Visual Studio 2005 database project for the managed Trigger.

Creating the Database project:

Open Microsoft Visual Studio 2005 and create a SQL Server Project.

File->New->Project->Database

 

Figure 1.

Adding a database reference:

Now it will ask for a database reference.  Add one.



Figure 2.

Add a Trigger:

Right click on the Project and add a Trigger.



Figure 3.



Figure 4.

The file Trigger1.vb:

Past following lines to the file Trigger1.vb. Make sure that the table Person exist on your database or give the name of your table instead of Person.

Imports System 

Imports System.Data 

Imports System.Data.SqlClient 

Imports Microsoft.SqlServer.Server 

Partial Public Class Triggers 

 

    ' Enter existing table or view for the target and uncomment the attribute line

    <Microsoft.SqlServer.Server.SqlTrigger(Name:="Trigger1", Target:="Person", [Event]:="FOR UPDATE")> _

    Public Shared Sub Trigger1() 

 

        ' Replace with your own code

 

        SqlContext.Pipe.Send("Trigger FIRED")

 

    End Sub

End Class

Deploy the Trigger:

Build the project and then deploy it.



Figure 5.

Test the Trigger:

Make sure the CLR is enabled with your SQL Server by running the following SQL statement.

sp_configure 'clr enabled', 1;

GO

RECONFIGURE;
GO

Now execute an update command on the table 'Person'. You will get a line 'Trigger FIRED'.



Figure 6.

Learned Triggering!!!!

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor