【发布时间】:2018-09-19 14:43:05
【问题描述】:
我在 MS Access 数据库 (.mdb) 中有一个表,其中包含一堆列。我运行了以下 SQL 查询,它将表中的所有内容返回到我的 C# 应用程序 (Visual Studio 2012 Express)。
Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term
From TransAll
Where Term = 'Invalid'
现在我正在尝试向where 子句添加更多条件。我正在尝试将表名 DateTime 上的日期范围设置在特定范围之间。
这是我通过代码创建的 SQL 选择语句:
Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term
From TransAll
Where Term = 'Invalid'
And DateTime Between '2018/6/24 00:00:00' and '2018/6/30 23:59:59'
我将 access 数据库导入 SQL Server express,这样我就可以手动找出 SQL 选择命令,然后修复代码。该代码在 SQL Server 数据库中运行。但是在 C# 代码中,我现在遇到了一个错误:
system.data.dll 中发生未处理的 system.data.oledb.oledbexception 类型的异常。附加信息:条件表达式中的数据类型不匹配。
这是我的代码:
string tmpString = strSelectQuery + strWhereClause;
private DataSet myDS = new DataSet();
oledbDataAdapter MyAdapter = new oledbDataAdapter(tmpString, MyConnection);
MyAdapter.Fill(myDS); //this is where the error happens when I run my code
dgvResults.AutoGenerateColumns = true;
dgvResults.AutoSize = true;
dgvResults.DataSource = myDS;
dgvResults.DataMember = "Table";
dgvResults.ReadOnly = true;
if (dgvResults.RowCount == 0)
MessageBox.Show("No Data for that Date Range/Week");
任何帮助将不胜感激。
【问题讨论】:
-
在伪 DateTime 数据周围打勾表示不是 DateTime 的文本。始终使用 SQL 参数。总是