Dear friends, do you compare datetime. Now?

Source: Internet
Author: User

The project hasRetrieve PasswordFunction, I am very happy to do it by myself, hey.

There is no problem after testing many times. The results were accidentally discovered by others.DatabaseInExpiration time (expireddate)AndCurrent Time (datetime. Now)Exist4 minutes or moreDifference", I always takeDatetime. NowThe current time is compared!

First, let's take a look at the following line.Code,

 
 VaRModelcode = loadrepository. getentities <entity. verification_code> (). Where (I => I. Code. Equals (CODE) & I. expireddate> =Datetime. Now). Firstordefault ();

According to SQL Server Profiler, I found that: = "[extent1]. [expireddate]> = getdate ():

Maybe you think there may be no problem, but I looked at the SQL parsed by the query analyzer, And the datetime. Now in the obtained SQL is changed to getdate (). What is the problem;

Exec sp_executesql n'Select top (1)[Extent1]. [verificationid] as [verificationid], [extent1]. [userid] as [userid], [extent1]. [Code] as [Code], [extent1]. [adddate] as [adddate], [extent1]. [expireddate] as [expireddate], [extent1]. [Status] as [Status] from [DBO]. [verification_code] as [extent1] Where ([extent1]. [Code]= @ P _ LINQ _ 0) and ([extent1]. [expireddate] >=( getdate ()))
', N'@ P _ LINQ _ 0 varchar (8000)',
@ P _ LINQ _ 0 ='F784222508ce4e859108fd0c8b95df8f'

So I tried to select getdate (); and found that there is an error between the real getdate () and the current time.

 

I may understand,Datetime. NowIn the WHERE clause, comparison is not allowed. Because LINQ can only be compared with a value. Therefore, you must convert it to a value first.

Modify as follows:

 
VaR Datetime=Datetime. Now;VaRModelcode = loadrepository. getentities <entity. verification_code> (). Where (I => I. Code. Equals (CODE) & I. expireddate> =Datetime). Firstordefault ();

Check the SQL statement according to the SQL Server Profiler. The problem is solved, and datetime. Now is back.@ P _ LINQ _ 1 ='2013-05-28 15:25:37.8470000'

Exec sp_executesql n '  Select top (1)  [Extent1]. [verificationid] as [verificationid], [extent1]. [userid] as [userid], [extent1]. [Code] as [Code], [extent1]. [adddate] as [adddate], [extent1]. [expireddate] as [expireddate], [extent1]. [Status] as [Status] from [DBO]. [verification_code] as [extent1] Where ([extent1]. [Code] = @ P _ LINQ _ 0) and ([extent1]. [expireddate] >=@ P _ LINQ _ 1)'  ,
N ' @ P _ LINQ _ 0 varchar ( 8000 ), @ P _ LINQ _ 1 datetime ' ,
@ P _ LINQ _ 0 = ' F784222508ce4e859108fd0c8b95df8f ' ,
@ P _ LINQ _ 1 = ' 2013 - 05 - 28 15 : 25 : 37.8470000 '

Summary:I hope you don't want to cook like me. Maybe I am the only one to make this mistake. The details really make people feel that learning is a pleasure!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.