0

我有以下代码对数据库视图执行 SqlCommand:

public IEnumerable<PickNote> GetData()
        {
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BTNInternalData_LiveEntities"].ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand(@"SELECT [PICKINGROUTEID],[CUSTOMER],[SALESNAME]
           FROM [dbo].[PickScreenData] WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, [ACTIVATIONDATETIME])) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) or [EXPEDITIONSTATUS] = 3", connection))
                {
                    // Make sure the command object does not already have
                    // a notification object associated with it.
                    command.Notification = null;

                    var dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    using (var reader = command.ExecuteReader())
                    {
                        var data = reader.Cast<IDataRecord>();

                        return data.Select(x => new PickNote
                        {
                            pickingRouteId = x["PICKINGROUTEID"].ToString()
                        }).ToList();
                    }
                }
            }
        }

private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        // do stuff
    }

但是,该dependency_OnChange方法仅在我的应用程序开始时被调用,并且无论我的视图中的数据是否更改都不会再次调用。我已经调试了 SqlNotificationEventArgs 并且信息是Invalid但我不确定为什么命令查询运行良好

编辑

我已经更改了查询,以便它直接查询表,但SqlNotificationEventArgs.Info仍然说Invalid. 这是新代码:

public IEnumerable<PickNote> GetData()
    {
        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AXLive"].ConnectionString))
        {
            connection.Open();
            using (var command = new SqlCommand(@"
                SELECT PICKINGROUTEID, EXPEDITIONSTATUS
                FROM         [dbo].[WMSPICKINGROUTE]
                WHERE     (EXPEDITIONSTATUS <> 20) 
                AND (DATEADD(dd, 0, DATEDIFF(dd, 0, [ACTIVATIONDATETIME])) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) 
                    OR [EXPEDITIONSTATUS] = 3)", connection))
            {
                // Make sure the command object does not already have
                // a notification object associated with it.
                command.Notification = null;

                var dependency = new SqlDependency(command);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                using (var reader = command.ExecuteReader())
                {
                    var data = reader.Cast<IDataRecord>();

                    return data.Select(x => new PickNote
                    {
                        pickingRouteId = x["PICKINGROUTEID"].ToString()
                    }).ToList();
                }
            }
        }
    }

WMSPICKINGROUTE是我的视图从之前获取数据的表。

4

1 回答 1

1

根据此 MSDN page,您不能对引用视图的选择语句使用 SqlDependency。这似乎是问题所在。重写您的查询以命中表,它应该可以工作。

于 2013-11-14T16:30:25.037 回答