This thread is the continuation of the below thread
Trigger Windows Service when the new record insert in to DB
I got the code from Demo.B suggested
public partial class Triggers
{
// Enter existing table or view for the target and uncomment the attribute line
[Microsoft.SqlServer.Server.SqlTrigger(Name = "Trigger_Web", Target = "StoryItems", Event = "FOR INSERT")]
public static void Trigger_Web()
{
SqlCommand command;
SqlTriggerContext triggerContext = SqlContext.TriggerContext;
SqlPipe pipe = SqlContext.Pipe;
SqlDataReader reader;
if (triggerContext.TriggerAction == TriggerAction.Insert)
{
using (SqlConnection connection = new SqlConnection(@"context connection=true"))
{
connection.Open();
command = new SqlCommand(@"SELECT * FROM StoryItems", connection);
reader = command.ExecuteReader();
reader.Read();
// get inserted value
string Name;
string Location;
Name = (string)reader[1];
Location =(string)reader[2];
reader.Close();
//try
//{
// // try to pass parameter to windows service
// //WindowsService param = new WindowService(Name, Location);
// //Do something if it pass
//}
//catch (Exception ex)
//{
//}
// Replace with your own code
SqlContext.Pipe.Send("Trigger FIRED");
}
}
}
}
I am facing few problem when i start execute,first of all it ask me change the version from 4.0 to lower (so changed the version to 3.5 in the properties).
When i try to step into its not working and all the time it says deploy succeed and the output window shows “Canceled by User”. Am not sure what going on the behind scene.
Please some one advise me how can i execute and see some results when the new row is inserted.
No, it does not. SQLCLR support for .Net 4.0 comes only in SQL Server 2012. However your Visual Studio knows better and will compile and deploy your DLLs as a .Net 2.0 assembly.
Other than that, doing Web calls from a trigger is a very bad idea. Your database will grind to a halt waiting on responses from WWW. Use a queueing mechanism and make the WWW call from process of your own, not from SQLCLR.