如何在今天日期之前在C#中从ms访问数据库中选择数据 [英] How to select data from ms access database in C# by today date

查看:95
本文介绍了如何在今天日期之前在C#中从ms访问数据库中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys! i want to know how to select data from ms access database in c# from today date i can select data from database between date but when i select today date and  get data it;s show me blank. so please help me.Here is my Code!

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Today Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] = #" + dateTimePicker1.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else if (textBox1.Text == "Custom Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker3.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else
            {
                MessageBox.Show("Sorry! File was not Found:Please Try Again", "Daily Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }







我尝试过的事情:






What I have tried:

Hello Guys! i want to know how to select data from ms access database in c# from today date i can select data from database between date but when i select today date and  get data it;s show me blank. so please help me.Here is my Code!

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Today Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] = #" + dateTimePicker1.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else if (textBox1.Text == "Custom Report")
            {
                DataSet dsa = new DataSet();
                DataTable dt = new DataTable();
                dsa.Tables.Add(dt);
                OleDbDataAdapter da = new OleDbDataAdapter();
                da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToString() + "# AND #" + dateTimePicker3.Value.ToString() + "#", VCON);
                da.Fill(dt);
                CrystalReport4 MYREPORT = new CrystalReport4();
                MYREPORT.SetDataSource(dt);
                crystalReportViewer1.ReportSource = MYREPORT;
                VCON.Close();
            }
            else
            {
                MessageBox.Show("Sorry! File was not Found:Please Try Again", "Daily Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



推荐答案

Google对于C#Access数据库SQL参数化查询,找出你正在做的事情是如此糟糕以及如何应对它。



您可以通过以下方式了解更多信息谷歌搜索SQL注入攻击。
Google for "C# Access database SQL parameterized queries" to find out why what you're doing is so bad and what to do about it.

You can find out more by Googling for "SQL Injection Attack".


引用:

我可以在日期之间从数据库中选择数据

i can select data from database between date

因此,您可以重新定义目标,即根据当天午夜选择数据,直到第二天午夜:

So, you can re-define your goal as selecting data based on midnight of the current day until midnight of then next day:

private TimeSpan private TimeSpan OneTick = TimeSpan.FromTicks(1L);
             
public DateTime StartOfToday()
{
    return DateTime.Today;
}

public DateTime EndOfToday()
{
    return DateTime.Today.AddDays(1).Subtract(OneTick);
}


您可能需要正确的日期值字符串表达式格式:



You may need the correct format for the string expressions of the date values:

" .. 
Where [Date] Between #" + dateTimePicker2.Value.ToString("yyyy'/'MM'/'dd") + "# And #" + dateTimePicker3.Value.ToString("yyyy'/'MM'/'dd") + "#", ..


这篇关于如何在今天日期之前在C#中从ms访问数据库中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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