如何使用ms访问在C#中的两个日期之间选择数据

时间:2016-09-08 20:27:58

标签: c# database datetime datagridview report

我想知道如何在我使用查询的两个日期之间选择数据,但我得到空数据请帮助我 我尝试过:

DataSet dsa = new DataSet();
DataTable dt1 = new DataTable();
dsa.Tables.Add(dt1);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person] from [Total] Where [Date] between " + dateTimePicker2.Value.ToShortDateString() + " AND #" + dateTimePicker3.Value.ToShortDateString() + "#", VCON);
da.Fill(dt1);

2 个答案:

答案 0 :(得分:2)

像史蒂夫说的那样,你可以使用如下参数:

DataSet dsa = new DataSet();
DataTable dt1 = new DataTable();
dsa.Tables.Add(dt1);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person] from [Total] Where [Date] between @DateStart AND @DateEnd", VCON);
da.SelectCommand.Parameters.AddWithValue("@DateStart", dateTimePicker2.Value);
da.SelectCommand.Parameters.AddWithValue("@DateEnd", dateTimePicker3.Value);
da.Fill(dt1);

答案 1 :(得分:0)

很明显,为什么你没有看到你要查询的数据而没有检索到任何结果... ...实际上两个选定日期之间是否有任何数据?

作为一个快速观察,你似乎错过了几个"#" dateTimePicker2周围的字符:

"SELECT [Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person] from [Total] Where [Date] between #" + dateTimePicker2.Value.ToShortDateString() + "# AND #" + dateTimePicker3.Value.ToShortDateString() + "#"