我有一个问题,当我使用另一个数据库实时更新不起作用。 [英] I Have A Problem That When I Use Another Database Real Time Updates Doesn't Work .

查看:86
本文介绍了我有一个问题,当我使用另一个数据库实时更新不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是html



 <   script     type   =  text / javascript    src   =  Scripts / jquery-1.9.1.min.js >  <   / script  >  
< script src = 脚本/jquery-jtemplates.js\" type = text / javascript > < / script >
<! - 参考SignalR库。 - >
< script src = / Scripts / jquery.signalR-2.2.0.js > < span class =code-keyword>< / script >
< script src = / Scripts / jquery.signalR-2.2.0.min。 js > < / script >
<! - 引用自动生成的SignalR集线器脚本。 - >
< script src = / signalr / hubs > < / script >

< script 类型 = text / javascript >

$(function(){
var notify = $ .connection.notificationsHub;

notify.client.displayNotification = function( msg){
alert(msg);

};

$ .connection.hub.start();
});
< / script >







和连接字符串:



 <   connectionStrings  >  
< add name = DefaultConnection connectionString = 数据源= .;初始目录= TestDB;集成安全性=真 providerName = System.Data.EntityClient / > ;
< / connectionStrings >


这是页面

公共部分类测试:System.Web.UI.Page
{

protected void Page_Load(object sender,EventArgs e)
{
SendNotifications();
}

列表< Model.Suggestion > 结果;
public List < Model.Suggestion > ; SendNotifications()
{

string conStr = ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString;


使用(SqlConnection connection = new SqlConnection(conStr))
{
string query = @select top 5 [sgtID],[Name],[Phone ],[性别],[年龄],[AnswerPerc],[AnswerRank],[日期],[注释]来自[dbo]。[建议]按日期排序desc;

using(SqlCommand command = new SqlCommand(query,connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange + = new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();

if(reader.HasRows)
{
reader.Read();
// result = reader [0] .ToString();
}
}
}
NotificationsHub nHub = new NotificationsHub();
//nHub.NotifyAllClients(message);
nHub.NotfiyAllClients(result);
返回结果;
}

private void dependency_OnChange(object sender,SqlNotificationEventArgs e)
{
if(e.Type == SqlNotificationType.Change)
{
SendNotifications();
}
}
}


这是集线器Class

命名空间TestSignalR.Hubs
{
public class NotificationsHub:Hub
{
private static string conString = ConfigurationManager.ConnectionStrings [DefaultConnection]。ToString();


public void NotfiyAllClients(List < Model.Suggestion > sgt)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext < NotificationsHub > ();
context.Clients.All.displayNotification(sgt);
}
}
}


这是全局类:

string connString = ConfigurationManager.ConnectionStrings [DefaultConnection ] .ConnectionString;

protected void Application_Start(object sender,EventArgs e)
{
SqlDependency.Start(connString);
}

解决方案

(function(){
var notify =


< blockquote> .connection.notificationsHub;

notify.client.displayNotification = function(msg){
alert(msg);

};


.connection.hub.start();
});
< / script >







和连接字符串:



 <   connectionStrings  >  
< add name = DefaultConnection connectionString = 数据源= .;初始目录= TestDB;集成安全性=真 providerName = System.Data.EntityClient / > ;
< / connectionStrings >


这是页面

公共部分类测试:System.Web.UI.Page
{

protected void Page_Load(object sender,EventArgs e)
{
SendNotifications();
}

列表< Model.Suggestion > 结果;
public List < Model.Suggestion > ; SendNotifications()
{

string conStr = ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString;


使用(SqlConnection connection = new SqlConnection(conStr))
{
string query = @select top 5 [sgtID],[Name],[Phone ],[性别],[年龄],[AnswerPerc],[AnswerRank],[日期],[注释]来自[dbo]。[建议]按日期排序desc;

using(SqlCommand command = new SqlCommand(query,connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange + = new OnChangeEventHandler(dependency_OnChange);
connection.Open();
SqlDataReader reader = command.ExecuteReader();

if(reader.HasRows)
{
reader.Read();
// result = reader [0] .ToString();
}
}
}
NotificationsHub nHub = new NotificationsHub();
//nHub.NotifyAllClients(message);
nHub.NotfiyAllClients(result);
返回结果;
}

private void dependency_OnChange(object sender,SqlNotificationEventArgs e)
{
if(e.Type == SqlNotificationType.Change)
{
SendNotifications();
}
}
}


这是集线器Class

命名空间TestSignalR.Hubs
{
public class NotificationsHub:Hub
{
private static string conString = ConfigurationManager.ConnectionStrings [DefaultConnection]。ToString();


public void NotfiyAllClients(List < Model.Suggestion > sgt)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext < NotificationsHub > ();
context.Clients.All.displayNotification(sgt);
}
}
}


这是全局类:

string connString = ConfigurationManager.ConnectionStrings [DefaultConnection ] .ConnectionString;

protected void Application_Start(object sender,EventArgs e)
{
SqlDependency.Start(connString);
}


Here is html

<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>
    <script src="Scripts/jquery-jtemplates.js" type="text/javascript"></script>
    <!--Reference the SignalR library. -->
    <script src="/Scripts/jquery.signalR-2.2.0.js"></script>
    <script src="/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>

     <script type="text/javascript">

         $(function () {
             var notify = $.connection.notificationsHub;

             notify.client.displayNotification = function (msg) {
                 alert(msg);

             };

             $.connection.hub.start();
         });
    </script>




and the Connection String :

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.EntityClient" />
    </connectionStrings>


and this is the page

  public partial class Test : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           SendNotifications();
        }

        List<Model.Suggestion> result;
        public List<Model.Suggestion> SendNotifications()
        {

            string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;


            using (SqlConnection connection = new SqlConnection(conStr))
            {
                string query = @"select top 5 [sgtID] , [Name] , [Phone] , [Gender] , [Age] ,[AnswerPerc] , [AnswerRank] , [date] , [Notes] from [dbo].[Suggestion] order by date desc";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Notification = null;
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        //result = reader[0].ToString();
                    }
                }
            }
            NotificationsHub nHub = new NotificationsHub();
            //nHub.NotifyAllClients(message);
            nHub.NotfiyAllClients(result);
            return result;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                SendNotifications();
            }
        }
    }


and this is the hub Class

namespace TestSignalR.Hubs
{
    public class NotificationsHub : Hub
    {
        private static string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();


        public void NotfiyAllClients(List<Model.Suggestion> sgt)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
            context.Clients.All.displayNotification(sgt);
        }
    }
}


Here is the global class:

 string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(connString);
        }

解决方案

(function () { var notify =


.connection.notificationsHub; notify.client.displayNotification = function (msg) { alert(msg); };


.connection.hub.start(); }); </script>




and the Connection String :

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.EntityClient" />
    </connectionStrings>


and this is the page

  public partial class Test : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
           SendNotifications();
        }

        List<Model.Suggestion> result;
        public List<Model.Suggestion> SendNotifications()
        {

            string conStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;


            using (SqlConnection connection = new SqlConnection(conStr))
            {
                string query = @"select top 5 [sgtID] , [Name] , [Phone] , [Gender] , [Age] ,[AnswerPerc] , [AnswerRank] , [date] , [Notes] from [dbo].[Suggestion] order by date desc";

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Notification = null;
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        //result = reader[0].ToString();
                    }
                }
            }
            NotificationsHub nHub = new NotificationsHub();
            //nHub.NotifyAllClients(message);
            nHub.NotfiyAllClients(result);
            return result;
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Type == SqlNotificationType.Change)
            {
                SendNotifications();
            }
        }
    }


and this is the hub Class

namespace TestSignalR.Hubs
{
    public class NotificationsHub : Hub
    {
        private static string conString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();


        public void NotfiyAllClients(List<Model.Suggestion> sgt)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
            context.Clients.All.displayNotification(sgt);
        }
    }
}


Here is the global class:

 string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(connString);
        }


这篇关于我有一个问题,当我使用另一个数据库实时更新不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆