这个线程是下面线程的延续 Trigger Windows Service when the new record insert in to DB
我从Demo.B得到了建议的代码
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");
}
}
}
}
我在开始执行时遇到了一些问题,首先它要求我将版本从 4.0 更改为更低版本(因此在属性中将版本更改为 3.5)。
当我尝试进入它不工作时,它一直说部署成功并且输出窗口显示“已被用户取消”。我不确定幕后发生了什么。
请有人告诉我如何在插入新行时执行并查看一些结果。
最佳答案
不,它没有。 SQLCLR 对 .Net 4.0 的支持仅出现在 SQL Server 2012 中。但是您的 Visual Studio 知道得更多,并且会将您的 DLL 编译和部署为 .Net 2.0 程序集。
除此之外,从触发器进行 Web 调用是一个非常糟糕的主意。您的数据库将逐渐停止等待来自 WWW 的响应。使用队列机制并从您自己的进程而不是 SQLCLR 进行 WWW 调用。
关于c# - 有人请澄清一下 CLR 触发器是否支持 .Net 4.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8889737/