在SQL Server中更改数据时自动刷新应用程序

meh*_*tfi 19 c# sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012

我使用SQL Server,我有3个应用程序服务器.当我的数据库中的表发生更改时,我需要向那些应用程序服务器刷新本地缓存的数据.我使用触发器来进行已知更改并通过服务代理队列发送消息.然后我创建一个存储过程并分配它来激活我的队列的存储过程,在这个存储过程中我收到消息,但我不知道如何在我的应用程序中调用refresh方法.

Ima*_*emi 8

我有类似的问题,下面的代码解决了这个问题

class QueryNotification
    {
        public DataSet DataToWatch { get; set; }
        public SqlConnection Connection { get; set; }
        public SqlCommand Command { get; set; }

        public string GetSQL()
        {
            return "SELECT * From YourTable";
        }

        public string GetConnection()
        {
            return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }

        public bool CanRequestNotifications()
        {

            try
            {
                var perm = new SqlClientPermission(PermissionState.Unrestricted);
                perm.Demand();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void GetData()
        {
            DataToWatch.Clear();
            Command.Notification = null;
            var dependency =
                new SqlDependency(Command);
            dependency.OnChange += dependency_OnChange;

            using (var adapter =
                new SqlDataAdapter(Command))
            {
                adapter.Fill(DataToWatch, "YourTableName");
            }
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {

            var i = (ISynchronizeInvoke)sender;

            if (i.InvokeRequired)
            {

                var tempDelegate = new OnChangeEventHandler(dependency_OnChange);

                object[] args = { sender, e };

                i.BeginInvoke(tempDelegate, args);

                return;
            }

            var dependency = (SqlDependency)sender;

            dependency.OnChange -= dependency_OnChange;

            GetData();
        }


    }
Run Code Online (Sandbox Code Playgroud)

更新:

检查权限:

 public bool CanRequestNotifications()
        {

            try
            {
                var perm = new SqlClientPermission(PermissionState.Unrestricted);
                perm.Demand();
                return true;
            }
            catch
            {
                return false;
            }
        }
Run Code Online (Sandbox Code Playgroud)

对于窗口加载中的实例:

if (!_queryNotification.CanRequestNotifications())
            {
                MessageBox.Show("ERROR:Cannot Connect To Database");
            }

            SqlDependency.Stop(_queryNotification.GetConnection());
            SqlDependency.Start(_queryNotification.GetConnection());

            if (_queryNotification.Connection == null)
            {
                _queryNotification.Connection = new SqlConnection(_queryNotification.GetConnection());
            }

            if (_queryNotification.Command == null)
            {
                _queryNotification.Command = new SqlCommand(_queryNotification.GetSQL(),
_queryNotification.Connection);
            }
            if (_queryNotification.DataToWatch == null)
            {
                _queryNotification.DataToWatch = new DataSet();
            }

            GetData();
Run Code Online (Sandbox Code Playgroud)