如何通过数据库使用datetimepicker筛选日期 [英] How to filter date using datetimepicker via database

查看:253
本文介绍了如何通过数据库使用datetimepicker筛选日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能帮我吗,我有一个按钮用来过滤基于2个datetimepicker(datefrom和dateto)的日期范围的数据,以下是我的代码,当我单击该按钮时,它什么也不显示.你能帮我什么问题吗?我是C#中的新手.

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. can you help me what is the problem? im new in 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();

推荐答案

大家好,我已经弄清楚了缺少的链接.这是更正的脚本.谢谢大家!

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

这篇关于如何通过数据库使用datetimepicker筛选日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆