1   启用当前数据库的 SQL Server Service Broker

a   检查Service Broker 是否开启

SELECT is_broker_enabled FROM sys.databases WHERE name = 'WLZhuJianMes' 

查询结果:is_broker_enabled de 结果是  0,代表数据库没有启动 Service Broker

解决办法:注:两句同时执行,单独执行显示:正在回滚不合法事务。估计回滚已完成: 100%。

b 开启 Service Broker

ALTER DATABASE WLZhuJianMes SET NEW_BROKER WITH ROLLBACK IMMEDIATE; 

ALTER DATABASE WLZhuJianMes  SET ENABLE_BROKER; 

再次查询is_broker_enabled状态,状态为1,数据库没有启动 Service Broker成功

光用ALTER DATABASE DBName SET ENABLE_BROKER;语句开启经常会死机卡住,解决这个问题的方法是,先停止其它正在使用数据库的程序,然后运行

ALTER DATABASE DBName SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

ALTER DATABASE DBName SET ENABLE_BROKER;
在数据库中停用 Service Broker
将数据库改为设置 DISABLE_BROKER 选项。
示例 复制
USE master ;
GO ALTER DATABASE AdventureWorks2008R2 SET DISABLE_BROKER ;
GO

2   新建 Windows 服务程序 ,设置服务的相关信息

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace DamonService
{
public partial class Service1 : ServiceBase
{
static string _connStr = ConfigurationManager.AppSettings["Connection"].ToString();
public Service1()
{
InitializeComponent();
this.WriteLog("初始化");
this.WriteLog(_connStr);
SqlDependency.Start(_connStr);//传入连接字符串,启动基于数据库的监听
// 先手动启动一次
UpdateGrid();
} protected override void OnStart(string[] args)
{
this.WriteLog("服务开始启动");
} protected override void OnStop()
{
this.WriteLog("服务停止");
}
private void UpdateGrid(int sync)
{
try
{
using (SqlConnection connection = new SqlConnection(_connStr))
{
//依赖是基于某一张表的,而且查询语句只能是简单查询语句,
//不能带top或 *,同时必须指定所有者,即类似[dbo].[]
using (SqlCommand command = new SqlCommand(@"SELECT [Mid] ,[MatTitle] FROM [dbo].[IPGMatter] where issend=0", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); SqlDataReader sdr = command.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
if (sync == )
{
// sdr.
string id = sdr["MatTitle"].ToString();
this.WriteLog(id);
}
}
sdr.Close();
}
}
}
catch (Exception ex)
{
WriteLog("UpdateGrid:" + ex.StackTrace);
}
} #region 监听
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= dependency_OnChange;
if (e.Info == SqlNotificationInfo.Insert)
{
WriteLog("UpdateGrid:" + "Insert");
UpdateGrid();
}
else if (e.Info == SqlNotificationInfo.Update)
{
UpdateGrid(-);
}
else if (e.Info == SqlNotificationInfo.Delete)
{
UpdateGrid();
}
else
{
UpdateGrid();
}
}
#endregion #region 日志
private void WriteLog(String message)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serverLog.txt");
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
using (FileStream fs = File.Create(path))
{
fs.Close();
}
}
using (FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fileStream))
{
sw.WriteLine(DateTime.Now.ToString() + ":" + message);
}
} } #endregion
}
}

安装和卸载服务的脚本

保存为bat文件,放在服务程序的根目录

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe  /i DamonService.exe

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe  /u DamonService.exe

Windows服务 --- SqlDependency的使用的更多相关文章

  1. C#曲线分析平台的制作(六,Sqldependency+Signalr+windows 服务)

    在经过五天的学习和资料收集后,终于初步实现了利用sqldependency进行数据库变化监控,signalr进行前后台交互,数据实时更新.下面将源代码贴出进行初步分析: 1.系统整体框架构成: 2.具 ...

  2. C#曲线分析平台的制作(五,Sqldependency+Signalr+windows 服务 学习资料总结)

    在前篇博客中,利用interval()函数,进行ajax轮询初步的实现的对数据的实时显示.但是在工业级别实时显示中,这并非是一种最好的解决方案.随着Html5 websocket的发展,这种全双工的通 ...

  3. 基于SignalR实现B/S系统对windows服务运行状态的监测

    通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...

  4. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  5. 玩转Windows服务系列汇总

    玩转Windows服务系列汇总 创建Windows服务 Debug.Release版本的注册和卸载及其原理 无COM接口Windows服务启动失败原因及解决方案 服务运行.停止流程浅析 Windows ...

  6. 玩转Windows服务系列——给Windows服务添加COM接口

    当我们运行一个Windows服务的时候,一般情况下,我们会选择以非窗口或者非控制台的方式运行,这样,它就只是一个后台程序,没有界面供我们进行交互. 那么当我们想与Windows服务进行实时交互的时候, ...

  7. 玩转Windows服务系列——使用Boost.Application快速构建Windows服务

    玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...

  8. 玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理

    Windows服务Debug版本 注册 Services.exe -regserver 卸载 Services.exe -unregserver Windows服务Release版本 注册 Servi ...

  9. C# 开发windows服务的一些心得

    最近在做一个windows服务的项目,发现并解决了一些问题,拿出来和大家分享一下,以下windows服务简称“服务” 文章会在适合时间更新,因为朋友们在不断提出新的意见或思路,感谢-.- 1.服务如何 ...

随机推荐

  1. vue 全局 js 方法

    1.新增 getCurrentDataType.js 文件 import cookieUtils from '@/config/cookieUtils' function getCurrentData ...

  2. 企业级Web服务器安全主动防御措施

    篇一 : 企业级Web服务器安全主动防御措施 Web服务器现在已经成为了病毒.木马的重灾区.不但企业的门户网站被篡改.资料被窃取,而且还成为了病毒与木马的传播者.有些Web管理员采取了一些措施,虽然可 ...

  3. PHP curl_copy_handle函数

    curl_copy_handle — 复制一个cURL句柄和它的所有选项 说明 resource curl_copy_handle ( resource $ch ) 复制一个cURL句柄并保持相同的选 ...

  4. 10.2-linux文件与目录管理

    1.1-目录的相关操作 . rm Remove (unlink) the FILE(s). -f, --force #强制删除 ignore nonexistent files, never prom ...

  5. HTML 3-列表、表单、语义化

    有序列表.无序列表.自定义列表在语义上的区别 1.有序列表ol>li:列表有顺序编号(默认列表前加1.,2.,3.,...编号),适用于有逻辑顺序的排列项目,而且次序不能乱.2.无序列表ul&g ...

  6. hdu 5517 Triple

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5517 ------------------------------------------------ ...

  7. 20150721—HTML的定位 JS (转)

    本文转载于:http://blog.csdn.net/xuantian868/article/details/3116442   HTML:scrollLeft,scrollWidth,clientW ...

  8. <读书笔记>《React:引领未来的用户界面开发框架》

    <React:引领未来的用户界面开发框架>(GitHub 附demo版) 1.Component的创建与复合 1.1 React简介 背景介绍,全书概览 1.本质上是一个状态机,它以精简的 ...

  9. Neural Network and Artificial Neural Network

    神经网络的基本单元为神经元neuron,也称为process unit,可以做一些基本的运算操作.   人脑和动物大脑的发育,依赖于经验的积累和学习.神经网络就是一个用来仿照人脑进行学习的机器,其包含 ...

  10. QTP 保留对象

    1. 常用保留对象(Utility Objects) 保留对象:所谓QTP保留对象就是QTP本身预留的一些可用对象. 通俗些讲就是,当打开QTP时它就已经把这些对象给实例化了,直到关闭QTP后,这些保 ...