我在使用 MS-Accessmdb
数据库的 C# 表单应用程序中工作。我有一个数据库,其中有一个Customers
包含两列CustomerId
And的表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());
//}
有些代码在评论中,有些代码在每种方法中我都会遇到同样的错误。