C#AND ACCESS - 条件表达式中的数据类型不匹配

Jam*_*sus 2 c# oledb ms-access

我已经创建了一个代码,用于更新/编辑连接到MS Access的C#程序的计算机/电子产品的详细信息.以下是代码:

OleDbCommand cmd = new OleDbCommand("UPDATE Available SET ProductType = '" + newAvailable.ProductType + "', Brand = '"+ newAvailable.Brand + "', Model = '" + newAvailable.Model + "', SerialNo = '" + newAvailable.SerialNo + "', Remarks = '" + newAvailable.Remarks + "', RAM = '" + newAvailable.RAM + "', HDD = '" + newAvailable.HDD + "', ODD = '" + newAvailable.ODD + "', VideoCard = '" + newAvailable.VideoCard + "', PS = '" + newAvailable.PS + "'  WHERE AvailableID = '"+oldAvailable.AvailableID+"'", cnn);
cmd.CommandType = CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
Run Code Online (Sandbox Code Playgroud)

AvailableID接受Int32值,其余变量是字符串.该程序是可执行的,但C#检测到错误.

条件表达式中的数据类型不匹配.

我该怎么办?

Dim*_*rov 10

我怀疑你没有正确传递你的一个参数AvailableID,而是尝试以这种方式添加参数:

var cmd = new OleDbCommand
{
    Connection = cnn,
    CommandType = CommandType.Text,
    CommandText = "UPDATE Available SET ProductType = ?, Brand = ?, Model = ?, SerialNo = ?, Remarks = ?, RAM = ?, ODD = ?, VideoCard = ?, PS = ?  WHERE AvailableID = ?"
};

cmd.Parameters.Add(new OleDbParameter {Value = newAvailable.ProductType});
cmd.Parameters.Add(new OleDbParameter {Value = newAvailable.Brand});
// add the other parameters ...
Run Code Online (Sandbox Code Playgroud)

作为旁注,通过连接字符串生成查询并不是一个好主意,无论如何你应该总是使用参数.