简体   繁体   中英

Refresh Application Automatically When Data changed in SQL Server

I use SQL Server and I have 3 Application servers. When a table in my database have changed I need to those application servers refresh there local cached data. I use a trigger to known change and send a message via Service broker queue. Then I create a stored procedure and assign it to activate stored procedure of my queue, In this stored procedure I receive message, but I don't know How should I call refresh method in my application.

I had similiar issue and with below code resolved this issue

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();
        }


    }

Update:

Check for permission:

 public bool CanRequestNotifications()
        {

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

For Instance in your window load:

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();

You should look at using the SqlDependency class.

More information at: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency(v=vs.110).aspx

I can suggest you to try solving the problem using TCP. Each app listens to a port and when another app updates the db it sends a message to the other apps saying they need to refresh.

Hope that was a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM