Solved

C#/SQL Server Triggers

Posted on 2004-04-23
6
409 Views
Last Modified: 2011-10-03
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?

Cheers
Ben
0
Comment
Question by:bjohns33
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10905475
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?
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10905632
0
 
LVL 1

Author Comment

by:bjohns33
ID: 10906542
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.

Rama

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.

Thanks
Ben
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:bjohns33
ID: 10906573
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!
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 500 total points
ID: 10909734
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.
0
 
LVL 1

Author Comment

by:bjohns33
ID: 10910958
Thanks Joe, I think xp_cmdshell is exactly what I need.

Cheers
Ben
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now