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. IntelliJ IDEA设置maven

    1.更改默认的maven仓库 2.手动更新maven 项目——也就是下载依赖的jar包 3. 不想每次手动更新,设置IDEA自动更新mav项目,下载jar包

  2. mybatis 动态Sql的模糊查询

    where teacher.tname like concat(concat(#{tName}),'%') 2:distinct的使用 下面先来看看例子: table    id name    1 ...

  3. 第一次用angularJS做后台管理点滴

    很早以前就大概看过一点angualrjs,但是没有项目,一直没有进行下去,就是干巴巴的看着,过了一段时间发现什么也不记得了. 来yulebaby我的第一个后台管理是用easyui做的,做完那个以后发现 ...

  4. JPush极光推送Java服务器端实例

    import cn.jpush.api.JPushClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.j ...

  5. 【HDOJ6602】Longest Subarray(线段树,vector)

    题意:给定一个长为n的序列,第i个数a[i]都是一个[1,c]中的整数 如果一段序列[l,r]中出现过的数字出现次数都>=K则称其为好的序列 求最长的好的序列的长度 n,k,c,a[i]< ...

  6. AT2000 Leftmost Ball(计数dp+组合数学)

    传送门 解题思路 设\(f[i][j]\)表示填了\(i\)个白色,\(j\)种彩色的方案数,那么显然\(j<=i\).考虑这个的转移,首先可以填一个白色,就是\(f[i][j]=f[i-1][ ...

  7. angualr6 引入iframe

    项目开发中需要在angular项目中嵌入iframe窗口,上网搜索了相关文档,不是很多,但是总算是把功能实现了,现记录一下,便于后期查看: step1:在.html中放入需要承载内容的div,并定义好 ...

  8. ibatis 的使用

    1. 文本的使用 select  ‘day’+Num from Table;//Sql select convert(varchar,'day')+Num from Table;//ibatis

  9. arcgis api for javascipt 加载天地图、百度地图

    写在前面的话: 1.百度地图是自己定义的坐标系统,wkid=102100.百度地图数据是加密的产物.下文将附上百度坐标与WGS84,谷歌等坐标系统转换方法(地理-地理),此方法并未亲测,据说准 2.百 ...

  10. TList TObjectList的区别和使用

    所在的单元 TList(Classes.pas) TObjectList(Contnrs.pas) TObjectList对象的创建方法有一个参数: constructor TObjectList.C ...