c# - 有人请澄清一下 CLR 触发器是否支持 .Net 4.0?

标签 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)。

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

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

最佳答案

不,它没有。 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/

相关文章:

c# - Windows 手机 : RemoveBackEntry after navigate failing

c# - MySQL 相当于 MS SQL 代码

sql-server - 如何编写具有多条记录的动态列 PIVOT 查询?

visual-studio - 如何将旧的 Visual Studio 项目迁移到 Visual Studio 2017?

c# - SQL Server CLR 静态变量设置回 null

sql - 连续 N 行的最大总和

c# - 使用 C# 循环遍历 gridview 的所有行和列

c# - 如何创建文本文件并将其保存到共享目录?

sql - Select Join 在不同数据库中是否存在性能问题?

c# - 如何在 Dapper 中为多映射支持命名列?