[英]Error in update query in c# for access database
I am facing error in following Query.According to my knowledge I have written everything perfectly fine. 我在执行Query时遇到错误。据我所知,我编写的一切都很好。 But its giving error that:
但是它给人的错误是:
"there is an error in update query"
“更新查询中存在错误”
string insert_query = "update aho set read=?,pick=? where Cont_no='" + contract_no + "'";
OleDbCommand ocmd = new OleDbCommand();
ocmd.CommandText = insert_query;
//ocmd.Parameters.AddWithValue("@contrct_no", contract.Text.ToString());
ocmd.Parameters.AddWithValue("@read_val", Convert.ToInt32(read.Text));
ocmd.Parameters.AddWithValue("@pic_val", Convert.ToInt32(pick.Text));
ocmd.Connection = conn;
ocmd.ExecuteNonQuery();
You didn't gave us too much information but.. 您没有给我们太多信息,但是..
I think your Cont_no
type is some numerical type, not one of the character type. 我认为您的
Cont_no
类型是某种数字类型,而不是字符类型之一。 Looks like that's why you get error when you try to add it with ''
. 看起来这就是为什么在尝试使用
''
进行添加时出现错误的原因。
For example like; 例如:
Cont_no = '123'
Try this one; 试试这个;
string insert_query = "update aho set [read]=?,pick=? where Cont_no=?";
OleDbCommand ocmd = new OleDbCommand();
ocmd.CommandText = insert_query;
ocmd.Parameters.AddWithValue("@read_val", Convert.ToInt32(read.Text));
ocmd.Parameters.AddWithValue("@pic_val", Convert.ToInt32(pick.Text));
ocmd.Parameters.AddWithValue("@contrct_no", contract_no);
ocmd.Connection = conn;
ocmd.ExecuteNonQuery();
EDIT : HansUp is totally right . 编辑 :HansUp是完全正确的 。
Read
is a reserved keyword
. Read
是reserved keyword
。 You should use it with square brackets like [Read]
in your query. 您应该在查询中使用方括号,例如
[Read]
。
In your query string you consider parameters by priority, but when you create them you are giving them a name. 在查询字符串中,您会按优先级考虑参数,但是在创建参数时会为其指定名称。
According to http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx , OleDbCommand does not support named parameters. 根据http://msdn.microsoft.com/zh-cn/library/system.data.oledb.oledbcommand.parameters.aspx,OleDbCommand不支持命名参数。
Look at this example (source: http://www.java2s.com/Code/CSharp/Database-ADO.net/PassparametertoOleDbCommand.htm ): 查看以下示例(源: http : //www.java2s.com/Code/CSharp/Database-ADO.net/PassparametertoOleDbCommand.htm ):
using System;
using System.Data;
using System.Data.OleDb;
public class Prepare {
public static void Main () {
String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
OleDbConnection con = new OleDbConnection(connect);
con.Open();
Console.WriteLine("Made the connection to the database");
OleDbCommand cmd1 = con.CreateCommand();
cmd1.CommandText = "SELECT ID FROM Employee "
+ "WHERE id BETWEEN ? AND ?";
OleDbParameter p1 = new OleDbParameter();
OleDbParameter p2 = new OleDbParameter();
cmd1.Parameters.Add(p1);
cmd1.Parameters.Add(p2);
p1.Value = "01";
p2.Value = "03";
OleDbDataReader reader = cmd1.ExecuteReader();
while(reader.Read())
Console.WriteLine("{0}", reader.GetInt32(0));
reader.Close();
con.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.