当我使用datetime列过滤器执行查询时
WHERE [Order].CreatedOn >= @CreatedOn
Run Code Online (Sandbox Code Playgroud)
使用a SqlDependency,对数据源的更改将触发SqlDependency.OnChange事件,但SqlDataReader与之关联的SqlCommand不会返回数据(reader.HasRows始终返回false).
当我只是将我的SQL语句中的过滤条件更改为
WHERE [Order].StatusId = 1"
Run Code Online (Sandbox Code Playgroud)
它只是工作正常和SqlDataReader返回数据(reader.HasRows返回true)
码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SignalRServer
{
public partial class DepartmentScreen : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var u = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var UserName = u.Translate(Type.GetType("System.Security.Principal.NTAccount")).Value;
CheckForNewOrders(DateTime.Now);
}
private void CheckForNewOrders(DateTime dt) …Run Code Online (Sandbox Code Playgroud) 我有一个SqlTableDependency的问题.当我对欲望表进行插入/更新/删除时,不会调用My Changed方法.OnStatusChanged事件工作正常.
string conn = @"data source=secret server; integrated security=True; initial catalog=secret db;User id=secret user";
var mapper = new ModelToTableMapper<SqlDataModel>();
mapper.AddMapping(c => c.datavalue, "datavalue");
using (var dep = new SqlTableDependency<SqlDataModel>(conn, "data", mapper))
{
dep.OnChanged += Changed;
dep.OnStatusChanged += OnStatusChanged;
dep.OnError += OnError;
dep.TraceLevel = TraceLevel.Verbose;
dep.TraceListener = new TextWriterTraceListener(Console.Out);
dep.Start();
Console.WriteLine("Press a key to exit");
Console.ReadKey();
dep.Stop();
}
}
static void OnStatusChanged(object sender, StatusChangedEventArgs e)
{
Console.WriteLine(e.ToString());
}
static void OnError(object sender, ErrorEventArgs e)
{
Console.WriteLine(e.ToString());
}
static void Changed(object …Run Code Online (Sandbox Code Playgroud) 我们如何在Asp.Net MVC中为缓存对象应用Sql Dependency?
我有一个表和一个等待新插入的SqlDependency.
OnChange会根据需要触发,但我不明白是否有可能获得导致数据库更改的行.
SqlDependency sql命令:
SqlCommand cmd = new SqlCommand("SELECT id FROM dbo.DataRequests", m_sqlConn);
Run Code Online (Sandbox Code Playgroud)
OnChange代码:
private void OnChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= OnChange;
Console.WriteLine("Info: " + e.Info.ToString());
Console.WriteLine("Source: " + e.Source.ToString());
Console.WriteLine("Type: " + e.Type.ToString());
Console.WriteLine(DateTime.Now);
GetMessages();
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码使用SqlDependency来监视我的一个数据库中的更改它工作得很好,除了每次运行它在数据库中生成自己的带有guid的队列/服务/路由:
类:
class SqlWatcher
{
private string connectionString;
private string sqlQueue;
private string listenerQuery;
private SqlDependency dependency;
public SqlWatcher(string connectionString, string sqlQueue, string listenerQuery)
{
this.connectionString = connectionString;
this.sqlQueue = sqlQueue;
this.listenerQuery = listenerQuery;
this.dependency = null;
}
public void Start()
{
SqlDependency.Start(connectionString);
ListenForChanges();
}
public void Stop()
{
SqlDependency.Stop(this.connectionString);
}
private void ListenForChanges()
{
//Remove existing dependency, if necessary
if (dependency != null)
{
dependency.OnChange -= onDependencyChange;
dependency = null;
}
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command …Run Code Online (Sandbox Code Playgroud) 我有一个Windows服务使用SqlDependency类监听表中的插入.
它工作好几天但突然停止工作.
在正常情况下,我收到更改事件
e.Type = SqlNotificationType.更改
e.Info = SqlNotificationInfo.插入
e.Source = SqlNotificationSource.数据
如果没有任何变化,我会每3600秒获得一次超时事件
e.Type = SqlNotificationType.更改
e.Info = SqlNotificationInfo.错误
e.Source = SqlNotificationSource.超时
或(不知道为什么有两个不同的超时事件)
e.Type = SqlNotificationType.更改
e.Info = SqlNotificationInfo.未知的
e.Source = SqlNotificationSource.超时
这可以工作一周或更长时间但突然间我不再接收更改事件,而是每60秒接收一次事件
e.Type = SqlNotificationType.更改
e.Info = SqlNotificationInfo.错误
e.Source = SqlNotificationSource.客户
SqlNotificationSource.Client的msdn文档说
发生客户端启动的通知,例如客户端超时或尝试将命令添加到已触发的依赖项.
我认为这意味着在创建依赖项时发生了一个时间.
相同的代码一直在运行,如下所示:
private void CreateDependency() {
using (var connection = new SqlConnection(_connectionString)) {
connection.Open();
var command = new SqlCommand();
command.CommandText = "SELECT ..."; …Run Code Online (Sandbox Code Playgroud) 问题:
有许多不同的数据库,它们直接由许多不同的应用程序填充(没有任何常见的应用程序层).只能通过SP(按政策)访问数据
任务:
应用程序需要跟踪这些数据库中的更改并在最短时间内做出响应
可能的解决方案:
1)为每个数据库中的每个表创建触发器,这将使用事件填充一个表.应用程序将通过SqlDependency观察此表.
2)通过SqlDependency观察每个数据库中的每个表.
3)为每个数据库中的每个表创建触发器,这将使用托管扩展通知应用程序.
哪种方式最好?
1)我想知道sql server如何在客户端和数据库之间建立通道.我想必须有一个频道,为什么sql server可以通过该频道向客户端发送通知.请详细讨论这个问题.因为我看到很多关于sql依赖的文章,但每个机构都给出了代码,但没有机构解释它是如何工作的细节.什么是服务经纪人?
Service Broker体系结构允许您构建松散耦合的SQL Server实例,以便实例使用正常的消息传递方式相互通信.Service Broker使用TCP/IP从网络传输消息,因此允许加密消息传递.它既适用于使用SQL Server实例的应用程序,也适用于将工作分配到多个SQL Server实例的应用程序.Service Broker允许使用Queue来保存消息,因此消息将被逐个处理,而调用者无需等待接收消息.
1)我想知道服务代理总是以加密格式传递消息?
2)Service Broker允许使用Queue来保存消息.服务代理使用的队列名称是什么.我如何才能看到该队列中存储的内容?
3)我看到很多人创建队列,但没有提到他们创建的原因?他们也没有在代码中使用该队列.这是一个网址和示例代码
http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/
CREATE QUEUE NameChangeQueue;
CREATE SERVICE NameChangeService ON QUEUE NameChangeQueue ([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]);
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO YourUserName;
ALTER DATABASE YourDatabaseName SET ENABLE_BROKER;
Run Code Online (Sandbox Code Playgroud)
他们从不使用NameChangeQueue队列为什么?我怎么知道谁将使用这个队列?
4)即使我看到人们创造角色但却不知道为什么在这种情况下需要角色?
所以请详细讨论我的所有要点,因为我需要了解所有要点.谢谢
我有一个Web应用程序,它使用与其他Web应用程序共享的SQL Server数据库(我无法控制).我必须知道任何Web应用程序何时对数据库中的某些表进行更改.
我的第一个想法是使用SqlDependency(具体来说SqlTableDependency,因为我需要知道更改的数据),但我担心性能问题.
我想知道是否有任何性能比较SqlDependency(非SqlTableDependency),触发器(触发WS,exe等)和轮询.
我发现了一些问题和文章,但对我来说还不够清楚
其他信息:
谢谢!
SignalR 集线器的一大用途似乎是向所有其他客户端显示一个客户端的操作。我希望使用 SignalR 的目的是,当我的服务器端代码中发生某个事件时,我想实例化一个集线器对象并调用其方法之一来与所有客户端进行通信。如果您看到我之前的文章(Route To Take With SqlDependency OnChange),我想在 SqlDependency 的 OnChange 方法中执行此操作。经过研究,我发现有些人谈论使用 IHubContext 对象,尽管我还没有找到很多实例化和实际向客户端发送数据的示例。
这是否可以做到(如果可能的话,使用 IHubContext 向所有客户端发送数据可能会是什么样子),如果不可能,有什么方法可以绕过实例化这样的集线器?
sqldependency ×10
c# ×7
sql-server ×5
.net ×2
asp.net ×2
signalr ×2
ado.net ×1
asp.net-mvc ×1
polling ×1
signalr-hub ×1
sql ×1
triggers ×1