更新Datagridview当网络数据库数据发生更改时

时间:2014-10-30 09:55:09

标签: c# .net sql-server winforms

我使用以下连接将多个客户端连接到数据库:

Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

如何在数据库发生变化时(插入|| update || delete)更新所有客户端datagridview信息? (我使用的是SQL Server 2012)

感谢

2 个答案:

答案 0 :(得分:0)

更新网格数据,你必须制作线程,如果数据更新,则检查数据更新,然后从该线程获取数据并更新网格数据源

答案 1 :(得分:0)

这是我的班级

public class ManageNotificationUtil
{
    private BackgroundWorker NotificationWorker;
    private Timer timerNotification;
    private Form ParentForm;
    private NotificationDetailData NotificationDetail;
    public ManageNotificationUtil()
    {
        //NotificationWorker = new System.ComponentModel.BackgroundWorker();
        //timerNotification = new Timer();
        //NotificationWorker.RunWorkerAsync();
        //InitializeComponent();
    }
    public ManageNotificationUtil(Form owner)
    {
        ParentForm = owner;
        NotificationWorker = new System.ComponentModel.BackgroundWorker();
        NotificationWorker.RunWorkerAsync();
        timerNotification = new Timer();
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.timerNotification.Enabled = true;
        this.timerNotification.Interval = 60000;
        this.timerNotification.Tick += new EventHandler(timerNotification_Tick);
        this.NotificationWorker.DoWork += new DoWorkEventHandler(NotificationWorker_DoWork);
        NotificationWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NotificationWorker_RunWorkerCompleted);
    }
    private void NotificationWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        NotificationDetail = NotificationBD.GetNotificationDataByReceiverUserName(SessionManagerUI.UserSessionDetails.UserDetails.LoginUserName);

    }

    private void NotificationWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (NotificationDetail != null)
        {
            if (NotificationDetail.NotificationList != null)
            {
                if (NotificationDetail.NotificationList.Count > 0)
                {
                    NotificationAlertControlUtility notificationAlertControlUtility;
                    foreach (NotificationData Notification in NotificationDetail.NotificationList)
                    {
                        notificationAlertControlUtility = new NotificationAlertControlUtility(Notification);
                        notificationAlertControlUtility.AlertControlShowConformation(ParentForm);
                    }
                }
            }
        }
    }

    private void timerNotification_Tick(object sender, EventArgs e)
    {
        if (!NotificationWorker.IsBusy && SessionManagerUI.SessionDetails != null)
        {
            NotificationWorker.RunWorkerAsync(0);
        }
    }
    public void disposeItems()
    {

    }
}

我使用此类来显示存储在数据库中的通知 你可以根据需要改变它