How to filter date using datetimepicker via database

后端 未结 5 1001
说谎
说谎 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:19

    i think you need to convert your datefrom and dateto values into Database Date format and type

    0 讨论(0)
  • 2021-01-16 06:25

    Your database has the date value as date type and you are comparing the string with date which will not match.

    You need to convert start and end date as date

    select * from bio_db.daily_data2 where Date between STR_TO_DATE('" + datefrom.Value.ToString() + "','%m/%d/%Y') and STR_TO_DATE('" + dateto.Value.ToString() + "','%m/%d/%Y') "
    

    You will need to specify exact format which is stored in database in place of '%m/%d/%Y'.

    Use Parameterized Sql

    Your current code is open to Sql injection it would be better to use parameterized sql. Don't concatenate values instead add parameters to query.

    0 讨论(0)
  • 2021-01-16 06:25

    This line is missing:

    MySqlCommandBuilder mcb = new MySqlCommandBuilder(mda);
    

    Sources:

    https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-data-adapter.html http://www.techonthenet.com/mysql/between.php

    So the code should be:

    MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=8888;password=888888");
    string query = string.Format("select * from bio_db.daily_data2 where Date "
                               + "BETWEEN CAST('{0}' AS DATE) AND CAST('{1}' AS DATE) ", 
                                 dateFrom.Value.ToString("M/d/yyyy"), 
                                 dateTo.Value.ToString("M/d/yyyy"));
    MySqlDataAdapter mda = new MySqlDataAdapter(query, mcon);
    MySqlCommandBuilder mcb = new MySqlCommandBuilder(mda); //added code
    mcon.Open();
    DataTable dt = new DataTable();
    mda.Fill(dt);
    dbgrid1.DataSource = dt;
    dbgrid1.Refresh();
    mcon.Close();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2021-01-16 06:40

    Hi guys i already figure out the missing link. Here's the corrected script. Thanks everyone!

    DateTime dtFrom = Convert.ToDateTime(datefrom.Text); //some DateTime value, e.g. DatePicker1.Text;
    DateTime dtTo = Convert.ToDateTime(dateto.Text); //some DateTime value, e.g. 
    DatePicker1.Text;<n>MySqlConnection mcon = new MySqlConnection("datasource=localhost;port=3306;username=8888;password=8888888");
    MySqlDataAdapter mda = new MySqlDataAdapter("select * from bio_db.daily_data2 where Date between '" + dtFrom.ToString("yyyy/MM/dd") + "' and '" + dtTo.ToString("yyyy/MM/dd") + "' ", mcon);
    
            System.Data.DataSet ds = new System.Data.DataSet();
            mcon.Open();
            mda.Fill(ds, "root");
            dbgrid1.DataSource = ds.Tables["root"];
            dbgrid1.Refresh();
            mcon.Close();
    
    0 讨论(0)
提交回复
热议问题