When I execute a query with a datetime column filter
WHERE [Order].CreatedOn >= @CreatedOn
using a SqlDependency
, the change on data source fires the SqlDependency.OnChange
event but the SqlDataReader
associated with the SqlCommand
doesn't return data (reader.HasRows
always returns false
).
When I just change the filter condition in my SQL statement to
WHERE [Order].StatusId = 1"
it just works fine and the SqlDataReader
returns data (reader.HasRows
returns true
)
Code:
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);
}
}
}
}
Images:
When passing DateTime.Now as a reference Date you are not very likely to retrieve records that have been created at some point in time (except if the records are created in the future and therefore you have some problem with your server time or the column name "createdOn" is very missleading).
To get the latest records based on some update date you need to do something like this:
_refDate
in my example, initialized to the value you choose, DateTime.MinValue in my case to get all records in the first call and then only get them incrementally, you can also take DateTime.Now to start at one moment in time)dependency_OnChange
event gets triggered you need to trigger the query with the last value of_refDate
in order to get everything you haven't retrieved yet_refDate
again and so on ..Not tested but this should work (take care of _refDate to be acccessible globally)