简体   繁体   English

Access 2010数据库表中的更新字段

[英]Update fields in Access 2010 Database Table

I have a database with four tables in it. 我有一个包含四个表的数据库。 I am trying to update the Techs table. 我正在尝试更新Techs表。 To do this I need to read the existing values, store them into memory, and add a values to the data. 为此,我需要读取现有值,将它们存储到内存中,然后向数据中添加一个值。 My problem occurs when i try to update the values back to the database I get a error saying there is a syntax error in my update statement. 当我尝试将值更新回数据库时,出现了我的错误,提示我的更新语句中存在语法错误。

string userName = "Admin";
int closed = 0;
decimal money = 0.00M;

string sql = "Select * From Techs WHERE TechName = @uName";
cmd.CommandText = sql;
cmd.Parameters.Add("@uName", OleDbType.VarChar, 50); cmd.Parameters["@uName"].Value = userName;

cmd.Connection = conn;
dbReader = MakeReader(sql);
while (dbReader.Read())
{
        money = decimal.Parse(dbReader["Money"].ToString());
        closed = int.Parse(dbReader["#Closed"].ToString());
}
dbReader.Close();

//Proof of sucessful read
MessageBox.Show(money + ":" + closed);

string update = "Update Techs SET Money=@money, #Closed=@closed WHERE TechName=@uName";
OleDbCommand updateCmd = new OleDbCommand(update, conn);
updateCmd.Parameters.AddWithValue("@money", money + 12.00M);
updateCmd.Parameters.AddWithValue("@closed", closed + 1);
updateCmd.Parameters.AddWithValue("@uName", userName);
updateCmd.ExecuteNonQuery(); //Error here

Error message 错误信息

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll System.Data.dll中发生了类型为'System.Data.OleDb.OleDbException'的未处理异常

Additional information: Syntax error in UPDATE statement. 附加信息:UPDATE语句中的语法错误。

Money is a keyword, and #Closed contains a special character, both should be bracketed: Money是一个关键字,#Closed包含一个特殊字符,两者都应放在方括号中:

string update = "Update Techs SET [Money]=@money, [#Closed]=@closed WHERE TechName=@uName";

Personally, I'd avoid Money as a parameter name too, but unsure if that would cause trouble. 就个人而言,我也避免将Money作为参数名称,但是不确定是否会引起麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM