【发布时间】:2017-04-04 11:17:47
【问题描述】:
我有 ms 访问表“AddDate”。两列 'Id' -Autonumber,'AddDate' -DateTime。
在 VS 2015 中,我有一个表单,其中有一个 dateTimePicker 来插入日期值。
我使用以下查询插入日期:
conn.Open();string str = "Insert into AddDate ([AddDate]) Values(@AddDate)";
OleDbCommand cmd = new OleDbCommand(str, conn);
cmd.Parameters.AddWithValue("@AddDate", dateTimePicker1.Value.ToShortDateString());
cmd.ExecuteScalar();
conn.Close();
当我输入了记录后,我想在两个日期之间搜索这些记录。 为此,我有两个 datetimepicker,我使用以下代码进入 datagridview :
OleDbDataAdapter odb = new OleDbDataAdapter("Select * from AddDate where AddDate Between '"+dateTimePicker1.Value.ToShortDateString() + "' and '"+ dateTimePicker2.Value.ToShortDateString() + "' ", conn);
DataTable dt = new DataTable();
odb.Fill(dt);
dataGridView1.DataSource = dt;
现在,如果 ms 访问表 AddDate 列设置为 DateTime,则选择查询将给出错误“条件表达式中的数据类型不匹配”。如果我在 ms 访问表中将列 DataType 属性更改为文本,则查询可以工作,但它会在两个日期之间从表中收集所有记录,而不考虑月份。
请用示例和完整代码解释。还要在 MS Access 2010 中指定 DataType 属性。
提前致谢。
问候 Manoj Yadwad。
【问题讨论】:
标签: c# ms-access-2010 select-query