Bogdan
Bogdan

Reputation: 682

SqlDependency doesn't fire OnChange event

EDIT: I Fixed it. What I did was change the owner of the database to something different then the user I used to connect to it. To check database owner:

select name, suser_sname(owner_sid) from sys.databases

To change owner:

ALTER AUTHORIZATION ON DATABASE::ISS TO sa;

This was pretty much it, I still don't know the reason for this, but it works so yeah, cool. (Maybe the previous dbowner was missing some rights?)

This is giving me headaches for way too long and I have absolutely no clue why this is happening. So long story short, I took Mark Nischalke's project from: http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events. The project has a SQL script to create the database and the SqlDependecy fires on his project/database. Now I knew I had issues with my previous databases and I copied his script and created my own database. Everything is identical except the tables (obviously). This means my SqlDependency should work since I have an identically configured database to another one which works; but no... it doesn't work. At this point, in my project I changed the SqlDependecy to check his database for notifications and it works, so it's not a .NET issue on my project. Then I tried to make the SqlDependency in his project check my database and nope, still doesn't work. In both cases I use the same connection string, integrated security but different initial catalogs. Thanks for any help.

(Also to note, the MessageQueue is created on my database and removed when I close the project SqlDependecy.Stop but the event isnt' firing)

This is his database creation script

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')  

And this is my database creation script (I just replaced all "Chatter" with "ISS")

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
)                            

Upvotes: 2

Views: 3926

Answers (2)

S.Roshanth
S.Roshanth

Reputation: 1507

Make sure you have run

ALTER DATABASE kmsdta SET ENABLE_BROKER;

if this statements hangs use this one enable broker

alter database kmsdta set enable_broker with rollback immediate;

Also you make sure you ran this in your Dababase

CREATE QUEUE ContactChangeMessages;

CREATE SERVICE ContactChangeNotifications
  ON QUEUE ContactChangeMessages

and some important things to consider when using SqlDependency to monitor a table

  1. In the previous code, you will notice that my SQL query does not use the "" wildcard to return all columns. You MUST return the exact columns that you want. If you use the "", you will have bad consequences.

  2. Also in the previous code, you will notice that my SQL query contains the "two-part" table name. This is also REQUIRED. Using just "TableName" instead of "owner.TableName" will also cause unwanted consequences.

example for a simple query:

select OrderID from dbo.[DTAOrders];

Upvotes: 0

B L
B L

Reputation: 1592

You might have an incorrect data type in your underlying table structure or some other illegal gotcha. I'd suggest checking out this article and making sure you meet all the criteria under the "Supported SELECT Statements" section:

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

Upvotes: 1

Related Questions