简体   繁体   English

选择查询C#查询中的条件表达式中的数据类型不匹配错误

[英]Data Type Mismatch error in Criteria expression in Select query C# query

My sample code is as follows, I am getting following error; 我的示例代码如下,出现以下错误;

Data Type Mismatch error in criteria expression. 条件表达式中的数据类型不匹配错误。

Details => ScannerAlarmLimits is my table from .mdb database. 详细信息=> ScannerAlarmLimits是我的.mdb数据库表。

string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
string sqlQuery1S = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID='" +select1S+ "'";
OleDbCommand cmd1S = new OleDbCommand(sqlQuery1S, conn);
OleDbDataAdapter adapter1S = new OleDbDataAdapter(cmd1S);
adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");

If your ScannerID column is integer, then you should not use single quotes with it. 如果您的ScannerID列是整数,则不应在其上使用单引号。 Single quotes are for characters. 单引号用于字符。 Like; 喜欢;

WHERE ScannerID = " + select1S;

But as a better way, you should always use parameterized queries . 但是,作为一种更好的方法,您应该始终使用参数化查询 This kind of string concatenations are open for SQL Injection attacks. 这类字符串连接对SQL注入攻击开放。 Aka bobby-tables . 又名鲍比桌

And use using statement to dispose your connections, commands and adapters. 并使用using语句来处理您的连接,命令和适配器。

string jointS = dsetChamberS1.Tables[0].Rows[tot][0].ToString();
int select1S = Convert.ToInt32(jointS);
using(var conn = new OleDbConnection(conString))
using(var cmd1S = conn.CreateCommand())
{
    cmd1S.CommandText = "SELECT TMin,TMax,HMin,HMax from ScannerAlarmLimits WHERE ScannerID = @id";
    cmd1S.Parameters.AddWithValue("@id", OleDbType.Integer).Value = select1S;

    using(var adapter1S = new OleDbDataAdapter(cmd1S))
    {
        adapter1S.Fill(dsetTempS, "ScannerAlarmLimits");
    }
}

我只是在where子句的条件下添加了单引号,现在它可以工作了。

var query = "SELECT * from  checkinout where read <> '1'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM