当我将它添加到我的 where 子句时:
DueDate <= getDate() AND DueDate IS NOT null
我得到一个不错的小故障,上面写着:无法创建子:System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.Data.EvaluateException:表达式包含未定义的函数调用 getDate()。
知道我做错了什么吗?
更新 使用中的代码:
private string getFilter(int mode) {
String filter = "";
if ((ALL_WORK_ORDERS & mode) == 0) {
if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
filter += "DueDate <= getDate() AND DueDate IS NOT null OR";
}
if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
filter += " FinishedDate IS NOT null";
}
}
filter = filter.Trim();
if (filter.EndsWith("OR")) {
filter = filter.Remove(filter.Length - 2);
}
return filter;
}
它在这里被使用:
tblWorkOrderBindingSource.Filter = getFilter(mode);
在表中,DueDate 是日期时间类型。
旁注: 我可以跑
SELECT [ID]
,[WorkDesc]
,[DueDate]
FROM [RentalEase].[dbo].[tblWorkOrder]
WHERE [DueDate] <= getDate() AND [DueDate] IS NOT null
在 MS SQL Server Management Studio Express 中没有问题。
最终解决方案
private string getFilter(int mode) {
String filter = "";
if ((ALL_WORK_ORDERS & mode) == 0) {
if ((mode & OUTSTANDING_WORK_ORDERS) == OUTSTANDING_WORK_ORDERS) {
filter += "DueDate <= #" + DateTime.Now.ToShortDateString() + "# AND DueDate IS NOT null AND FinishedDate IS null OR";
}
if ((mode & COMPLETED_WORK_ORDERS) == COMPLETED_WORK_ORDERS) {
filter += " FinishedDate IS NOT null";
}
}
filter = filter.Trim();
if (filter.EndsWith("OR")) {
filter = filter.Remove(filter.Length - 2);
}
return filter;
}