我有一个类似于下面的数据库:
Table1(AutoNumber, Text, Number, Memo) // this is field types
Table1(ID, Title, Price, Image)
我尝试使用以下方法更新C#中数据库的现有元素:
const string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" + "data source=bd.mdb";
OleDbConnection m_dataBase = new OleDbConnection(connectionString);
OleDbConnection m_dataBase.Open();
SQL = "UPDATE Table1 SET Title='test', Price=35, Image='Test' WHERE ID=1";
OleDbCommand SQLQueryCommand = new OleDbCommand(SQL, m_dataBase);
int response = SQLQueryCommand.ExecuteNonQuery();
结果我收到了这个错误。 " UPDATE指令中的Microsoft JET数据库引擎错误语法"。
我做错了什么?
PS:我可以成功SELECT
或INSERT
,但不能UPDATE
。
答案 0 :(得分:0)
好吧,如果你的SQL命令是唯一的问题,那么就会出现一些明显的问题。 只需尝试使用下面的参数化您的Update子句,这将防止许多小错误和SQL注入。
SQL = "UPDATE Table1 SET Title=?, Price=?, Image=? WHERE ID=?";
SQLQueryCommand.Parameter.Add("@MyTitle", OleDbType.VarChar).Value = "Test";
SQLQueryCommand.Parameter.Add("@MyPrice", OleDbType.Integer).Value = 35;
SQLQueryCommand.Parameter.Add("@MyImage", OleDbType.VarChar).Value = "TestAgain";
SQLQueryCommand.Parameter.Add("@MyID", OleDbType.VarChar).Value = 1;
要了解有关参数化的更多信息,请尝试查看此MSDN文章底部的示例。
OleDbCommand.Parameters Property
此外,最好在using
语句中包围您的连接。
using (var m_dataBase = new OleDbConnection(connectionString) { ... }