简体   繁体   English

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

[英]How to filter date using datetimepicker via database

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 displays nothing.你能帮我吗,我有一个按钮来过滤数据,基于我的 2 个日期时间选择器(datefrom 和 dateto),下面是我的代码,当我点击按钮时,它什么也不显示。 can you help me what is the problem?你能帮我解决什么问题吗? im new in C#.我是 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();

You need to convert your dateFrom and dateTo values into DateTime您需要将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();

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

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

Your database has the date value as date type and you are comparing the string with date which will not match.您的数据库将date值作为date type并且您正在将string与不匹配的date进行比较。

You need to convert start and end date as date您需要convert startend日期convertdate

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' .您需要指定存储在数据库中的确切格式来代替'%m/%d/%Y'

Use Parameterized Sql使用参数化的 Sql

Your current code is open to Sql injection it would be better to use parameterized sql .您当前的代码对Sql injection开放,最好使用parameterized sql Don't concatenate values instead add parameters to query.不要连接值,而是向查询add parameters

我认为您需要将 datefrom 和 dateto 值转换为数据库日期格式和类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM