c# - 当新记录插入数据库时​​触发 Windows 服务

标签 c#

<分区>

Possible Duplicate:
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 名称。

最佳答案

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

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

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

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

[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 是表的名称

关于c# - 当新记录插入数据库时​​触发 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874699/

相关文章:

c# - Roslyn:如何用表达式替换标识符名称

c# - 依赖倒置。对象创建

c# - 如何使用多线程 C# 应用程序在 Redis 中插入数百万个键/值

javascript - 使用Jquery检索YouTube标签

c# - 如何从kinect深度数据中确定 "real world"x,y坐标?

c# - 数据表添加数据行时出现错误[]

c# - 创建一个 Web 服务器,用一条简单的消息来响应每个传入的请求

c# - 在标签助手中进行上下文关联

c# - 获取对象处置/销毁的通知

c# - 在大列表中搜索要删除的项目