views:

72

answers:

2

I have a table called Recharge in a Access Database.

Some of the fields are RechargeDate, Account, Number, etc.

I wanted to retrieve all records between two dates so I wrote the following query:

string Query = "select * from Recharge where Account='" + comboBox1.Text + "' 
and RechargeDate between '"+dateTimePicker1.Value.Date.ToShortDateString()+"' and '"+dateTimePicker2.Value.Date.ToShortDateString()+"'"

The query runs fine but the only problem I've run in to is that I am only able to retrieve dates from a single Month.

If I request records from a span that encompasses more than a single month, I do not get the proper result.

Any help?

the query executes nice but the problem is i can only able to get details between the dates in single month, if the starting month and ending month differs i cant get the proper result plzz help me

A: 

I've used this with success, Recharge >= Date1 and Recharge <= Date2 I haven't used between maybe it is more efficent, but I use the former.

Nate Bross
>= and <= should be optimized exactly the same as Between/And. If you don't want it to be inclusive, you can't use Between/And with Jet/ACE. Other db engines may treat Between/And differently, though.
David-W-Fenton
+2  A: 

Maybe you're running into some string date format issue; try with this code:

OleDbCommand command = new OleDbCommand(
    "SELECT * FROM Recharge " + 
    "WHERE Account=@Account and " + 
    "RechargeDate between @RechargeDateStart AND @RechargeDateEnd");
command.Parameters.AddWithValue("@Account", comboBox1.Text);
command.Parameters.AddWithValue("@RechargeDateStart",dateTimePicker1.Value.Date);
command.Parameters.AddWithValue("@RechargeDateEnd"  ,dateTimePicker2.Value.Date);

BTW, you shouldn't try to concatenate SQL commands, as this can lead to SQL injection attacks.

Rubens Farias