如何在更新数据源时刷新数据源? [英] How do I refresh my datasource when it's updated ?

查看:200
本文介绍了如何在更新数据源时刷新数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i开发了连接到sql server的c#应用程序

我的sql server数据库包含大约20个表。

a user(1)请求表记录,应用程序连接到服务器并执行sql命令将数据填充到gridview中

问题是当另一个用户(2)更新了用户所需的同一个表时(1) ),这些变化不会反映给用户(1)gridview。



感谢您提前回复。



我尝试了什么:



i使用服务代理尝试了c#sqldependency类,但这对我没什么帮助,因为sqldependency是用于监视单个sql命令

我想要的是监视表中发生的任何更改并通知所有用户刷新数据源并重新填充datagridview



我使用的部分代码:



启动sql依赖:



Hello
i have developed c# application that connects to sql server
my sql server database contains about 20 tables.
a user(1) asks for table records , the application connects to the server and sql command is executed to fill the data into gridview
the problem is when another user(2) updated the same table that was required by user(1) , the changes wont reflect to user(1) gridview.

thanks for your reply in Advance.

What I have tried:

i have tried c# sqldependency class using service broker but that didn't much helped me out because sqldependency is used to monitor a single sql command
what i want is to monitor any changes happened to the table and notify all users to refresh there datasource and refill the datagridview

part of the code i used :

starting sql dependency :

SqlDependency depend = new SqlDependency(cmd);
            depend.OnChange += new OnChangeEventHandler(OnDependencyChange);
            cmd.Notification = null;
            SqlDependency.Start(sqlcon.ConnectionString.ToString());





OnDependencyChange:





OnDependencyChange :

void OnDependencyChange(object sender, SqlNotificationEventArgs e)
        {
            SqlDependency dependency = sender as SqlDependency;
            dependency.OnChange -= new OnChangeEventHandler(OnDependencyChange);
            MessageBox.Show("table is altered");
        }

推荐答案

你必须自己设置它。一种方法是使用一个审计表来存储有关表更改的信息。我可能会使用以下架构:



LastChanged datetime

TableName nvarchar(64)



并写入存储过程,它返回在指定日期时间之后为指定表添加的记录数。如果计数为0,则应用程序不需要执行任何操作。如果计数>> 1,请在应用中执行某些操作。
You have to set it up yourself. One way would be to have an audit table that stores info regarding changes to a table. I would probably go with the following schema:

LastChanged datetime
TableName nvarchar(64)

and write stored proc that returns the count of records that were added for a specified table after a specified datetime. If the count is 0, the app doesn't need to do anything. If the count is >= 1, do something in the app.


这篇关于如何在更新数据源时刷新数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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