Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Winform with 2 date time pickers that I search a date range in a Access database.

All was working well until today. When I search from 9/24/18 to 10/1/18 I don’t get any results.

The reason I’m guessing is the numeric order of the database.

Example : Dates are sorted based on the individual digits that make up the value, instead of on the numeric value. For example, the value 10/1/18 appears before 9/24/18. My Access database for the Date is set to Date. This is the Code

Thanks

C#
private void btn_Range_Search_Click_1(object sender, EventArgs e)
        {
            try
            {
                string queryString = "SELECT HotSheetID, Today, Part, Timeord, Timerec, sdock, LCCN, Requestor, Notes, Type, Shift, RunOutTime, CICSTYPE FROM ILC,Reasontype WHERE Reasontype.typeID = ILC.typeID";
              
                queryString += string.Format(" AND ILC.Today BETWEEN '{0}' AND '{1}' ", dt3.Text, dt4.Text);  

                                
                loadDataGrid(queryString);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show("You must Refresh first before you can Search again!");
                return;

            }
        }


What I have tried:

Internet lookup
C#
queryString += string.Format(" AND ILC.Today BETWEEN '#{0}#' AND '#{1}#' ", dt3.Text, dt4.Text);  queryString += string.Format(" AND ILC.Today BETWEEN '{0}' AND '{1}' ", dt3.Text, dt4.Text);
Posted
Updated 1-Oct-18 11:49am
Comments
j snooze 1-Oct-18 17:43pm    
would something like this work? You might be able to pass your string values to the new DateTime, I can't remember off the top of my head. Either way a true parameterized query would be best vs string concatenation.

https://stackoverflow.com/questions/19519831/c-sharp-and-ms-access-queries-with-date-range-issue

1 solution

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Plus ... using parameters means that the value is passed as a DateTime object that SQL understands, so it does not have to make assumptions about date formats, which is causing the problem you have noticed...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900