按日期c#winform数据gridview查询Ms Access

时间:2018-05-20 14:25:18

标签: c# ms-access datagridview

我使用以下代码使用MS Access数据库中的dataadapter填充datagridview,它不会给出任何错误,但不会返回任何值

这是代码

 private void button1_Click_1(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;User Id=;Password=;Data Source=TS-POS.accdb");
        conn.Open();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT InvoiceNo, DayDate, Gross, Net, Staff FROM GrossSales Where DayDate = #" + dateFrom.Value.ToString("MM'/'dd'/'yyyy") + "#;", conn);
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        dataGridView2.DataSource = ds.Tables[0];
        dataGridView2.Columns["InvoiceNo"].HeaderText = "رقم الفاتورة";
        dataGridView2.Columns["InvoiceNo"].Width = 150;
        dataGridView2.Columns["DayDate"].HeaderText = " التاريخ";
        dataGridView2.Columns["DayDate"].Width = 350;
        dataGridView2.Columns["Gross"].HeaderText = "الإجمالي";
        dataGridView2.Columns["Gross"].Width = 150;
        dataGridView2.Columns["Net"].HeaderText = " الصافي";
        dataGridView2.Columns["Net"].Width = 150;
        dataGridView2.Columns["Staff"].HeaderText = " الموظف";
        dataGridView2.Columns["Staff"].Width = 150;
        conn.Close();
    }

以下是它在数据库中的显示方式

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用:

OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT InvoiceNo, DayDate, Gross, Net, Staff FROM GrossSales Where DayDate >= #" + dateFrom.Value.ToString("yyyy'/'MM'/'dd") + "# And DayDate < #" + dateFrom.Value.AddDays(1).ToString("yyyy'/'MM'/'dd") + "#;", conn);