c#-SqlDependency.OnChange没有在datetime列上使用过滤器触发

我在SqlDependency中使用此SQL语句时遇到问题.它只是不激活SqlDependency.OnChange事件,而是在SQL Server查询窗口中按预期返回结果.

SELECT [Order].OrderId
FROM [dbo].[Order]
WHERE [Order].CreatedOn > '20150815 21:11:57.502'

我在这里阅读了支持的SELECT语句部分https://technet.microsoft.com/en-us/library/ms181122(v=sql.105).aspx,但没有发现任何违反的规则.

有任何想法吗?

更新:

我的完整代码如下:

    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 > '{0}'"
, dt.ToString("yyyyMMdd HH:mm:ss.fff"));  // 20150814 00:00:00.000

            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();
                    json = "testing ... "; reader[0].ToString();
                }
            }
        }
    }

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

更新(响应jmelosegui的回答):

            query = string.Format(@"
                    SELECT [Order].OrderId
                    FROM [dbo].[Order]
                    WHERE [Order].CreatedOn > @CreatedOn");

            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)
                {
                ...
                }
            }

解决方法:

我认为您应该只获得通知事件,而不是那种类型.

您能否在else_OnChange方法中添加一个else分支,以查看是否正在获取其他任何SqlNotificationType,例如:

private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
    if (e.Type == SqlNotificationType.Change)
    {
        CheckForNewOrders(DateTime.Now);
    }
    else
    {
        //Do somthing here
        Console.WriteLine(e.Type);
    }
}

更新资料

您能否尝试添加类型化参数:

WHERE [Order].CreatedOn > @myDateTime

并传入DateTime类型的参数,而不使用字符串转换.

上一篇:如何仅在sql中为特定连接创建事务?


下一篇:可以通过很多空格传递MySQL查询吗?