Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
mehdi lotfi Avatar asked Aug 17 '14 10:08

mehdi lotfi


People also ask

How do I get SQL Server to automatically update stats?

To set the asynchronous statistics update option in SQL Server Management Studio, in the Options page of the Database Properties window, both Auto Update Statistics and Auto Update Statistics Asynchronously options need to be set to True. Statistics updates can be either synchronous (the default) or asynchronous.

What is DB refresh activity in SQL Server?

Generally, it is the process of overwriting an existing database with a stage, development or production database to purge old data. This process refreshes the database and its data while keeping all database objects intact.


1 Answers

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();
like image 68
ImanKazemi Avatar answered Sep 22 '22 12:09

ImanKazemi