Select 查询 C# 中发生类型 'System.Data.OleDb.OleDbException' 的未处理异常
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in Select query C#
我在 MS Access 数据库 (.mdb
) 中有一个 table,它有一堆列。我 运行 以下 SQL 查询并将 table 中的所有内容返回到我的 C# 应用程序 (Visual Studio 2012 Express)。
Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term
From TransAll
Where Term = 'Invalid'
现在我正在尝试向 where
子句添加更多条件。我正在尝试在 table 名称 DateTime 上设置一个日期 运行ge,使其介于特定的 运行ge 之间。
这是我通过代码创建的 SQL select 语句:
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 select 命令,然后修复代码。该代码在 SQL 服务器数据库中运行。但是在我现在的 C# 代码中出现错误:
Unhandled exception of type system.data.oledb.oledbexception occurred in system.data.dll. Additional information: Data type mismatch in criteria expression.
这是我的问题代码:
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");
如有任何帮助,我们将不胜感激。
如@Plutonix 所述,使用参数而不是字符串连接来创建语句将大有帮助。它还将避免 sql 注入攻击。
string query = "Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term from TransAll where Term = @Term And DateTime Between @Startdate and @Enddate";
var cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.Connection = sqlConnection;
cmd.Parameters.Add("@Term", OleDbType.VarChar).Value = termString;
cmd.Parameters.Add("@Startdate", OleDbType.DBTimeStamp).Value = startDate; // of type date
cmd.Parameters.Add("@Enddate", OleDbType.DBTimeStamp).Value = endDate; // of type date
这样您就可以知道哪里也可能发生错误。请尝试处理sql连接等
要使您的 SQL 正常工作,请对日期值的字符串表达式使用 Access 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#
但是,如前所述,使用参数。更容易调试。
我在 MS Access 数据库 (.mdb
) 中有一个 table,它有一堆列。我 运行 以下 SQL 查询并将 table 中的所有内容返回到我的 C# 应用程序 (Visual Studio 2012 Express)。
Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term
From TransAll
Where Term = 'Invalid'
现在我正在尝试向 where
子句添加更多条件。我正在尝试在 table 名称 DateTime 上设置一个日期 运行ge,使其介于特定的 运行ge 之间。
这是我通过代码创建的 SQL select 语句:
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 select 命令,然后修复代码。该代码在 SQL 服务器数据库中运行。但是在我现在的 C# 代码中出现错误:
Unhandled exception of type system.data.oledb.oledbexception occurred in system.data.dll. Additional information: Data type mismatch in criteria expression.
这是我的问题代码:
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");
如有任何帮助,我们将不胜感激。
如@Plutonix 所述,使用参数而不是字符串连接来创建语句将大有帮助。它还将避免 sql 注入攻击。
string query = "Select OperatorNo, DateTime, FuelIssued, PreviousCredit, Term from TransAll where Term = @Term And DateTime Between @Startdate and @Enddate";
var cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.Connection = sqlConnection;
cmd.Parameters.Add("@Term", OleDbType.VarChar).Value = termString;
cmd.Parameters.Add("@Startdate", OleDbType.DBTimeStamp).Value = startDate; // of type date
cmd.Parameters.Add("@Enddate", OleDbType.DBTimeStamp).Value = endDate; // of type date
这样您就可以知道哪里也可能发生错误。请尝试处理sql连接等
要使您的 SQL 正常工作,请对日期值的字符串表达式使用 Access 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#
但是,如前所述,使用参数。更容易调试。