当我使用日期时间列过滤器执行查询时
WHERE [Order].CreatedOn >= @CreatedOn
使用 a SqlDependency
,对数据源的更改会触发SqlDependency.OnChange
事件,但与SqlDataReader
关联的事件SqlCommand
不会返回数据(reader.HasRows
始终返回false
)。
当我只是将我的 SQL 语句中的过滤条件更改为
WHERE [Order].StatusId = 1"
它工作正常并且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)
{
string json = null;
string conStr = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(conStr))
{
string query = string.Format(@"
SELECT [Order].OrderId
FROM [dbo].[Order]
WHERE [Order].CreatedOn >= @CreatedOn");
// query = string.Format(@"
// SELECT [Order].OrderId
// FROM [dbo].[Order]
// WHERE [Order].StatusId = 1");
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.Add("@CreatedOn", SqlDbType.DateTime);
command.Parameters["@CreatedOn"].Value = DateTime.Now;
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();
json = reader[0].ToString();
}
}
}
SignalRHub hub = new SignalRHub();
hub.OrderReceived(json, null);
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
CheckForNewOrders(DateTime.Now);
}
else
{
//Do somthing here
//Console.WriteLine(e.Type);
}
}
}
}
图片: