Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

Thanks in advance.

share|improve this question

3 Answers 3

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();
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.