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. 前端必用正则(js)

    手机号 /^1((3[\d])|(4[5,6,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[1-3,5-8])|(9[1,8,9]))\d{8}$/ 大写字母 /^[A- ...

  2. java生成快递单并调用打印机打印

    生成快递单过程中需要生成条形码,生成条形码可参考:https://www.cnblogs.com/linbky/p/12091248.html 下面这段代码生成的快递单是完全符合京东快递的10 x 1 ...

  3. Centos7安装Nginx1.14.0

    一.官网下载 http://nginx.org/en/download.html 版本说明: Nginx官网提供了三个类型的版本 Mainline version:Mainline 是 Nginx 目 ...

  4. php strnatcasecmp()函数 语法

    php strnatcasecmp()函数 语法 作用:使用"自然"算法来比较两个字符串(不区分大小写):直线电机优势 语法:strnatcasecmp(string1,strin ...

  5. 【bzoj1185】[HNOI2007]最小矩形覆盖 (旋转卡壳)

    给你一些点,让你用最小的矩形覆盖这些点 首先有一个结论,矩形的一条边一定在凸包上!!! 枚举凸包上的边 用旋转卡壳在凸包上找矩形另外三点... 注意精度问题 #include<cstdio> ...

  6. tab切换中的滚动条下拉分页带来的问题

    相信做过tab切换中滚动条下拉分页的童鞋都知道,我们在用scroll方法来做滚动条下拉分页的时候,都是有bug,切换中间的内容会互相影响,为了解决这个问题,我总结了2种方法: 1.方法一: <! ...

  7. 【CF1238E】Keyboard Purchase(状压DP,贡献)

    题意:有m种小写字符,给定一个长为n的序列,定义编辑距离为序列中相邻两个字母位置差的绝对值之和,其中字母位置是一个1到m的排列 安排一种方案,求编辑距离最小 n<=1e5,m<=20 思路 ...

  8. BUUCTF | CODE REVIEW 1 (反序列化,md5绕过)

    <?php /** * Created by PhpStorm. * User: jinzhao * Date: 2019/10/6 * Time: 8:04 PM */ highlight_f ...

  9. SyntaxError: missing ] after element list

    在前端页面js报错,找了很久没找到原因. 后来发现是后台向前端输出json字符串,而前端接收是html格式,需要将后台json字符串改成正常字符串就可以输出,或者通过ajax的方式接收json字符串.

  10. Laex/Delphi-OpenCV

    https://github.com/Laex/Delphi-OpenCV 66 Star119 Fork75 Laex/Delphi-OpenCV CodeIssues 3Pull requests ...