首页 > 代码库 > CLR触发器

CLR触发器

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


public partial class Triggers
{
    // 为目标输入现有表或视图并取消对属性行的注释
    [Microsoft.SqlServer.Server.SqlTrigger (Name="Trigger1", Target="Unit_E", Event="FOR INSERT")]
    public static void Trigger1()
    {
        SqlPipe sp = SqlContext.Pipe;
        string sql = "insert into Unit_E1 select * from inserted";
        using (SqlConnection conn = new SqlConnection("context connection=true"))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            cmd.CommandText = sql;
            SqlDataReader rdr = cmd.ExecuteReader();
            sp.Send(rdr);
            conn.Close();
        }
        //SqlContext.Pipe.Send("Trigger FIRED");
    }
}

CLR触发器