Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# / SQL Listener to tell me if a row has been inserted into a table

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.

like image 384
greg Avatar asked Jun 15 '11 22:06

greg


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Are you perhaps looking for the SqlDependency class, that lets your code register to be notified when changes occur?

SqlDependency is ideal for caching scenarios, where your ASP.NET application or middle-tier service needs to keep certain information cached in memory. SqlDependency allows you to receive notifications when the original data in the database changes so that the cache can be refreshed.

Or does this fall within the realm of things that you're disallowing, it's not entirely clear?

like image 164
Damien_The_Unbeliever Avatar answered Nov 11 '22 12:11

Damien_The_Unbeliever


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?

like image 40
Kyle Trauberman Avatar answered Nov 11 '22 14:11

Kyle Trauberman