Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. CLR-Trigger coded in c# (Express Edition 2005)

CLR-Trigger coded in c# (Express Edition 2005)

Scheduled Pinned Locked Moved C#
databasehelpcsharpsql-serverdotnet
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jacklynn_mei
    wrote on last edited by
    #1

    Hi...:) I am newbie in c#, please help me on clr trigger. I have done a clr trigger and everytime i debug my c# code,an error occured on this part AIClass.AICheck.checkUserRole(); The error description is like this :-> InvalidOperationException was Unhandled The requested operation requires a SqlClr context, which is only available when running in the Sql Server process. I dont know what to do... And I cannot create a trigger inside Sql Server Express Edition 2005. I did like this:-> create trigger tgAI on AIEvent for insert as external name AIAssembly.AIClass.userRoleCheck and the error appeared:-> Msg 6505, Level 16, State 1, Procedure tgAI, Line 1 Could not find Type 'AIClass' in assembly 'AIClass'. my code in c# is like this (will be called in the main class in console App.):-> using System; using System.Collections.Generic; using System.Text; using Microsoft.SqlServer.Server; using System.Data; using System.Data.SqlClient; namespace AIClass { public class AICheck { //[SqlTrigger(Event = "FOR INSERT", Name = "AI_tg", Target = "AIEvent")] public static void checkUserRole() { SqlTriggerContext tgContext = SqlContext.TriggerContext; //SqlConnection conn = new SqlConnection("Data Source=DIMENSION3000\\SQLEXPRESS; Initial Catalog=AITania; User Id=sa; Password=123456"); using (SqlConnection conn = new SqlConnection("context connection=true")) conn.Open(); SqlCommand cmd = conn.CreateCommand(); SqlDataReader reader; string msg = ""; if (tgContext.TriggerAction == TriggerAction.Insert) { //SqlCommand sqlComm = new SqlCommand(); //SqlPipe sqlPipe = SqlContext.Pipe; cmd.CommandText = "SELECT * FROM INSERTED"; reader = cmd.ExecuteReader(); //sqlComm.Connection = conn; //sqlComm.CommandText = "SELECT * FROM INSERTED"; //for (int x = 0; x < tgContext.ColumnCount; ++x) //{ // msg += string.Format("Column {0} {1} been updated{2}", x, (tgContext.IsUpdatedColumn(x) ? "has" : "has not"), Environment.NewLine); //} for (int i = 0; i < reader.FieldCount;i++ ) { msg = msg + reader.GetName(i) + ":" + (string)reader[i] + " "; } }conn.Clos

    L 1 Reply Last reply
    0
    • J jacklynn_mei

      Hi...:) I am newbie in c#, please help me on clr trigger. I have done a clr trigger and everytime i debug my c# code,an error occured on this part AIClass.AICheck.checkUserRole(); The error description is like this :-> InvalidOperationException was Unhandled The requested operation requires a SqlClr context, which is only available when running in the Sql Server process. I dont know what to do... And I cannot create a trigger inside Sql Server Express Edition 2005. I did like this:-> create trigger tgAI on AIEvent for insert as external name AIAssembly.AIClass.userRoleCheck and the error appeared:-> Msg 6505, Level 16, State 1, Procedure tgAI, Line 1 Could not find Type 'AIClass' in assembly 'AIClass'. my code in c# is like this (will be called in the main class in console App.):-> using System; using System.Collections.Generic; using System.Text; using Microsoft.SqlServer.Server; using System.Data; using System.Data.SqlClient; namespace AIClass { public class AICheck { //[SqlTrigger(Event = "FOR INSERT", Name = "AI_tg", Target = "AIEvent")] public static void checkUserRole() { SqlTriggerContext tgContext = SqlContext.TriggerContext; //SqlConnection conn = new SqlConnection("Data Source=DIMENSION3000\\SQLEXPRESS; Initial Catalog=AITania; User Id=sa; Password=123456"); using (SqlConnection conn = new SqlConnection("context connection=true")) conn.Open(); SqlCommand cmd = conn.CreateCommand(); SqlDataReader reader; string msg = ""; if (tgContext.TriggerAction == TriggerAction.Insert) { //SqlCommand sqlComm = new SqlCommand(); //SqlPipe sqlPipe = SqlContext.Pipe; cmd.CommandText = "SELECT * FROM INSERTED"; reader = cmd.ExecuteReader(); //sqlComm.Connection = conn; //sqlComm.CommandText = "SELECT * FROM INSERTED"; //for (int x = 0; x < tgContext.ColumnCount; ++x) //{ // msg += string.Format("Column {0} {1} been updated{2}", x, (tgContext.IsUpdatedColumn(x) ? "has" : "has not"), Environment.NewLine); //} for (int i = 0; i < reader.FieldCount;i++ ) { msg = msg + reader.GetName(i) + ":" + (string)reader[i] + " "; } }conn.Clos

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You cant use that code outside the SQL environment, you will have to specify a proper SQL connection string.

      J 1 Reply Last reply
      0
      • L leppie

        You cant use that code outside the SQL environment, you will have to specify a proper SQL connection string.

        J Offline
        J Offline
        jacklynn_mei
        wrote on last edited by
        #3

        hi leppie.. hem..thanks for the suggestion...but still blur.. so..how do i supposed set the connection string properly.. whenever i debug my project..it came out like this... The Error: AIClass.AICheck.checkUserRole(); --> invalidOperationException was unhandled --> The requested operation requires a SqlClr context, which is only available when running in the Sql Server process. i have search at msdn help online but still cannot find the answer.. please help me... thanks a lot Jac

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups