SqlDependency不会触发OnChange事件

时间:2012-05-11 17:05:45

标签: c# sql-server sql-server-2008 sqldependency

编辑:我修正了它。我所做的是将数据库的所有者更改为与我用来连接它的用户不同的东西。检查数据库所有者:

select name, suser_sname(owner_sid) from sys.databases

要更改所有者:

ALTER AUTHORIZATION ON DATABASE::ISS TO sa;

这就是它,我仍然不知道这个的原因,但是它的确如此,很酷。 (也许以前的dbowner缺少一些权利?)

这让我头疼太久了,我完全不知道为什么会这样。长话短说,我接受了Mark Nischalke的项目:http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events。该项目有一个SQL脚本来创建数据库,并在他的项目/数据库上触发SqlDependecy。现在我知道我以前的数据库存在问题,我复制了他的脚本并创建了我自己的数据库。除了表格(显然)之外,一切都是相同的。这意味着我的SqlDependency应该工作,因为我有一个相同配置的数据库到另一个工作;但没有......它不起作用。此时,在 my 项目中,我更改了SqlDependecy以检查他的数据库是否有通知,并且它可以工作,所以这不是我项目中的.NET问题。然后我尝试在他的项目中检查我的数据库中的SqlDependency并且nope仍然不起作用。在这两种情况下,我使用相同的连接字符串,集成安全性但不同的初始目录。谢谢你的帮助。

(另请注意,MessageQueue是在我的数据库上创建的,当我关闭项目SqlDependecy.Stop时删除了,但事件不是'触发'

这是他的数据库创建脚本

USE [master]
GO
/****** Object:  Database [Chatter]    Script Date: 11/18/2005 13:55:20 ******/
CREATE DATABASE [Chatter] ON  PRIMARY 
( NAME = N'Chatter', FILENAME = N'D:\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\Chatter.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'Chatter_log', FILENAME = N'D:\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\Chatter_log.ldf' , SIZE = 1536KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 COLLATE SQL_Latin1_General_CP1_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'Chatter', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Chatter].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [Chatter] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [Chatter] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [Chatter] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [Chatter] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [Chatter] SET ARITHABORT OFF 
GO
ALTER DATABASE [Chatter] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [Chatter] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [Chatter] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [Chatter] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [Chatter] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [Chatter] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [Chatter] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [Chatter] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [Chatter] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [Chatter] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [Chatter] SET  ENABLE_BROKER 
GO
ALTER DATABASE [Chatter] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [Chatter] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [Chatter] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [Chatter] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [Chatter] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [Chatter] SET  READ_WRITE 
GO
ALTER DATABASE [Chatter] SET RECOVERY FULL 
GO
ALTER DATABASE [Chatter] SET  MULTI_USER 
GO
ALTER DATABASE [Chatter] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [Chatter] SET DB_CHAINING OFF 

/***************************************************/

USE [Chatter]
GO
/****** Object:  Table [dbo].[Message]    Script Date: 11/18/2005 13:56:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Message](
    [int] [bigint] IDENTITY(1,1) NOT NULL,
    [Message] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Person_ID] [int] NOT NULL,
 CONSTRAINT [PK_Message] PRIMARY KEY CLUSTERED 
(
    [int] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

USE [Chatter]
GO
/****** Object:  Table [dbo].[Person]    Script Date: 11/18/2005 13:56:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

USE [Chatter]
GO
/****** Object:  StoredProcedure [dbo].[usp_GetMessages]    Script Date: 11/18/2005 13:59:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[usp_GetMessages]

AS

SELECT [Message], [Name]
FROM dbo.[Message]
JOIN dbo.Person ON id = [Message].Person_ID

USE [Chatter]
GO
/****** Object:  StoredProcedure [dbo].[usp_InsertMessage]    Script Date: 11/18/2005 13:59:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[usp_InsertMessage]
    @Message nvarchar(200),
    @Person_ID int
AS

INSERT INTO [Message] ([Message], Person_ID)
VALUES (@Message, @Person_ID)
GO
/****************************************/
INSERT INTO [Chatter].[dbo].[Person]
([Name])
VALUES('Larry')
GO
INSERT INTO [Chatter].[dbo].[Person]
([Name])
VALUES('Moe')
GO
INSERT INTO [Chatter].[dbo].[Person]
([Name])
VALUES('Curly')  

这是我的数据库创建脚本(我刚用“ISS”替换了所有“Chatter”)

USE [master]
GO
/****** Object:  Database [ISS]    Script Date: 11/18/2005 13:55:20 ******/
CREATE DATABASE [ISS] ON  PRIMARY 
( NAME = N'ISS', FILENAME = N'D:\iss.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'ISS_log', FILENAME = N'D:\iss_log.ldf' , SIZE = 1536KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
 COLLATE SQL_Latin1_General_CP1_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'ISS', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ISS].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [ISS] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [ISS] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [ISS] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [ISS] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [ISS] SET ARITHABORT OFF 
GO
ALTER DATABASE [ISS] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [ISS] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [ISS] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [ISS] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [ISS] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [ISS] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [ISS] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [ISS] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [ISS] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [ISS] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [ISS] SET  ENABLE_BROKER 
GO
ALTER DATABASE [ISS] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [ISS] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [ISS] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [ISS] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [ISS] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [ISS] SET  READ_WRITE 
GO
ALTER DATABASE [ISS] SET RECOVERY FULL 
GO
ALTER DATABASE [ISS] SET  MULTI_USER 
GO
ALTER DATABASE [ISS] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [ISS] SET DB_CHAINING OFF 

USE [ISS]

create table sectii (
    cods int identity(0,1) primary key,
    denumire varchar(30) not null,
)         

create table useri (
    codu int identity(0,1) primary key,
    login varchar(20) not null,
    password varchar(30) not null,
    cods int foreign key references sectii(cods),
    tip int not null,
)

create table medicamente (
    codm int identity(0,1) primary key,
    denumire varchar(50) not null
)

create table comenzi (
    codc int identity(0,1) primary key,
    cods int foreign key references sectii(cods),
    data datetime not null,
    codu int foreign key references useri(codu), --nu e nevoie
    onorata bit
)

create table medicamente_comanda (
    codc int foreign key references comenzi(codc) ON DELETE CASCADE,
    codm int foreign key references medicamente(codm),
    cantitate int
)                            

2 个答案:

答案 0 :(得分:1)

您的基础表结构中可能包含不正确的数据类型或其他一些非法的问题。我建议查看这篇文章,并确保符合“支持的SELECT语句”部分下的所有条件:

http://msdn.microsoft.com/en-us/library/ms181122%28v=sql.105%29.aspx

答案 1 :(得分:0)

确保您已经运行

ALTER DATABASE kmsdta SET ENABLE_BROKER;

如果此语句挂起,请使用此启用代理

alter database kmsdta set enable_broker with rollback immediate;

此外,您确保在Dababase中运行此功能

CREATE QUEUE ContactChangeMessages;

CREATE SERVICE ContactChangeNotifications
  ON QUEUE ContactChangeMessages

以及使用SqlDependency监视表时需要考虑的一些重要事项

  1. 在上面的代码中,您会注意到我的SQL查询不使用" "通配符返回所有列。您必须返回所需的确切列。如果您使用" ",则会产生不良后果。

  2. 同样在前面的代码中,您会注意到我的SQL查询包含"两部分"表名。这也是必需的。仅使用" TableName"而不是" owner.TableName"也会造成不良后果。

  3. 简单查询的示例:

    select OrderID from dbo.[DTAOrders];