我有以下代码对数据库视图执行 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
是我的视图从之前获取数据的表。