Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call a C# function by a SQL Server trigger?

An external application come to my database for inserting row in a Sql server table.

I have a web application on my own, and I want on each insert in this table, do some treatement server side.

My idea is to create a trigger on insert on the table, and then call appropriate function.

What is the best way to do this ?

I use framework 3.5 with LINQ to SQL, and a SQL Server 2005 database.

Edit : Thank you for the answers. SQL Server CLR integration doesn't do it. The few libraries supported doesn't meet me requirement.

The table I have to log will take a new record every 5 minutes perhaps, not so much. Maybe I can have a job listening at the table every minute, look at the ID, take all the new ID, do my treatement. Is my solution not too ugly ?

like image 395
Cyril Gandon Avatar asked Jan 27 '11 07:01

Cyril Gandon


People also ask

Can you call C from Python?

We can call a C function from Python program using the ctypes module.

Can you call C from C++?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.

Can C and C++ be mixed?

The C++ language provides mechanisms for mixing code that is compiled by compatible C and C++ compilers in the same program. You can experience varying degrees of success as you port such code to different platforms and compilers.

How do you call C from R?

The most basic method for calling C code from R is to use the . C() function described in the System and foreign language interfaces section of the Writing R Extensions manual. Other methods exist including the . Call() and .


2 Answers

Use SQL Server CLR integration.

like image 63
Mikael Eriksson Avatar answered Sep 20 '22 00:09

Mikael Eriksson


If your function is going to take a long time to run, or going to access resources which aren't part of the same database, you might want to consider decoupling the function call from the trigger (so that the original statement that caused the trigger to fire can complete).

In that case, you might want to look at Service Broker, or just use a separate table to queue the requests to call the function (and use a SQL Agent job to dequeue these requests and call the function).

like image 25
Damien_The_Unbeliever Avatar answered Sep 20 '22 00:09

Damien_The_Unbeliever