如何使用datetimepicker通过数据库过滤日期

时间:2016-06-10 05:08:32

标签: c# mysql

你能帮助我吗,我有一个按钮来过滤数据,日期范围基于我的2 datetimepicker(datefrom和dateto),下面是我的代码,当我点击按钮时它什么也没显示。你能帮我解决一下这个问题吗?我是C#中的新人。

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 '" + datefrom.Value.ToString() + "' and '" + dateto.Value.ToString() + "' ", mcon);
mcon.Open();
DataSet ds = new DataSet();
mda.Fill(ds);
dbgrid1.DataSource = ds;
dbgrid1.Refresh();
mcon.Close();

5 个答案:

答案 0 :(得分:2)

您需要将dateFromdateTo值转换为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();

答案 1 :(得分:1)

缺少此行:

MySqlCommandBuilder mcb = new MySqlCommandBuilder(mda);

来源:

  

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

所以代码应该是:

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();

答案 2 :(得分:1)

您的数据库的date值为date type,并且您将stringdate进行比较,但不匹配。

您需要convert startend日期为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') "

您需要指定存储在数据库中的确切格式来代替'%m/%d/%Y'

  

使用参数化Sql

您当前的代码向Sql injection开放,最好使用parameterized sql。请勿将值add parameters连接到查询。

答案 3 :(得分:1)

我认为你需要将datefrom和dateto值转换为Database Date格式并输入

答案 4 :(得分:1)

大家好我已经找到了缺失的链接。这是更正后的脚本。谢谢大家!

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();