编辑:我修好了。我所做的是将数据库的所有者更改为与我用来连接它的用户不同的东西。检查数据库所有者:
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 应该可以工作,因为我有一个与另一个可以工作的数据库相同配置的数据库;但不......它不起作用。此时,在我的项目中,我更改了 SqlDependecy 以检查他的用于通知的数据库并且它可以工作,所以这不是我的项目中的 .NET 问题。然后我尝试让他项目中的 SqlDependency 检查我的数据库,不,仍然无法正常工作。在这两种情况下,我都使用相同的连接字符串、集成的安全性但不同的初始目录。谢谢你的帮助。
(还要注意,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
)