4

我在使用 MS-Accessmdb数据库的 C# 表单应用程序中工作。我有一个数据库,其中有一个Customers包含两列CustomerIdAnd的表Balance。两列都是integer数据类型。

我得到的错误是

System.Data.OleDb.OleDbException:UPDATE 语句中的语法错误。

在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
在 System.Data。
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,字符串方法)
在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()在xml_and_db_test.Form1.button1_Click
(Object sender , EventArgs e) 在 G:\my Documents\Visual Studio 2008\Projects\xml_and_db_test\xml_and_db_test\Form1.cs:line 45

到目前为止我尝试过的代码是

try
{
   OleDbConnection con = new 
   OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\database_for_kissan_Pashu_AhaR_Bills.mdb");

   int balance = Convert.ToInt32(textBox2.Text);
   int id = Convert.ToInt32(textBox1.Text);

   // int recordnumb = int.Parse(recordTextBox.Text);

   //  OleDbConnection oleDbConnection = new            OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Checkout-1\\Documents\\contact.accdb");    
   OleDbCommand update = new OleDbCommand("UPDATE Customers  SET Balance = '" + balance + "',  WHERE id = " + id + " ", con);

   con.Open();
   update.ExecuteNonQuery();
   con.Close();

   // string queryText = "UPDATE Customers SET Balance = ?, where CustomerId = ?;";
   //string queryText = " 'UPDATE Customers SET Balance =' " + balance+ " ' WHERE CustomerId=  ' " +  id +  " ' " ;

   //OleDbCommand cmd = new OleDbCommand(queryText, con);
   //cmd.CommandType = CommandType.Text;
   //cmd.Parameters.AddWithValue("@balance", Convert.ToInt32(textBox2.Text));
   //cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(textBox1.Text));
   //cmd.Parameters.Add("Balance", OleDbType.Integer).Value = Convert.ToInt32(textBox2.Text);
   //cmd.Parameters.Add("CustomerId", OleDbType.Integer).Value = Convert.ToInt32(textBox1.Text);

   //con.Open(); // open the connection
   ////OleDbDataReader dr = cmd.ExecuteNonQuery();

   //int yy = cmd.ExecuteNonQuery();
   //con.Close();
}
catch (Exception ex)
{
    string c = ex.ToString();
    MessageBox.Show(c);
}

//try
//{
//    OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\\my Documents\\Visual Studio 2008\\Projects\\xml_and_db_test\\xml_and_db_test\\bin\\Debug\\database_for_kissan_Pashu_AhaR_Bills.mdb");
//    string queryText = "UPDATE Customers SET Balance = ?, where CustomerId = ?;";

//    OleDbCommand cmd = new OleDbCommand(queryText, con);
//    cmd.CommandType = CommandType.Text;
//    //cmd.Parameters.AddWithValue("@balance", Convert.ToInt32(textBox2.Text));
//    //cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(textBox1.Text));

//    cmd.Parameters.Add("Balance", OleDbType.Integer).Value = Convert.ToInt32(textBox2.Text);

//    cmd.Parameters.Add("CustomerId", OleDbType.Integer).Value = Convert.ToInt32(textBox1.Text);
//    con.Open(); // open the connection
//    //OleDbDataReader dr = cmd.ExecuteNonQuery();

//    int yy = cmd.ExecuteNonQuery();
//    con.Close();

//}
//catch (Exception ex)
//{
//    string c = ex.ToString();
//    MessageBox.Show(c);

//}
//string connetionString = null;
//OleDbConnection connection;
//OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();
//string sql = null;
//connetionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\\my Documents\\Visual Studio 2008\\Projects\\xml_and_db_test\\xml_and_db_test\\bin\\Debug\\database_for_kissan_Pashu_AhaR_Bills.mdb;";
//connection = new OleDbConnection(connetionString);
//sql = "update Customers set Balance = '1807' where CustomerId = '1'";
//try
//{
//    connection.Open();
//    oledbAdapter.UpdateCommand = connection.CreateCommand();
//    oledbAdapter.UpdateCommand.CommandText = sql;
//    oledbAdapter.UpdateCommand.ExecuteNonQuery();
//    MessageBox.Show("Row(s) Updated !! ");
//    connection.Close();
//}
//catch (Exception ex)
//{
//    MessageBox.Show(ex.ToString());
//}

有些代码在评论中,有些代码在每种方法中我都会遇到同样的错误。

4

5 回答 5

5

正如gzaxx所说..尝试改变这个

string queryText = "UPDATE Customers SET Balance = ?, where CustomerId = ?;";

string queryText = "UPDATE Customers SET Balance = ? where CustomerId = ?;";
于 2013-07-02T16:40:41.550 回答
4

您的查询中余额后有一个逗号。此外,您将余额转换int32为字符串,因为它之间存在单引号,因此您将其作为字符串插入。

"UPDATE Customers  SET Balance = " + balance + "  WHERE id = " + id

这个查询应该有效。

于 2013-07-02T16:33:09.387 回答
3

去掉 set 子句后的逗号——你只有一个变量被更新。set xxx = xxx , where 应该设置 xxx = xxx where。

OleDbCommand update = new OleDbCommand("UPDATE Customers  SET Balance = '" + balance + "'  WHERE id = " + id + " ", con);
于 2013-07-02T16:31:59.527 回答
1

你只要把它改成

OleDbCommand update = new OleDbCommand("UPDATE Customers SET [Balance] = '" + balance + "', WHERE [id] = " + id + " ", con);

并且您的代码将正常工作

于 2014-03-05T13:53:20.703 回答
1
  OleDbCommand o_cmd = new OleDbCommand("Update tbl_SalesTax 
                       set tbl_SalesTax.SerialNumber='" + txtSerialNo.Text + "'
                      ,tbl_SalesTax.PartyCode='" + txtPartyCode.Text + "'
                      ,tbl_SalesTax.PartyName='" + txtPartyName.Text + "'
                      ,tbl_SalesTax.TinNumber='" + txtTinNo.Text + "'
                      ,tbl_SalesTax.Gr='" + cmbgr.Text + "'
                      ,tbl_SalesTax.Qty='" + txtQty.Text + "'
                      ,tbl_SalesTax.Price='" + txtPrice.Text + "'
                      ,tbl_SalesTax.Basic='" + txtBaisc.Text + "'
                      ,tbl_SalesTax.Value='" + txtValue.Text + "'
                      ,tbl_SalesTax.Total='" + txtTotal.Text + "'
                      ,tbl_SalesTax.Bags='" + txtBags.Text + "'
                      ,tbl_SalesTax.DumpCode='" + txtDumpCode.Text + "'
         where tbl_SalesTax.BookNumber='" + txtBookNo.Text + "'", my_con);
于 2016-01-29T06:42:01.970 回答