在c#form application中更新访问mdb数据库中的数据“update statement中的语法错误”

时间:2013-07-02 16:30:28

标签: c# database winforms

我在C#表单应用程序中使用MS-Access mdb数据库。我有一个数据库,其中有一个表Customers,其中包含两列CustomerIdBalance。这两列都是integer数据类型。

我得到的错误是

  

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

     

at 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.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Object& executeResult)
  在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)
  在System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
  位于G:\ my Documents \ Visual Studio 2008 \ Projects \ xml_and_db_test \ xml_and_db_test \ Form1.cs中的xml_and_db_test.Form1.button1_Click(Object sender,EventArgs e):第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());
//}

有些代码在注释中,有些代码在每种方法中都会出现相同的错误。

5 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:4)

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

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

此查询应该有效。

答案 2 :(得分:3)

在set子句之后删除逗号 - 您只更新了一个变量。设置xxx = xxx,其中应设置xxx = xxx where。

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

答案 3 :(得分:1)

你只需将其改为

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

您的代码将正常运行

答案 4 :(得分: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);