使用 C# 和 ms-access 的 INSERT INTO 语句中的语法错误
Syntax error in INSERT INTO statement using C# with ms-access
我无法弄清楚我的代码有什么问题。我试图通过 C# 在我的 Access Db 中添加一条记录,但遇到了一个异常,该异常表明我的 SQL 语句中有错误的语法。
private void buttonSUB_Click(object sender, EventArgs e)
{
string Adrs = textADRS.Text;
string Fname = textFN.Text;
string Mname = textMN.Text;
string Lname = textLN.Text;
string Pos = textPOS.Text;
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\genesis\Documents\Database1.accdb";
OleDbCommand cmd = new OleDbCommand("Insert into Employees (FN,MN,LN,Address,Position) Values (@FirstName,@MidName,@LastName,@Address,@Position)",con);
cmd.Parameters.Add(new OleDbParameter("@FirstName", Fname));
cmd.Parameters.Add(new OleDbParameter("@MidName", Mname));
cmd.Parameters.Add(new OleDbParameter("@LastName", Lname));
cmd.Parameters.Add(new OleDbParameter("@Address", Adrs));
cmd.Parameters.Add(new OleDbParameter("@Position", Pos));
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Submitted", "Nice!");
con.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
试试这样的:
cmd.Parameters.Add(new OleDbParameter("@FirstName","'" + Fname "'"));
带有字符串参数的单个代码。
从 Access SQL Reserved Words List 开始,POSITION
已定义为保留关键字。
If you use a reserved word or symbol to name a field in a table,
Access warns you that the word is reserved and that you might
encounter errors when referring to the field. You might also encounter errors if you use a reserved word to name a control, an object, or a variable.
If a reserved word is already in use, you can avoid error messages by
surrounding each occurrence of the word with brackets ([ ]).
因此,您需要添加方括号才能将其用作 table 名称:
OleDbCommand cmd = new OleDbCommand(
@"INSERT INTO Employees (FN, MN, LN, Address, [Position])
VALUES (@FirstName, @MidName, @LastName, @Address, @Position)", con);
注意:保留字在 Access 用法中不区分大小写,因此每次使用保留字的地方都必须放在方括号内。
我无法弄清楚我的代码有什么问题。我试图通过 C# 在我的 Access Db 中添加一条记录,但遇到了一个异常,该异常表明我的 SQL 语句中有错误的语法。
private void buttonSUB_Click(object sender, EventArgs e)
{
string Adrs = textADRS.Text;
string Fname = textFN.Text;
string Mname = textMN.Text;
string Lname = textLN.Text;
string Pos = textPOS.Text;
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\genesis\Documents\Database1.accdb";
OleDbCommand cmd = new OleDbCommand("Insert into Employees (FN,MN,LN,Address,Position) Values (@FirstName,@MidName,@LastName,@Address,@Position)",con);
cmd.Parameters.Add(new OleDbParameter("@FirstName", Fname));
cmd.Parameters.Add(new OleDbParameter("@MidName", Mname));
cmd.Parameters.Add(new OleDbParameter("@LastName", Lname));
cmd.Parameters.Add(new OleDbParameter("@Address", Adrs));
cmd.Parameters.Add(new OleDbParameter("@Position", Pos));
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Submitted", "Nice!");
con.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
试试这样的:
cmd.Parameters.Add(new OleDbParameter("@FirstName","'" + Fname "'"));
带有字符串参数的单个代码。
从 Access SQL Reserved Words List 开始,POSITION
已定义为保留关键字。
If you use a reserved word or symbol to name a field in a table, Access warns you that the word is reserved and that you might encounter errors when referring to the field. You might also encounter errors if you use a reserved word to name a control, an object, or a variable.
If a reserved word is already in use, you can avoid error messages by surrounding each occurrence of the word with brackets ([ ]).
因此,您需要添加方括号才能将其用作 table 名称:
OleDbCommand cmd = new OleDbCommand(
@"INSERT INTO Employees (FN, MN, LN, Address, [Position])
VALUES (@FirstName, @MidName, @LastName, @Address, @Position)", con);
注意:保留字在 Access 用法中不区分大小写,因此每次使用保留字的地方都必须放在方括号内。