有人请说明CLR Trigger支持.Net 4.0吗?

时间:2012-01-17 04:40:57

标签: c# sql-server sqlclr

此主题是以下主题的延续 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)。

当我尝试进入不工作状态时,它表示部署成功并且输出窗口显示“已取消用户”。我不确定幕后会发生什么。

请有人告诉我如何在插入新行时执行并查看一些结果。

2 个答案:

答案 0 :(得分:3)

不,它没有。 SQLCLR对.Net 4.0的支持仅在SQL Server 2012中提供。但是,您的Visual Studio知道更好,并将您的DLL编译和部署为.Net 2.0程序集。

除此之外,从触发器进行Web调用是非常糟糕的主意。您的数据库将停止等待来自WWW的响应。使用排队机制并从您自己的进程进行WWW调用,而不是从SQLCLR进行。

答案 1 :(得分:2)

不,Sql Server 2005-2008R2不支持.NET4程序集