简体   繁体   English

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

[英]C# AND ACCESS - Data type mismatch in criteria expression

I've created a code that updates/edits details of a/an computer/electronic product for a C# program connecting to the MS Access. 我已经创建了一个代码,用于更新/编辑连接到MS Access的C#程序的计算机/电子产品的详细信息。 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 accepts Int32 values and the rest of the variables are string. AvailableID接受Int32值,其余变量是字符串。 The program is executable, yet the C# detected the error. 该程序是可执行的,但C#检测到错误。

Data type mismatch in criteria expression. 条件表达式中的数据类型不匹配。

What should I do? 我该怎么办?

I suspect that you're not passing one of your parameters correct probably the AvailableID , instead try to add the parameters this way: 我怀疑你没有传递你的一个参数正确的可能是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 ...

As a side note, it's not a good idea to generate queries by concatenating strings anyway you should always use parameters. 作为旁注,通过连接字符串生成查询并不是一个好主意,无论如何你应该总是使用参数。

remove('')对于那些有整数值

Try this 尝试这个

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

simple is to put a debug point on this and check the query. 简单的是在此上放置一个调试点并检查查询。 Copy it and run it in access directly and make changes with data. 复制并直接在访问中运行它并使用数据进行更改。 You will find out what parameter value you are entering is wrong. 您将找出输入的参数值是错误的。

I would use something like this to achieve what you are trying to do. 我会用这样的东西来实现你想要做的事情。 This code works fine for me with MS Access 2013 使用MS Access 2013,此代码适用于我

//setting up connection.
OleDbConnection conn = new OledbConnection("connectionstring goes here");
//set the command string query
string cmdStr = "UPDATE Available SET ProductType = ?, Brand = ?, Model = ?, SerialNo = ?, Remarks = ?, RAM = ?, ODD = ?, VideoCard = ?, PS = ?  WHERE AvailableID = ?";
//create the command 
OleDbCommand cmd = new OleDbCommand(cmdStr,conn);
//add parameters
cmd.Parameters.AddWithValue("@p1",newAvailable.Brand);
cmd.Parameters.AddWithValue("@p2",newAvailable.Brand);
cmd.Parameters.AddWithValue("@p3",newAvailable.SerialNo);
// add all your parameters in the correct order here below .

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

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