BlackAlpha
BlackAlpha

Reputation: 376

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 ?

Upvotes: 0

Views: 62

Answers (3)

BlackAlpha
BlackAlpha

Reputation: 376

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

Upvotes: 0

Abe Baehaki
Abe Baehaki

Reputation: 61

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.

Upvotes: 0

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

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...

Upvotes: 1

Related Questions