C#/SQL Server Triggers

Hi all

I want to run some C# code when a record is added to a SQL Server table - what's the best way to achieve this?

Who is Participating?
JoeisanerdConnect With a Mentor Commented:
well you could do that but you could also you use the Sql Server function xp_cmdshell

xp_cmdshell {'command_string'} [, no_output]

in your trigger you could call this and specify a .net command line app that you create that would do the stuff you need.
what is it that you want the c# code to do? triggers are nice in that it runs on the sql server.

Is your program the only program updating the database?
The code that needs to run when a row is inserted, is it database related?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

bjohns33Author Commented:
Hi Joe

There's an external process which is pretty much black-boxed that will insert records in to this database.  When the record appears I want the .NET event to be run.  This will have some interaction with the database - it will basically create an email, add some attachments (which are defined in the inserted record), and send it to the email address specified in the inserted record.

I've looked at Notification Services, but these a) seem to be overkill for this, b) don't support attachments out of the box and c) are a pain in the arse to configure.

So basically, I want a trigger that runs a program.  All of it will be on the same server.


Both of those links relate to creating standard database triggers, which are self-contained within the database - this needs to call an external programme as a result of the trigger.

bjohns33Author Commented:
A brute force solution would be to write a service that polls a view in the database every x seconds that just shows unprocessed records, but while this would be simple, it doesn't seem very elegant!
bjohns33Author Commented:
Thanks Joe, I think xp_cmdshell is exactly what I need.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.