简体   繁体   English

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

[英]executing SELECT statement with date in WHERE clause fails

I'm building a dynamic query in my ASP.NET MVC project by the following: 我通过以下方式在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

Where basically the strWhere is a string that I build up a dynamic filter depending on what the user selects to search on. 基本上strWhere是一个字符串,我根据用户选择搜索的内容构建了一个动态过滤器。

This works great until I need to add a date comparison to the date clause. 在我需要向date子句添加日期比较之前,这非常有用。

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: 但是当我尝试使用ToList执行查询时,只要日期在where字符串中,我都会收到以下错误:

The argument types 'Edm.DateTime' and 'Edm.Int32' are incompatible for this operation. 此操作的参数类型'Edm.DateTime'和'Edm.Int32'不兼容。 Near WHERE predicate, line 1, column 103. 在WHERE谓词附近,第1行,第103列。

Any ideas on what my issue is? 关于我的问题有什么想法吗?

Thanks in advance 提前致谢

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. 10/09/1992被解释为一个Int值。

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: 例如:在Oracle中,我们使用to_date函数使用给定的模式将字符串转换为日期时间

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

In SQL Server we have the convert function as in: 在SQL Server中,我们具有如下convert功能:

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

More samples here . 这里有更多样本。

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). 您可以使用linq功能来生成查询(它“知道”如何为数据库查询生成datetime参数)。

Check a sample here 在这里检查样品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM