在 WHERE 子句中执行带有日期的 SELECT 语句失败

发布于 2024-09-19 02:38:35 字数 1070 浏览 8 评论 0原文

我正在我的 ASP.NET MVC 项目中通过以下方式构建动态查询:

Dim queryString As String = "SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos"
If strWhere <> "" Then
    queryString = queryString & " WHERE " & strWhere
End If

' Call the constructor with the specified query and the ObjectContext. 
Dim SearchUsersQuery As New System.Data.Objects.ObjectQuery(Of UserInformation)(queryString, MyDB)

Dim lstOfUsers As List(Of UserInformation) = SearchUsersQuery.ToList

其中 strWhere 基本上是一个字符串,我根据用户选择搜索的内容构建动态过滤器。

这非常有效,直到我需要向日期子句添加日期比较。

我正在尝试以下操作:

strWhere = " userInfos.BirthDate <= " & StartDateForQuery.ToShortDateString

最终结果为:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos Where userInfos.BirthDate <= 10/09/1992"

但是当我尝试使用 ToList 执行查询时,只要日期位于 where 字符串中,我就会收到以下错误:

参数类型“Edm.DateTime”和“Edm.Int32”与此操作不兼容。靠近 WHERE 谓词,第 1 行,第 103 列。

对我的问题有什么想法吗?

提前致谢

I'm building a dynamic query in my ASP.NET MVC project by the following:

Dim queryString As String = "SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos"
If strWhere <> "" Then
    queryString = queryString & " WHERE " & strWhere
End If

' Call the constructor with the specified query and the ObjectContext. 
Dim SearchUsersQuery As New System.Data.Objects.ObjectQuery(Of UserInformation)(queryString, MyDB)

Dim lstOfUsers As List(Of UserInformation) = SearchUsersQuery.ToList

Where basically the strWhere is a string that I build up a dynamic filter depending on what the user selects to search on.

This works great until I need to add a date comparison to the date clause.

I'm trying the following:

strWhere = " userInfos.BirthDate <= " & StartDateForQuery.ToShortDateString

Which will end up as:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos Where userInfos.BirthDate <= 10/09/1992"

But when i try to execute the query with the ToList whenever a date is in the where string i get the following error:

The argument types 'Edm.DateTime' and 'Edm.Int32' are incompatible for this operation. Near WHERE predicate, line 1, column 103.

Any ideas on what my issue is?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2024-09-26 02:38:35

您不能以这种方式比较日期。

当您执行此操作时:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= 10/09/1992"

10/09/1992 将被解释为 Int 值。

尝试在该值周围加上单引号,如下所示:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= '10/09/1992'"

可能您必须调用数据库日期转换函数(取决于您的数据库供应商)将此日期字符串传递给它。

像这样:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= DataBaseSpecificToDateFunction('10/09/1992')"

问题是这个查询被发送到数据库并且是执行它的数据库服务器。这就是为什么您需要一个数据库特定的日期转换函数。

例如:在 Oracle 中,我们有 to_date 函数,可以使用给定模式将字符串转换为日期时间

to_date('1998/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam')

在 SQL Server 中,我们有 < code>convert 函数如下:

convert(datetime, '2016-10-23 20:44:11',20) -- yyyy-mm-dd hh:mm:ss(24h)

更多示例此处

You can't compare dates that way.

When you do this:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= 10/09/1992"

10/09/1992 is being interpreted as an Int value.

Try putting single quotes around this value as in:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= '10/09/1992'"

Probably you'll have to call a database date conversion function (depends on your database vendor) passing to it this date string.

Like this:

"SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos 
 WHERE userInfos.BirthDate <= DataBaseSpecificToDateFunction('10/09/1992')"

The problem is that this query is being sent to the database and is the database server that'll execute it. That's why you need a database specific date conversion function.

For example: in Oracle we have the to_date function to convert a string to a datetime using a given pattern:

to_date('1998/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam')

In SQL Server we have the convert function as in:

convert(datetime, '2016-10-23 20:44:11',20) -- yyyy-mm-dd hh:mm:ss(24h)

More samples here.

逆夏时光 2024-09-26 02:38:35

为什么不使用下一个代码:

Dim queryString As String = "SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos"
' Call the constructor with the specified query and the ObjectContext. 
'
Dim SearchUsersQuery As New System.Data.Objects.ObjectQuery(Of UserInformation)(queryString, MyDB)

'verify if should be a startdate for adding to query
'
If StartDateForQuery <> "" Then
 'add the condition to the query
    '

 SearchUsersQuery = SearchUsersQuery.Where("it.BirthDate  <= @BirthDate ")

 'add the parameter to be used
    '
 SearchUsersQuery.Parameters.Add(New ObjectParameter("BirthDate ", StartDateForQuery))
End If

Dim lstOfUsers As List(Of UserInformation) = SearchUsersQuery.ToList

您利用 linq 功能来生成查询(它“知道”如何为数据库查询生成日期时间参数)。

此处查看示例

Why don't you use the next code:

Dim queryString As String = "SELECT VALUE userInfos FROM MyDBEntities.UserInformations AS userInfos"
' Call the constructor with the specified query and the ObjectContext. 
'
Dim SearchUsersQuery As New System.Data.Objects.ObjectQuery(Of UserInformation)(queryString, MyDB)

'verify if should be a startdate for adding to query
'
If StartDateForQuery <> "" Then
 'add the condition to the query
    '

 SearchUsersQuery = SearchUsersQuery.Where("it.BirthDate  <= @BirthDate ")

 'add the parameter to be used
    '
 SearchUsersQuery.Parameters.Add(New ObjectParameter("BirthDate ", StartDateForQuery))
End If

Dim lstOfUsers As List(Of UserInformation) = SearchUsersQuery.ToList

You make use of the linq capabilities to generate queries (it "knows" how to generate the datetime parameter for DB query).

Check a sample here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文