asp.net中INSERT INTO语句中的语法错误

时间:2014-11-24 08:25:23

标签: asp.net ms-access

我在MS.Access中使用ASP.NET 在数据库中,有一个名为login的表,有三个字段,即id,username,password。 以下是我试图运行的代码。

String username = TextBox1.Text;
String password = TextBox2.Text;
OleDbConnection dbConnection = new OleDbConnection("provider=microsoft.ACE.OLEDB.12.0; Data source=C:\\Users\\Mohit Kumar Singla\\Documents\\phonebook.accdb");
dbConnection.Open();

Console.WriteLine("Error Occurred. Please check");
System.Diagnostics.Debug.WriteLine("Error--------------------------------");

String q = "insert into login (username, password) values (@u, @p)";               
OleDbCommand cmd = new OleDbCommand(q, dbConnection);

cmd.Parameters.AddWithValue("@u", username);
cmd.Parameters.AddWithValue("@p", password);

cmd.ExecuteNonQuery();

我在INSERT INTO语句中收到此错误"语法错误。"

2 个答案:

答案 0 :(得分:0)

试试这个

String q = "insert into login (username, [password]) values (@u, @p)"; 

原因:密码是保留字

Source

答案 1 :(得分:0)

试试这个

String username = TextBox1.Text;
String password = TextBox2.Text;
OleDbConnection dbConnection = new OleDbConnection("provider=microsoft.ACE.OLEDB.12.0; Data source=C:\\Users\\Mohit Kumar Singla\\Documents\\phonebook.accdb");
dbConnection.Open();

Console.WriteLine("Error Occurred. Please check");
System.Diagnostics.Debug.WriteLine("Error--------------------------------");

String q = "insert into login (username, [password]) values (@u, @p)";               
OleDbCommand cmd = new OleDbCommand(q, dbConnection);

cmd.Parameters.AddWithValue("@u", username);
cmd.Parameters.AddWithValue("@p", password);

cmd.ExecuteNonQuery();
dbConnection.Close();