Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在SQL Server中更改数据时自动刷新应用程序_C#_Sql Server_Sql Server 2008_Sql Server 2008 R2_Sql Server 2012 - Fatal编程技术网

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

C# 在SQL Server中更改数据时自动刷新应用程序,c#,sql-server,sql-server-2008,sql-server-2008-r2,sql-server-2012,C#,Sql Server,Sql Server 2008,Sql Server 2008 R2,Sql Server 2012,我使用SQL Server,我有3台应用服务器。当数据库中的表发生更改时,我需要这些应用程序服务器刷新本地缓存数据。我使用触发器进行已知更改,并通过ServiceBroker队列发送消息。然后我创建一个存储过程并将其分配给激活队列的存储过程,在这个存储过程中我收到消息,但我不知道如何在我的应用程序中调用refresh方法。我可以建议您尝试使用TCP解决此问题。每个应用监听一个端口,当另一个应用更新数据库时,它会向其他应用发送一条消息,表示它们需要刷新 希望这是个好主意。您应该看看如何使用SqlD

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

我可以建议您尝试使用TCP解决此问题。每个应用监听一个端口,当另一个应用更新数据库时,它会向其他应用发送一条消息,表示它们需要刷新


希望这是个好主意。

您应该看看如何使用
SqlDependency


更多信息请访问:

我遇到了类似的问题,通过以下代码解决了此问题

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


    }
更新:

检查权限:

 public bool CanRequestNotifications()
        {

            try
            {
                var perm = new SqlClientPermission(PermissionState.Unrestricted);
                perm.Demand();
                return true;
            }
            catch
            {
                return false;
            }
        }
例如,在窗口加载中:

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