Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select query BETWEEN two dates in WindowsForms Application c#

I have a problem with the query BETWEEN .I'm trying to select records from a table between two dates, SO, I used the following query :

SqlDataAdapter sda1 = new SqlDataAdapter(
  "select distinct * from BLC where DATE_BLC between '" + 
   dateTimePicker1.Value.ToString() + "' and'" + 
   dateTimePicker2.Value.ToString() + "'", conx);

When I enter the following dates :

From date 05/02/2016 To date 15/03/2016

It returns the records between (06/02/2016 to 15/03/2016) date but the records that begin with date 05/02/2016 , they are not returned. and when I choose the date (From 05/02/2016 to 05/02/2016 ) there is no records here. Can someone tell me what I'm doing wrong here?

like image 716
Y.Arsoy Avatar asked Feb 15 '26 23:02

Y.Arsoy


1 Answers

It is a better codding practice to use parameters as they will sterilize any inputs for any queries and are generally safer to user. Thus the code would become

SqlDataAdapter sda1 = new SqlDataAdapter("select distinct * from BLC where DATE_BLC between @Date1 and @Date2", conx);
sda1.SelectCommand.Parameters.Add(new SqlParameter("@Date1", dateTimePicker1.Value));
sda1.SelectCommand.Parameters.Add(new SqlParameter("@Date2", dateTimePicker2.Value));
like image 195
Shon Avatar answered Feb 17 '26 14:02

Shon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!