SQL Server 日期函数
当我将其添加到我的 where 子句中时:
DueDate <= getDate() AND DueDate IS NOT null
我得到一个不错的小失败,上面写着: 无法创建子项:System.Reflection.TargetInitationException:调用目标已引发异常。 ---> 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;
}
When I add this to my where clause:
DueDate <= getDate() AND DueDate IS NOT null
I get a nice little failure that says:
Could not create child: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.EvaluateException: The expression contains undefined function call getDate().
Any idea what I'm doing wrong?
Update
Code in use:
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;
}
And it is getting used here:
tblWorkOrderBindingSource.Filter = getFilter(mode);
In the table, DueDate is type datetime.
Side note:
I can run
SELECT [ID]
,[WorkDesc]
,[DueDate]
FROM [RentalEase].[dbo].[tblWorkOrder]
WHERE [DueDate] <= getDate() AND [DueDate] IS NOT null
in the MS SQL Server Management Studio Express without a problem.
Final Solution
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个异常很清楚地表明这里出了什么问题。 您指定的方法 getdate 不能在过滤表达式中使用。 请参阅有关 DataColumn 类的 Expression 属性的文档,了解哪些内容有效,哪些内容无效:
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
也就是说,您需要连接 getdate 而不是 getdate将日期添加到过滤器中(调用 DateTime 结构上的静态 Now 属性来获取当前日期)。 上面的链接将向您展示如何正确设置日期格式(您不能只按照凯利的答案中所示的方式进行操作,尽管该答案大部分都是如此)。
The exception is pretty clear on what is wrong here. You are specifying a method, getdate, which can't be used in a filter expression. See the documentation on the Expression property on the DataColumn class for what is valid and what is not:
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
That being said, instead of getdate, you need to concatenate the date into the filter (call the static Now property on the DateTime structure to get the current date). The link above will show you how to format dates properly (you can't just do it as indicated in Kelly's answer, although that answer is most of the way there).
你有没有尝试过
have you tried
此消息不是来自 Sql Server。 我认为您实际上是在该字符串中编写 C# 并在运行时针对 .net 类型(而不是表定义)对其进行编译/解释。
另外,SQLServer 2005 在 || 方面存在问题。 和&&...
This message did not come from Sql Server. Methinks you are actually writing C# in that string and getting it compiled/interpretted at runtime against .net types (instead of table defs).
Also, SQLServer 2005 has trouble with || and &&...
那应该是 T-SQL 吗? 尝试这个
Is that supposed to be T-SQL? try this
如果这应该是 sql,那么 "||" 不是 "OR" 的符号,并且 "&&" 也不是AND 的符号。
If this is supposed to be sql then "||" is not the symbol for "OR" and "&&" is not the symbol for AND.