How to filter date using datetimepicker via database

后端 未结 5 1037
说谎
说谎 2021-01-16 06:16

Can you help me, i have a button to filtered data with date range base on my 2 datetimepicker (datefrom and dateto), Below is my code, and when i click the button it display

5条回答
  •  情歌与酒
    2021-01-16 06:32

    You need to convert your dateFrom and dateTo values into DateTime

    DateTime dtFrom =Convert.ToDateTime(DatePicker1.Text); //some DateTime value, e.g. DatePicker1.Text;
    DateTime dtTo =Convert.ToDateTime(DatePicker2.Text); //some DateTime value, e.g. DatePicker1.Text;
    MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=8888;password=888888");
    MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.daily_data2 where Date between '" + dtFrom.ToString("MM/dd/yyyy")+ "' and '" + dtTo.ToString("MM/dd/yyyy") + "' ", mcon);
    DataSet ds = new DataSet();
    mda.Fill(ds);
    dbgrid1.DataSource = ds;
    dbgrid1.Refresh();
    mcon.Close();
    

提交回复
热议问题