简体   繁体   中英

OLEDB SQL Syntax Error, I can't see it

I am trying to send a new user on my C# program to the database (MSAccess in this case). I create the string as follows...

String StrCmd = String.Format("INSERT INTO tbl_Users (Username, Password, IsAdmin) VALUES ('{0}', '{1}', {2});", Username, passwordHash, AdminPower);

This creates an SQL string, for example...

INSERT INTO tbl_Users (Username, Password, IsAdmin) 
VALUES ('TestUser', '1013638657', False);

This works fine if I paste it into a Query in MSAccess, but in the code I keep getting a Syntax error in INSERT INTO statement. error.

Can anyone see something obvious I am missing?

Additional Information:

Username and passwordHash are Strings, AdminPower is a Boolean.

Username and Password are Text Fields, IsAdmin is a Yes/No Field.

Code Block of interest...

OleDbConnection MyConn = new OleDbConnection(Program.ConnStr);
MyConn.Open();
String StrCmd = String.Format("INSERT INTO tbl_Users (Username, Password, IsAdmin) VALUES ('{0}', '{1}', {2});", Username, passwordHash, AdminPower);
OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);
Cmd.ExecuteNonQuery();
MyConn.Close();

试试这个'{2}'

String StrCmd = String.Format("INSERT INTO tbl_Users (Username, Password, IsAdmin) VALUES ('{0}', '{1}', '{2}');", Username, passwordHash, AdminPower);

Facepalms I noticed the the list of reserved words I was checking was for SQL-Sever. I found one for MSAccess and "PASSWORD" is a reserved word in Access... I changed it to UserPassword, and it works...

grrr.

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