One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Thursday, July 12, 2012

Fire Alarm When You Insert a Row to SQL Table

First you have to write a particular trigger for run the .exe file, when you insert a record to the data table.

Following is the Trigger


create trigger runexe
on tbDetails
for insert
As
Begin
print 'Inserted'
Exec Master..xp_cmdshell 'E:\Hiran\Alarm.exe'
End

Some SQL Server versions does not allowed to run the "xp_cmdshell" command. In that case you have to enable that feature by executing the following commands.


EXEC sp_configure ‘show advanced options’, 1

EXEC sp_configure ‘xp_cmdshell’, 1


After that you have to write a particular C# program to run the particular batch (.bat) file. The program.cs should look like below. We can run the batch file at once. But then, it will not be Asynchronous and we have to wait until finish the wave file to insert the record. Therefore the best way is this.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;


namespace Alarm
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            System.Diagnostics.Process.Start("cmd.exe", @"/c E:\Hiran\aaa.bat");
            Application.Exit();
        }
    }
}

Then open the notepad and write the below cording. After that save it using .bat extension.

sndrec32 /play /close "E:\Hiran\SirenSound.wav"


Now You are done. Please comment if you need any help.
Thank You.









1 comment: