Can someone post sample code or tell me how to set up a listener to notify me (trigger an event) if a new row is inserted into a SQL Server database?
I don't want to use any of the SQL Server messaging or broker services. I currently have a file listener which will notify me when new rows are appended to a flat file. I would like to do the same for a database table.
Are you perhaps looking for the SqlDependency class, that lets your code register to be notified when changes occur?
Or does this fall within the realm of things that you're disallowing, it's not entirely clear?
Trigger sounds best option:
You can use sql notifications to do this but you said you don't want to use the broker. Otherwise you can poll, but as mentioned this can cause performance issues.
Another way to do this is using triggers on the database table to touch a file on the filesystem.
Then use a FileSystemWatcher as you mentioned you are already doing in your application to receive a notification when the file changes.
There are security permissions that you will need to grant to your Sql Server user to make this possible but if that is acceptable then this will work without using the broker.
I also recommend trigger that registers inserts somewhere in db (and you must poll database anyway, which is not so expensive if the trigger modifies a special table with a single row). But if you have IDENTITY on your primary key, you can monitor the value of current identity of the table:
It's a hack, however, but it's fast and you don't need to modify your database (of course, it works only for insert operations). However you may skip insert operations if between polls identity value was modified explicitly and was set exactly to the value it was at the moment of previous polling (which is not very probable though).
As per "Damien" you need to use the sqldependency to detect changes.
sample for sqldependency at msdn:- https://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx
You would need to do some sort of database trigger on insert or poll the database regularly to look for new records. I don't recommend the latter suggestion as that can be very performance intensive.
Other than that, you aren't giving us much to go on. What version of SQL Server are you using? What have you tried already? What problems have you encountered?
Here are a few links that might point you in the right direction:
Exploring SQL Server Triggers
How to: Create and Run a CLR SQL Server Trigger
A similar question that suggests an alternative (better?) way of doing this: Can SQL CLR triggers do this? Or is there a better way?