插入语句 SQL C# 中的语法错误

Syntax Error In Insert Statement SQL C#

我遇到了 df.ExecuteNonQuery(); 声称插入语句有 SQL 错误的问题。我不确定为什么程序的其他部分正在使用 Insert 语句,但学生只是拒绝工作。
数据库:http://puu.sh/hoTCv/c1ccb77551.png

OleDbCommand df = new OleDbCommand("INSERT into Students(ID,Password,FirstName,LastName,Street,City,State,Zip,EMail,GPA)" + "VALUES (?,?,?,?,?,?,?,?,?,?)", db);
//creating parameters
df.Parameters.AddWithValue("@ID", iDText.Text);
df.Parameters.AddWithValue("@Password", PassText.Text);
df.Parameters.AddWithValue("@FirstName", fnText.Text);
df.Parameters.AddWithValue("@LastName", LnText.Text);
df.Parameters.AddWithValue("@Street", StreetText.Text);
df.Parameters.AddWithValue("@City", CityText.Text);
df.Parameters.AddWithValue("@State", StateText.Text);
df.Parameters.AddWithValue("@Zip", ZipText.Text);
df.Parameters.AddWithValue("@EMail", EmailText.Text);
df.Parameters.AddWithValue("@GPA", GPAText.Text);
df.ExecuteNonQuery();
db.Close();

可以这样吗:

INSERT into Students(ID,Password ....

为此:

INSERT into Students(ID,[Password] ....

Password 是 Microsoft OLE DB 提供程序中的 reserved keyword。您需要使用 [Password] 这样的方括号。作为最佳做法,将您的列名称更改为 非保留 字。

也不要使用AddWithValue方法。 It may generate unexpected results. Use .Add() overloads to specify your OleDbType 和你的参数大小。

最好使用 using statement 自动处理 OleDbConnectionOleDbCommand 而不是手动调用 .Close().Dispose() 方法。

Password 是保留关键字。把你的密码包装成 [Password]

Microsoft SQL Server uses reserved keywords for defining, manipulating, and accessing databases. Reserved keywords are part of the grammar of the Transact-SQL language that is used by SQL Server to parse and understand Transact-SQL statements and batches. Although it is syntactically possible to use SQL Server reserved keywords as identifiers and object names in Transact-SQL scripts, you can do this only by using delimited identifiers.