1

我在关注 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();
4

2 回答 2

1

你没有给我们太多信息,但是..

我认为您的Cont_no类型是某种数字类型,而不是字符类型之一。看起来这就是为什么当您尝试使用''.

例如喜欢;

Cont_no = '123'

试试这个;

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

编辑: HansUp 是完全正确的Read是一个reserved keyword。您应该在查询中使用方括号[Read]

于 2013-10-31T12:38:30.110 回答
0

在您的查询字符串中,您按优先级考虑参数,但是当您创建它们时,您正在给它们一个名称。

根据http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx,OleDbCommand 不支持命名参数。

看这个例子(来源: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();
     }
    }
于 2013-10-31T12:35:50.847 回答