C# 如何使用signer在数据库更新时通知客户机? 名称空间somename { 公共类ClaimInfo存储库 { 公共IEnumerable GetData() { 使用(var connection=new SqlConnection(ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString)) { connection.Open(); 使用(SqlCommand命令=newsqlcommand(@“从[dbo].[table]中选择计数(1),其中ClaimStatus=8”,连接)) { //确保命令对象尚未具有 //与之关联的通知对象。 command.Notification=null; SqlDependency=新的SqlDependency(命令); dependency.OnChange+=新的OnChangeEventHandler(dependency\u OnChange); if(connection.State==ConnectionState.Closed) connection.Open(); 使用(var reader=command.ExecuteReader()) return reader.Cast() .Select(x=>newclaiminfo() { ClaimStatus=x.GetInt32(0) }).ToList(); } } } void dependency_OnChange(对象发送方,SqlNotificationEventArgs e) { if(e.Type==SqlNotificationType.Change) { //从这里我们将向客户端发送通知消息 IHubContext context=GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.notify(); } } } }
如果我删除If条件C# 如何使用signer在数据库更新时通知客户机? 名称空间somename { 公共类ClaimInfo存储库 { 公共IEnumerable GetData() { 使用(var connection=new SqlConnection(ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString)) { connection.Open(); 使用(SqlCommand命令=newsqlcommand(@“从[dbo].[table]中选择计数(1),其中ClaimStatus=8”,连接)) { //确保命令对象尚未具有 //与之关联的通知对象。 command.Notification=null; SqlDependency=新的SqlDependency(命令); dependency.OnChange+=新的OnChangeEventHandler(dependency\u OnChange); if(connection.State==ConnectionState.Closed) connection.Open(); 使用(var reader=command.ExecuteReader()) return reader.Cast() .Select(x=>newclaiminfo() { ClaimStatus=x.GetInt32(0) }).ToList(); } } } void dependency_OnChange(对象发送方,SqlNotificationEventArgs e) { if(e.Type==SqlNotificationType.Change) { //从这里我们将向客户端发送通知消息 IHubContext context=GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.notify(); } } } },c#,sql,model-view-controller,signalr,sqldependency,C#,Sql,Model View Controller,Signalr,Sqldependency,如果我删除If条件If(e.Type==SqlNotificationType.Change)它可以工作,但它进入无限循环(一次又一次地通知),如果有更改,我只想通知一次。。!!如果您有任何帮助,我们将不胜感激。我已点击了此链接-您遇到了什么错误?或者是if(e.Type==SqlNotificationType.Change)block为false?如果我添加if block,它没有任何错误。。!!如果我添加If块。。!我的信号器也停止工作在if块中放置一个断点并检查状态。。。 namespa
If(e.Type==SqlNotificationType.Change)
它可以工作,但它进入无限循环(一次又一次地通知),如果有更改,我只想通知一次。。!!如果您有任何帮助,我们将不胜感激。我已点击了此链接-您遇到了什么错误?或者是if(e.Type==SqlNotificationType.Change)
block为false?如果我添加if block,它没有任何错误。。!!如果我添加If块。。!我的信号器也停止工作在if块中放置一个断点并检查状态。。。
namespace somename
{
public class ClaimInfoRepository
{
public IEnumerable<ClaimInfo> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT count(1) FROM [dbo].[table] WHERE ClaimStatus=8", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new ClaimInfo()
{
ClaimStatus = x.GetInt32(0)
}).ToList();
}
}
}
void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
//from here we will send notification message to client
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ClaimHub>();
context.Clients.All.notify();
}
}
}
}