C# 客户端的 SQL Server 2008 更改跟踪
我有 2 个应用程序,第一个是与用户交互的 ASP.NET 站点,第二个是用于安排作业执行的 C# 应用程序。我在 C# 应用程序中进行更改跟踪时遇到问题,因此它无法响应用户对数据库的更新。基本思想是这样的:
用户通过 ASP.NET 网站向产品数据库添加一个新行。然后,C# 应用程序应该收到新行的通知,并且它将生成一个 Quartz.NET 作业来对这个新创建的产品执行某些操作。如果产品详细信息更新,C# 应用程序会收到通知并相应更新 Quartz.NET 作业。
伪代码:
while(true) // it's not really a loop like this, but this suffices
{
var newProducts = from p in dc.Products where Added_On > DateTime.Now;
foreach(Product product in newProducts)
CreateNewJob(product);
}
然后,每个作业都需要跟踪对其各个列的更改:
void Execute(...)
{
var product = from p in dc.Products where Id == this._product.Id;
if (CompareProduct(this._product, product) == false)
_product = product;
// do work with the product
}
或使用时间戳:
void Execute(...)
{
var updated = from p in dc.Products where Id == _product.Id && Updated_On > DateTime.Now;
if (updated != null)
_product = updated;
}
由于产品通常不会更改,是否有比每次执行时查询更改更好的解决方案?
要求是,如果数据发生更改,Quartz.NET 作业不应使用陈旧数据执行。在 Execute() 的开头,它应该是最新的更改。如果在执行过程中状态发生变化也没关系。
我查看了 SqlDependency,但我读到的所有内容都表明它并没有告诉您发生了什么变化,只是告诉您发生了什么变化。对于一个大型数据库来说,不断地拉下全表并自己进行比较似乎是不可行的。
变更跟踪似乎也可能效率低下。我必须拉下所有更改,找到与更改项相对应的 Quartz.NET 作业,并使用最新数据更新它。
I have 2 applications, one an ASP.NET site the user interfaces with, and then a second C# application that schedules jobs for execution. I'm having trouble with change tracking in the C# application, so that it can respond to updates to the database from the user. The basic idea is this:
The user adds a new row to the Products database via the ASP.NET website. The C# application should then be notified of the new row, and it will spawn a Quartz.NET job to do something with this newly created product. If the product details are updated, the C# application is notified and updates the Quartz.NET job appropriately.
Pseudocode:
while(true) // it's not really a loop like this, but this suffices
{
var newProducts = from p in dc.Products where Added_On > DateTime.Now;
foreach(Product product in newProducts)
CreateNewJob(product);
}
Then, each job is required for tracking changes to it's individual column:
void Execute(...)
{
var product = from p in dc.Products where Id == this._product.Id;
if (CompareProduct(this._product, product) == false)
_product = product;
// do work with the product
}
or with a timestamp:
void Execute(...)
{
var updated = from p in dc.Products where Id == _product.Id && Updated_On > DateTime.Now;
if (updated != null)
_product = updated;
}
Since more often than not, the product's won't be changing, is there a better solution than to query for changes every time it executes?
The requirement is that if the data has changed, the Quartz.NET job should not execute with stale data. At the beginning of Execute() it should be the most recent changes. It is OK if the state changes during execution though.
I've looked at SqlDependency, but everything I've read says it does not tell you what changed, only that something changed. It seems infeasible for a large database to constantly pull down the full table, and do the comparison myself.
Change tracking also seems that it might be inefficient. I'd have to pull down all the changes, find the Quartz.NET job corresponding to the changed item, and update it with the most recent data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许最好的方法是采用老式的方法——使用 sql TIMESTAMP 列自己进行更改跟踪。那么你的调度程序只需要抓取 TIMESTAMP > 的行。 [上次运行的时间戳] 来安排。计划时,它可以检查行以查看时间戳是否已更改并在运行之前获取新数据。
现在,如果您需要担心多个表,那么事情就会变得更加复杂。
Probably the best way is to do this the old-fashoined way -- use the sql TIMESTAMP column to do the change tracking yourself. Then your scheduler only need grab rows where TIMESTAMP is > [last runned TIMESTAMP] to schedule. When scheduled, it can check the row to see if the TIMESTAMP has changed and grab the new data before running.
Now, if you've got multiple tables to worry about, this gets to be a bit more complex.