【问题标题】:Trigger Windows Service when the new record insert in to DB [duplicate]当新记录插入数据库时​​触发 Windows 服务 [重复]
【发布时间】:2012-01-16 00:42:03
【问题描述】:

可能重复:
Change Notification with Sql Server 2008

我只是想知道我是否可以在 C# 中编写一个 Windows 服务,该服务将在新记录插入数据库时​​触发。

我也想通过 wcf 连接数据库。请提出任何想法或建议。

提前致谢。

根据demo.b指令,代码如下。

SQL 数据库详细信息


我的数据库名称:MyWeb,表格名称:StoryItems, 列:位置、标题、名称、类型。


 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
            // ***********Here am trying to retrieve the location and name column value            
            Location= (string)reader[9];
            Name= (String) reader[9];
            reader.Close();
            try
            {
                // try to pass parameter to windows service

                WindowsService param = new WindowService(InsertedValue1, InsertedValue2);
            }
            catch (Exception ex)
            {

            }



        // Replace with your own code
        SqlContext.Pipe.Send("Trigger FIRED");
      }
    }
}
}

它不喜欢列名,我不确定这里缺少什么。“Trigger_Web”是我的 CLR SP 名称。

【问题讨论】:

    标签: c#


    【解决方案1】:

    首先您需要在 Visual Studio 中创建一个触发器应用程序。

    文件 --> 新建 --> 项目 --> 数据库 --> 选择 V​​isual C# CLR 数据库项目。

    它将提示您连接到数据库。完成后,确保您的触发器应用程序监听您喜欢的任何表上的记录插入(您可以在 Visual Studios here 中阅读有关 CLR 应用程序的更多信息)。

    从上面链接中的步骤,添加一个触发器。你的方法应该是这样的:

    [Microsoft.SqlServer.Server.SqlTrigger(Name = "GetTransaction", Target = "EvnLog", Event = "FOR INSERT")]
    public static void GetTransaction()
    {
        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 INSERTED", connection);
                reader = command.ExecuteReader();
                reader.Read();
                // get inserted value
                InsertedValue1 = (DateTime)reader[0];
                InsertedValue2 = (string)reader[9];
                reader.Close();
                try
                {
                    // try to pass parameter to windows service
    
                    WindowsService param = new WindowService(InsertedValue1,InsertedValue2)
                }
                catch (Exception ex)
                {
    
                }
    
            }
    

    注意:GetTransaction 是您要创建的触发器的名称,在这种情况下,Evnlog 是表的名称

    【讨论】:

    • 我很欣赏你所展示的一个例子(这几乎解决了问题),但是......我想如果你只得到一个值,那么它应该是 SELECT TOP 1 * 否则SQL Server 可能会继续假脱机结果。 reader 应该是 .Dispose'd,而不仅仅是 .Close'd。
    • 非常感谢 demo.b,我的 Windows 服务在远程位置运行或我的 sql 服务器在远程位置运行会发生什么情况。在这种情况下,WCF 或任何服务是唯一的通信选项触发器,如果​​有错误请纠正我。
    • 当我尝试创建一个新项目时收到非常有趣的警告“注意:如果您的开发计算机上没有安装 .Net Framework 3.5 版,如果您想开发,则必须安装它用于 sql server 2005 或 Sql Server 2008 的 SQL CLR 程序集仅支持那些面向 .Net Framework 2.0、3.0 或 3.5 版本的程序集。您在项目属性中指定 .Net Framework 版本。按 F1 获取更多信息。"
    • 您的 Windows 服务从哪里运行并不重要。它可以与 sql server 位于同一台服务器上,也可以位于远程服务器上。如果它在同一个盒子上,在视觉工作室中,右键单击触发器项目,选择属性。在数据库设置下,将权限级别更改为“保存”。如果它是一个远程盒子,你将设置为“不安全”或“外部”。一旦你完成了触发器应用程序的开发,你需要通过右键单击并选择部署来部署到数据库。部署需要一些数据库特权。
    • 在部署到数据库之前,请阅读此内容以准备数据库geekswithblogs.net/rasyadi/archive/2005/11/18/60459.aspx。要回答另一个问题,您需要 vs 2008/2010 和 sql server 2005/2008
    【解决方案2】:

    在 SQL 服务器中使用类似扩展存储过程的东西,它调用您的 C# 类/可执行文件,然后可以执行服务。

    您还可以从表上的 on_insert 事件的触发器中调用命令行函数,这可以启动/停止服务,或运行 exe 或批处理文件。

    一些想法: http://www.sqlservercentral.com/Forums/Topic960855-392-1.aspx

    并且 http://msdn.microsoft.com/en-us/library/ms189799.aspx

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2016-01-30
      • 2014-01-26
      相关资源
      最近更新 更多