当数据库(Sql Server)表中有新记录添加时,能不能做到可以通知应用程序?

shang515 2005-01-20 07:06:41
当数据库(Sql Server)有新记录添加时,能不能做到可以通知应用程序?

该表用来保存用户的消息,当有有某用户的消息时,通知应用程序该用户有新消息
...全文
374 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyne 2005-01-26
  • 打赏
  • 举报
回复
刚刚找到两篇文章,介绍给楼主。

.NET Remoting 技术漫谈(上)
http://www.csdn.net/news/newstopic/15/15426.shtml

.NET Remoting 技術漫談(下)
http://www.csdn.net/news/newstopic/15/15427.shtml

用Remoting可以完全显现你的要求,只不过深度大了些..

顺便介绍一本书《C# Web服务高级编程——使用.NET Remoting和ASP.NET创建Web服务》
shang515 2005-01-26
  • 打赏
  • 举报
回复
谢谢 yyne(yyne) 兄
vzxq 2005-01-25
  • 打赏
  • 举报
回复
help up
yyne 2005-01-24
  • 打赏
  • 举报
回复
沉了,友情帮你UP一下,希望还有高手支招~
shang515 2005-01-22
  • 打赏
  • 举报
回复
顶一把
warren1999 2005-01-21
  • 打赏
  • 举报
回复
好象有动态游标
xiaoniaofei 2005-01-21
  • 打赏
  • 举报
回复
gz
haoco 2005-01-21
  • 打赏
  • 举报
回复
要数据库能通知,那就要数据库提供商提供接口来跟应用程序打交道,不过般都不会提供的。

如果你不想用扫描的话你可以专门做一个用于更新的方法,要保证所有更新数据库的地方都调用这个方法。之后在这个方法中再做相关的事件。
shang515 2005-01-21
  • 打赏
  • 举报
回复
=======================================================
|| 我要想要实现数据库主动通知,不要定时扫描 ||
=======================================================
shang515 2005-01-21
  • 打赏
  • 举报
回复
谢谢大家的回复

我是想把聊天信息放到数据库里,如果使用wangsaokui(无间道III(终极无间)) 说的xp_cmdshell执行外部命令,这个效率太低,我测试过

使用RAISERROR这个还没有测试(待定)

其实使用扩展存储过程调用外部也挺好的,但是只能用在master库中
yyne 2005-01-21
  • 打赏
  • 举报
回复
wangsaokui(无间道III(终极无间)) 兄所说的便是我所指。

其实server通知与client定时请求都是一样的了,用文件依赖方式进行缓存请求,对server的负载并不是很大,实现起来也比较简单。

缓存变了,client会对server请求成功并返回新内容;
缓存没变,server就相当于不理会client的请求。

如果楼主想深入,可以学习一些xml与services的知识。
各有优劣。
wu896222 2005-01-21
  • 打赏
  • 举报
回复
可以利用存储过程调用DLL的方法实现你的目的. 相关资料网上都有.
fellowcheng 2005-01-21
  • 打赏
  • 举报
回复
学习一下
wangsaokui 2005-01-21
  • 打赏
  • 举报
回复
ORACLE数据库肯定有,其他的数据库我就不知道了
wangsaokui 2005-01-21
  • 打赏
  • 举报
回复
忘了,MSSQL中有xp_cmdshell这个命令,可以执行外部的程序

A. Return a list of executable files
This example shows the xp_cmdshell extended stored procedure executing a directory command.

EXEC master..xp_cmdshell 'dir *.exe'

B. Use Windows NT net commands
This example shows the use of xp_cmdshell in a stored procedure. This example notifies users (with net send) that SQL Server is about to be shut down, pauses the server (with net pause), and then shuts the server down (with net stop).

CREATE PROC shutdown10
AS
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 10 minutes. No more connections allowed.', no_output
EXEC xp_cmdshell 'net pause sqlserver'
WAITFOR DELAY '00:05:00'
EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down
in 5 minutes.', no_output
WAITFOR DELAY '00:04:00'
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 1 minute. Log off now.', no_output
WAITFOR DELAY '00:01:00'
EXEC xp_cmdshell 'net stop sqlserver', no_output

C. Return no output
This example uses xp_cmdshell to execute a command string without returning the output to the client.

USE master
EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',
NO_OUTPUT

D. Use return status
In this example, the xp_cmdshell extended stored procedure also suggests return status. The return code value is stored in the variable @result.

DECLARE @result int
EXEC @result = xp_cmdshell 'dir *.exe'
IF (@result = 0)
PRINT 'Success'
ELSE
PRINT 'Failure'

E. Write variable contents out to file
This example writes the contents of the current directory to a file named dir_out.txt in the current server directory.

DECLARE @cmd sysname, @var sysname
SET @var = 'dir /p'
SET @cmd = 'echo ' + @var + ' > dir_out.txt'
EXEC master..xp_cmdshell @cmd

上面是执行各种外部命令的例子
你可以把这些写到触发器中,但是执行这个必须要有数据库管理员的权限
wangsaokui 2005-01-21
  • 打赏
  • 举报
回复
相当与推和拉
楼主希望是数据库推到客户端,而不是客户端去拉数据
数据库不同,结果也不同,而且数据库只支持当前可能的应用程序,它不能判断你安装了什么专用的应用程序,当然也不能发消息给你,但它可以发给数据库管理系统或者邮件等通知你,你的应用程序再检测数据库管理系统或者邮件等接收这个消息,
比如:
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE
AS RAISERROR (50009, 16, 10)
GO
这个触发器会在sysmessages表中插入消息,参见sp_addmessage这个存储过程
你可以用各种程序,比如form中的timer、windows service中socket等检测在sysmessages是否有相关的消息发生。

关键在于数据库和你的应用程序间没有联系,这个联系需要应用程序作出,数据库是没有办法作出的,因为在开发数据库的时候根本不知道你应用程序的情况

trigger还有其他的方式,比如email等,你可以看MSSQL中的帮助
B. Use a trigger with a reminder e-mail message
This example sends an e-mail message to a specified person (MaryM) when the titles table changes.

USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE, DELETE
AS
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
GO
LoveCherry 2005-01-21
  • 打赏
  • 举报
回复
使用文件依赖
yistudio 2005-01-21
  • 打赏
  • 举报
回复
gz
shoutor 2005-01-21
  • 打赏
  • 举报
回复
可以,使用 Triger
yyne 2005-01-20
  • 打赏
  • 举报
回复
我表达得比较概括,楼主可以搜索一下相关资料。:)
加载更多回复(6)

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧