如何清理SQL Server以清除过期的SqlDependency对象?从SqlDepedency对象收到事件后,我需要创建一个新事件才能获得新事件.但是,SQL Server进程的内存使用量会增加,直到超出允许的内存(SQL Server Express).如何摆脱旧查询?
码:
// Func: RegisterTableListener
using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.DatabseEventConnectionString))
{
if (cmd == null)
{
cmd = cn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT HostName, LastStatus, LastDetails, xml FROM dbo.[SystemTable]";
}
lock (cmd)
{
cmd.Connection = cn;
cn.Open();
cmd.Notification = null;
// creates a new dependency for the SqlCommand
if (dep == null)
dep = new SqlDependency(cmd);
// creates an event handler for the notification of data
// changes in …Run Code Online (Sandbox Code Playgroud) 可能重复:
使用Sql Server 2008更改通知
我只是想知道无论如何我可以在C#中编写一个Windows服务,当新记录插入数据库时将触发该服务.
我想通过wcf连接DB.请提出任何想法或建议.
提前致谢.
基于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 = …Run Code Online (Sandbox Code Playgroud) 使用.Net 4.0和SQL Server 2008 R2
我一直在寻求利用SqlDependency/SqlNotifications来监控一些数据库结构.这是将SqlDependency/SqlNotifications与SQL Server Service代理一起使用的最佳过程/实践吗?
人们是否使用其他方法来完成类似的任务?我总是可以查看更改,但我宁愿不必这样做.另一方面,我也不想在我们现有的环境中引入复杂的场景.
编辑(潜在选项):
我有一个WCF服务,当数据库发生更改时需要通知它的客户端(sql server 2005).只要我找到一种方法来通知我的服务任何更改,这相对容易完成.我可以在表上创建一个数据库触发器并让该触发器启动一个通知我的服务的小服务客户端,但我想知道是否有更好的方法来执行此操作?让服务轮询数据库进行更改是一个可行的解决方案,但我不确定最好的方法(并向我的服务发送通知将是首选).
由于相关更新仅适用于数据库的某个部分,我还想知道是否也可以将这样的触发器(或其他机制)链接到数据库图表.
所有帮助表示赞赏!rinze
我WebAPI用来将一些数据推送到数据库.
在同一服务器中运行的Windows服务必须在数据插入时得到通知 WebAPI
我已经看到这个问题建议使用Service Broker或者SqlDependency问题似乎是在不久前提出来的.
我google了但找不到更简单的解决方案.我想做的就是when a new data is inserted to Database, a function in the the windows service should fire.
我们在WebAPI - Windows Services上下文中是否有任何替代方案或类似的解决方案?