简体   繁体   English

MS Access 2007 - 从数据库C#中选择数据

[英]MS Access 2007 - Select data from Database C#

Good Morning all, i have a problem when display data from my mdb database. 早安所有,我在显示mdb数据库中的数据时遇到问题。

My Code : 我的代码:

    private void btn_Preview_Click(object sender, EventArgs e)
    {
        //MessageBox.Show(dateTimePicker1.Value.ToShortDateString());
        dataGridView1.Refresh();
        string sql = "SELECT * FROM DATA where tgl BETWEEN #01/01/2017# AND #30/01/2017# order by tgl  Asc";
        //string sql = "SELECT * FROM DATA where tgl = #07/01/2017#";
        OleDbConnection conn = new OleDbConnection(koneksi);
        conn.Open();
        OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "DATA");
        conn.Close();
        dataGridView1.DataSource = ds.Tables["DATA"].DefaultView;


    }

when i run the program 当我运行程序

string sql = "SELECT * FROM DATA where tgl BETWEEN #01/01/2017# AND #30/01/2017# order by tgl  Asc";

this is the result: Result when use show Between date 这是结果:使用show时的结果日期之间

使用时的结果显示日期之间

then i try to run with specific date : 然后我尝试以特定日期运行:

string sql = "SELECT * FROM DATA where tgl = #07/01/2017#";

使用特定日期

no record appear. 没有记录出现。

when i change #07/01/2017# to #29/12/2016# the data is loaded to Datagridview1 当我将#07/01/2017#更改为#29/12/2016#时,数据将加载到Datagridview1

Please tell me what wrong with my code. 请告诉我我的代码有什么问题。 when i use #07/01/2017# the data can't loaded to datagridview, but when i use date between 01/01/2017 and 30/01/2017 the data is loaded and contain 07/01/2017. 当我使用#07/01/2017#时,数据无法加载到datagridview,但是当我在01/01/2017到30/01/2017之间使用日期时,数据已加载并包含07/01/2017。

Thanks 谢谢

Check the date format, it is the most probable cause of this irregularity. 检查日期格式,这是造成这种不规则性的最可能原因。 Try 01/07/2017 instead. 请尝试01/07/2017。 You can also change the date format in control panel -> clock language, and region, so you can have the date/time format of your choice. 您还可以在控制面板 - >时钟语言和区域中更改日期格式,以便您可以选择日期/时间格式。

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

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