如何在SQL中触发以在C#dot net中触发事件? [英] How do I make a trigger in SQL to fire an event in C# dot net?

查看:88
本文介绍了如何在SQL中触发以在C#dot net中触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想通过提供此要求的实际原因的所有细节来复杂化。因此,我将尝试举一个简单的例子。



以下是我的要求:

1)每当有空的时候弹出一个MessageBox在名为stock的表中更改值。

2)简单来说,如果我在MS SQL中运行以下查询:



update stocktable set stockcount = 4其中StockID = 1111



然后,我的应用程序应该认识到值有变化所以它应该在我的应用程序中给我一个弹出窗口。



我尝试了什么:



我能够通过使用计时器每秒检查一次库存计数来实现这一点。

但这在性能方面是不可行的。因为如果说有500个用户,那么在所有500台计算机上,每天将有500(人)* 12(小时)* 60(最少)* 60(最少)。

因此,我能想到的另一种方法是仅在有变化时调用函数来显示MessageBox,而不是每秒都检查应用程序的变化。

请救命。我正在使用C#在Visual Studio中编写此代码。后端是MSSQL。

I don't want to complicate by giving all details of actual reason for this requirement. Hence, I'll try to give a simple example.

Following is my requirement:
1) Pop out a MessageBox whenever there is a change of value in a table called stock.
2) In simple words, if I run following query in MS SQL:

update stocktable set stockcount = 4 where StockID = 1111

Then, my application should recognize that there is a change in value so it should give me a popup in MessageBox in my application.

What I have tried:

I have been able to achieve this by checking stockcount every second using a timer.
But it's not feasible in terms of performance. Because if say there are 500 users, then on all 500 computers, there will be 500(people) * 12(hours) * 60(minues) * 60(minues) per day.
Hence, the other way I could think of is to invoke function to display MessageBox only when there is a change rather than checking for the change from application every second.
Please help. I am writing this code in Visual Studio using C#. Backend is MSSQL.

推荐答案

这个问题的答案是SQL依赖。

使用此链接:

使用SqlDependency检测更改 [ ^ ]
The answer to this problem is SQL Dependency.
Use this link:
Detecting Changes with SqlDependency[^]


这篇关于如何在SQL中触发以在C#dot net中触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆