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

Data Type Mismatch error in Criteria expression in Select query C# query

我的示例代码如下,出现如下错误;

Data Type Mismatch error in criteria expression.

详细信息 => ScannerAlarmLimits 是我的 table 来自 .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");

如果您的 ScannerID 列是整数,那么您不应该对它使用单引号。单引号用于字符。喜欢;

WHERE ScannerID = " + select1S;

但作为更好的方法,您应该始终使用 parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Aka bobby-tables

并使用 using statement 处理您的连接、命令和适配器。

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'";