简体   繁体   中英

Update query when database in ms access

My update query

 update tbl_Clients set  Username=@Username, Password=@Password where Id=@Id

When I am updating this code in giving error Syntax error in update statement

The reason might because Passowrd is a reserved keyword in MS Access. You should use it with square brackets like [Password]

update tbl_Clients set Username = @Username, [Password] = @Password where Id = @Id

As a best practice, change it to non-reserved word.

By the way, if you using OleDb provider, it doesn't care about the named parameters. It only care about their orders. Since you didn't show your code, I hope you provided your parameters in a same order that you defined in your command. Like;

var cmd = OleDbCommand("update tbl_Clients set Username = @Username, [Password] = @Password where Id = @Id");
cmd.Parameters.Add("@Username", OleDbType.VarChar, 255).Value = Username;
cmd.Parameters.Add("@Password", OleDbType.VarChar, 255).Value = Password;
cmd.Parameters.Add("@Id", OleDbType.Integer).Value = Id;

For MS Access you use a question mark ("?") in order to reference parameters in a query, so:

update tbl_Clients set Username=?, Password=? where Id=?

Of course you must be careful to add the parameters to the command object in the same order as they appear in the query. See this blog post for some examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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