我尝试使用更新命令更新我的库存但发生数据类型未命中匹配条件错误 [英] i am try to update my stock using update command but data type miss match criteria error occured
问题描述
try
{
con = new OleDbConnection(s);
con.Open();
sq = "Update BillTable Set Quantity ="+ textBox5.Text + " where Id = " + textBox2.Text;
cmd = new OleDbCommand(sq,con);
cmd.ExecuteNonQuery();
MessageBox.Show("row updated...");
con.Close();
}//end f try
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
推荐答案
不要在查询字符串中使用字符串连接,因为它非常不安全。
您的问题是缺少符号和
;
用于关闭语句的符号以及查询字符串末尾的查询。
Do not use string concatenation in query strings, as its highly unsafe.
Your issue in your case is the missing"
symbol and the;
symbol for closing the statement as well as the query at then end of your query string.
string sq = ("Update BillTable Set Quantity = " + textBox5.Text + " where Id = " + textBox2.Text + ";");
在MySQL,SQL中,你可以使用Named查询中的参数如@Yourname。但是在OleDB中,您需要按查询顺序指定参数并使用?而不是@。请参阅下文:
In MySQL, SQL, you can use Named parameters like @Yourname in your query. But in OleDB you need to specify your parameters in the order of your query and use ? instead of @. See below:
//Opp being your table. ? is your target value parameters which acts as a placeholder. Conn is your connection.
String dStr = "Two";
String eStr = "Three";
OleDbCommand cmdcom = new OleDbCommand("Insert Into Opp [Year] VALUES (?eNum);", Conn);
cmdcom.Parameters.AddWithValue("?eNum", OledbType.VarChar).Value = dStr);
OleDb查询中的参数仅按位置分配,名称完全被忽略。如果以有序的方式添加,您可以使用命名查询。含义;如果您的查询有多个占位符?
,它们将按照它们写入的顺序调用。示例:
Parameters in OleDb queries are only assigned by position and the names are disregarded entirely. You can use named queries if added in an orderly way. Meaning; if your query has multiple placeholders ?
, they will be called in the order they are wrote. Example:
cmdcom.Parameters.AddWithValue("?dNum", OledbType.VarChar).Value = dStr);
cmdcom.Parameters.AddWithValue("?eNum", OledbType.VarChar).Value = eStr);
//dNum, then eNum etc...
此外,有些词是 OleDB中的保留字,需要包含在<$ c中$ c> [] 括号。如果Year是一个保留字(它是),你也可以用方括号括起来。
Also, some words are Reserved Words In OleDB and are required to be wrapped in []
brackets. If Year were a reserved word (which it is), you would also wrap that in square brackets.
这篇关于我尝试使用更新命令更新我的库存但发生数据类型未命中匹配条件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!