Parameter Query using 2 dates only using one of the dates

LabProARW

Well-known member
Joined
Nov 4, 2019
Messages
52
Programming Experience
1-3
Accessing an ACCESS database back end.

DateTime searchDate = DateTime.Now.AddDays(0);
DateTime searchDate2 = DateTime.Now.AddDays(-170);

Using OleDbCommand cmd;
cmd = new OleDbCommand("SELECT * FROM tbl_Sample_Login_table WHERE LoginDate BETWEEN @nowDate AND @thenDate);
cmd.Parameters.Add("@nowDate", OleDbType.DBDate).Value = searchDate;
cmd.Parameters.Add("@thenDate", OleDbType.DBDate).Value = searchDate2;
dbConnection.Open();
dataAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();

** The crazy thing is I keep getting results which only account for searchDate2 and ignores my now search date!? Watching the execution my now date shows today... but when the data set loads it uses some other date every time!
Can anyone tell me why only one of the "BETWEEN" dates is actually getting used in the SELECT query?

Thanks for all responses.
 
By the way, I'm not sure that swapping them over in the SQL statement would work. AFAIAA the Access driver does understand @named parameters to be parameters but the actual names are ignored; position is still important. In the OP's code dates are being added to the parameters collection in the wrong order compared to appearance order of parameter placeholders; notionally all @named parameters are converted to ? parameter placeholders and then the regular rules apply
True. I guess I was assuming that they would be added to the command in the same order they appeared in the query but I should have stated that explicitly.
 
Back
Top Bottom