Well my windows service has to send out automated emails when the sql database has been updated.
How Exactly would i go about doing this? any code or tutorials would really help me
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Solution 1 – Using sp_send_dbmail
Here is an example of creating a trigger that sends an email when an INSERT/UPDATE/DELETE event occurs on a specific table:
Source: http://msdn.microsoft.com/en-us/library/ms189799.aspx
sp_send_dbmailwas introduced in SQL Server 2005. More info: http://msdn.microsoft.com/en-us/library/ms190307.aspxNote:
Solution 2 – Using xp_cmdshell
If you can’t set up Database Mail you have another option:
xp_cmdshell.With it you can run command line commands within SQL statements e.g. a small email sending tool.
This one is a small example how to send emails using
System.Net.Mailin a C# application: http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspxHow to use
xp_cmdshell: http://msdn.microsoft.com/en-us/library/aa260689%28v=sql.80%29.aspxSo you create a small C# console app that sends email then you execute it with
xp_cmdshellright from your SQL statement.Solution 3 – Using a windows service (as he wants)
A Windows Service can’t determine by itself whether a rows gets updated in an MSSQL database. You need to log the changes. To do so you may create an trigger for a specific table to record changes. By recording changes I mean inserting a new row into a
logtable withing the trigger like this:Creating a Windows Service Project is as easy as creating a Console Application using Visual Studio.
Your service will then read the ‘log’ table like every minute and send out emails if there were any rows in it (and deletes them of course).
It is possible to determine what kind of change happened: INSERT, UPDATE or DELETE. See the comments on this site for more details: http://msdn.microsoft.com/en-us/library/ms189799.aspx