Chinese (People's Republic of China)  English  Français


Supinfo-Projects.com
Tous les projets des élèves ingénieurs de Supinfo



Projets
  Dernier Projet
  Les plus populaires
  Tous les Projets

325 Visiteurs
3168 Projets


My Supinfo-Projects

   Connectez-vous
   Créez un Compte


Synopsis

   15 Visites
   Note INTERNET : 20
    (1 Votants)
   25 Commentaires

   Lire l'article

Evaluez cet article

20
18
16
14
12
10
8
6
4
2
0


Commentez cet article

Auteur :

Email :

Votre commentaire :



 
2005 - Note de Synthèse Stage
Integrate DotNet in Sql Server 2005
[25 mn de lecture - paru le 10/29/2005 6:06:13 PM - Public : Confirmé]

Auteur

degrem_mMichel DEGREMONT
Elève-Ingénieur Supinfo Paris
Promotion SUPINFO 2006

   Lui écrire
   Tous les projets de cet auteur
   Le mini-CV de cet auteur

3. To create Trigger in Sql server 2005 with NET

3.1. Creation of the Trigger class

We will take for example that if our table is modified then we will automatically create an INSERT in this same table with the current date in parameter.

To add a news class Trigger.

sql_clr_14.jpg

 

Simple example


 
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

partial public class Triggers
{
[ Microsoft.SqlServer.Server. SqlTrigger (Name = "MonTrigger" , Target = "project" , Vent = "FOR UPDATE" ) ]
public static void Trigger1()
{
SqlTriggerContext monTrig = SqlContext TriggerContext;
SqlPipe sqlPip = SqlContext Pipe;
yew (monTrig.TriggerAction == TriggerAction Update)
{
using ( SqlConnection conn = new SqlConnection ( "context connection=true;" ))
using ( SqlCommand cmd = conn.CreateCommand())
{
try
{
cmd.CommandText = "INSERT INTO project (nomProjet) BEEN WORTH (getdate())" ;
conn.Open();
SqlContext Pipe.Send( "Result: " + cmd.ExecuteNonQuery().ToString());
}
wrestling ( Exception ex)
{
SqlContext Pipe.Send( "Error" + ex.Message);
}
finally
{
conn.Close();
}
}
}
}
}

The Trigger class is declared into partial (class partial). The class partial will allow to divide the clase into several files what will make it possible to facilitate the development, the tests and maintenance.

partial public class Triggers
{

}

One places an attribute to specify in Sql Server that it is a procedure.

The attribute takes several parameters:
- Name: the name of the object
- Target: on which table the object acts
- Vent: on which event: FOR INSERT, FOR UPDATE, FOR DELETE, FOR CREATE COUNTS...

[ Microsoft.SqlServer.Server. SqlTrigger (Name = "MonTrigger" , Target = "project" , Vent = "FOR UPDATE" ) ]
public static void Trigger1()
{

}

When you are carried out a procedure, a trigger, a function CLR within SQL Server 2005, the code operates in the context of a preestablished connection. SqlContext represents this connection. It provided various objects like SqlPipe, SqlResultSet, SqlTransaction, Sql TriggerContext....

Sql TriggerContext will determine the cause of release of the trigger.

SqlTriggerContext monTrig = SqlContext TriggerContext;
SqlPipe sqlPip = SqlContext Pipe;

Then it is checked if the trigger detected Update

yew (monTrig.TriggerAction == TriggerAction Update)
{

}

One recovers connection into short.

using ( SqlConnection conn = new SqlConnection ( "context connection=true;" ))

Then one carries out the request insert in the Data base

using ( SqlCommand cmd = conn.CreateCommand())
{
try
{
cmd.CommandText = "INSERT INTO project (nomProjet) BEEN WORTH (getdate())" ;
conn.Open();
SqlContext Pipe.Send( "Result: " + cmd.ExecuteNonQuery().ToString());
}
wrestling ( Exception ex)
{
SqlContext Pipe.Send( "Error" + ex.Message);
}
finally
{
conn.Close();

 

3.2. To deploy the assemblie in SQL Server 2005

Then you must deploy your DLL,

Click right on the project > Deploy

sql_clr_13.jpg

To rock in Sql Server Management Studio (in the past undertaken to manage)

Make Refresh of your data base

sql_clr_17.jpg

In the tree structure of your data base: DataBase > > VotreTable > > Triggers. Your trigger was well spread.

3.3 Test of Trigger

 

sql_clr_16.jpg

By carrying out this script you must see that a line it is created in the same table with the date and hour of the day.

The test is finished.



Articles de la même catégorie

 Pages : Top


20 Visites
0 Commentaires
Théorie du vectorielle
[25 mn de lecture - paru le 10/29/2005 4:41:28 PM - Public : Expert]

En savoir plus


11 Visites
0 Commentaires
Prevent hacker attacking company database
[10 mn de lecture - paru le 10/29/2005 4:32:47 PM - Public : ÐÂÊÖ]

En savoir plus


15 Visites
0 Commentaires
What can make Windows server 2003 more secure
[10 mn de lecture - paru le 10/29/2005 1:12:50 PM - Public : ÐÂÊÖ]

En savoir plus

   Tous les Articles


SUPINFO Training Center peut vous proposer une formation ...

   Devenez Ingénieur Système Microsoft en 35 jours avec SUPINFO Training Center
   Devenez Certifiés Cisco en 13 jours avec SUPINFO Training Center
   Devenez Administrateur Système Microsoft avec SUPINFO Training Center
   Devenez Développeur Microsoft .NET en 13 jours avec SUPINFO Training Center



Powered by Campus-Booster Technology
Conditions d'utilisation & Copyright | Respect de la vie privée
© Copyright 1965-2006 Supinfo Paris, Paris Academy of Computer Science
Supinfo, Ecole Supérieure d'Informatique et Paris Academy Of Computer Science are trade marks.
23, rue de Château LANDON - 75010 PARIS - Phone : +33 (0) 153359 700 Fax : +33 (0) 153359 701

Web site autided by :