<分区>
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 名称。