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

发布于 2024-12-27 20:15:03 字数 1887 浏览 2 评论 0原文

该线程是以下线程的延续 当新记录插入到时触发 Windows 服务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)。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2025-01-03 20:15:03

不,事实并非如此。 SQLCLR 对 .Net 4.0 的支持仅在 SQL Server 2012 中提供。但是您的 Visual Studio 对此了解得更清楚,并且会将您的 DLL 编译和部署为 .Net 2.0 程序集。

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

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.

一花一树开 2025-01-03 20:15:03

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

No, Sql Server 2005-2008R2 not supporting .NET4 assemblies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文