C#和访问-条件表达式中的数据类型不匹配 [英] C# AND ACCESS - Data type mismatch in criteria expression

查看:83
本文介绍了C#和访问-条件表达式中的数据类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a code that updates/edits details of a/an computer/electronic product for a C# program connecting to the MS Access. Here are the codes:

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();

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

AvailableID accepts Int32 values and the rest of the variables are string. The program is executable, yet the C# detected the error.

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

Data type mismatch in criteria expression.

我该怎么办?

推荐答案

我怀疑您没有传递参数之一来纠正AvailableID,而是尝试通过以下方式添加参数:

I suspect that you're not passing one of your parameters correct probably the AvailableID, instead try to add the parameters this way:

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 ...

请注意,通过连接字符串来生成查询不是一个好主意,无论如何,您应该始终使用参数.

As a side note, it's not a good idea to generate queries by concatenating strings anyway you should always use parameters.

这篇关于C#和访问-条件表达式中的数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆