Lord Vermillion
Lord Vermillion

Reputation: 5424

C# LINQ detect insert

I have a Web API where i want to trigger a function when a new row has been inserted to a DB table. I don't want to have to get all the data and look for new data manually.

Is it possible to have a SQL Server trigger that somehow calls a function in my C# Web API?

How can this be done? Is it a good idea?

Upvotes: 3

Views: 341

Answers (2)

Sid
Sid

Reputation: 14896

You can override SubmitChanges in your DataContext, something like:

public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{

    var set = this.GetChangeSet();//list of pending changes

    //Call your methods here
    foreach (var insert in set.Inserts)
    {
        //Insert Logic 
    }
    foreach (var update in set.Updates)
    {
        //Update Logic 
    }
    foreach (var item in set.Deletes)
    {
        //Delete Logic
    }


    base.SubmitChanges(failureMode);//allow the DataContext to perform it's default work (including your new log changes)

}

Upvotes: 4

Tinwor
Tinwor

Reputation: 7973

Are you sure that you need a Trigger? Why not choose a pub/sub system (such as rabbitmq, zeromq, redis...) where you can create a channel and every client pubblish and subscribe to it?
But if you prefer use a trigger here you can find two document on how to consume a sql server (link1 and link2)

Upvotes: 0

Related Questions