Search SQL by date ASP.NET

You should parameterized your query. DateTime doesn’t have any format associated with it, Format is only useful for displaying purpose.

OleDbCommand cmd = new OleDbCommand("Select * From TEST WHERE MatchDate >= @matchDate", conn);
cmd.Parameters.AddWithValue("@matchDate", DateTime.Today); // Just date part comparision
                                                           // Or use DateTime.Now depending on your requirement)

OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);

This will save you from SQL Injection as well, and it will take care of the DateTime value.

Also instead of DateTime.Now it appears that you want to compare records greater than particular date, instead of Date and Time, Use DateTime.Today or DateTime.Now.Date. This will have Time part set to 00:00 so you can compare records against a particular Date.

You should also enclose your Command/Connection objects with using statement, since they implement IDisposable interface and will ensure connection disposal even in case of exception.

Browse More Popular Posts

Leave a Comment