简体   繁体   中英

Date condtion doesn't work on my application but work in SQL Server

I have a weird trouble with a request. I have a table with a field 'datetime' named 'DATE' .

Here an exactly exemple of one value : 2015-03-09 00:00:00.000

I do in my application (in C#) a request like this : 'SELECT ... WHERE DATE BETWEEN datedeb AND datefin'

Here an exactly exemple of the value of datedeb : 09/03/2015

When i do the request in my application, it doesn't work at all, no results.

When i copy the request (via MessageBox.Show()) in SQL Server, it works.

When i change datedeb and datefin to 2015-03-09 in my application, it doesn't work.

When i change in SQL Server, it works.

I really don't know where is the problem ... have you some ideas ?

Like this:

 SqlCommand command = new SqlCommand("Select * from whatever where Date Between @begin and @end");
 command.Parameters.Add(new SqlParameter("begin", yourbegin));
 command.Parameters.Add(new SqlParameter("end", yourEnd));
 ...

yourBegin and yourEnd are of type DateTime...

Try using this:

SqlCommand command = new SqlCommand("SELECT * FROM TABLE WHERE DATE BETWEEN @begin and @end");
command.Parameters.Add("@begin", SqlDbType.DateTime).Value = DateTime.Now;
command.Parameters.Add("@end", SqlDbType.DateTime).Value = DateTime.Now.AddDays(1);

I thinik, it is better if you can send the parameter as DateTime type so you don't have to deal with locale problem.

Ok guys, i'm a really stupid boy. You know where was the problem ? I forgot to write the database i want to use in my ConnectionString. And i have several database who are very similar. My application was searching in an other table ... I'm very stupid ! Thank you a lot for your time, you're awesome, specially @Florian Schmidinger Sorry for being stupid lol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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