【问题标题】:SQL Insert statement not working C#SQL插入语句不起作用C#
【发布时间】:2015-10-20 09:13:41
【问题描述】:

像这样使用 Access 数据库:

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Request.PhysicalApplicationPath + "Resources/cars_db.accdb");
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Users (Username, Password, Email, Address, Question, Answer) VALUES ('" + txtUsernameRP.Text + "','" + txtPasswordRP.Text + "','" + txtEmailRP.Text + "','" + txtAddressRP.Text + "','" + txtQuestionRP.Text + "','" + txtAnswerRP.Text + "')";
int i = cmd.ExecuteNonQuery(); -- **Breaks here and says syntax error**

我试过了:

  1. 取出 int i 位。
  2. 将@符号放在语句前面。
  3. 检查并确保我使用的是访问代码。
  4. 在创建之前尝试关闭连接,以防它在那里。
  5. 将数据替换为固定值,然后我在 Access 中的一个查询中运行该值。
  6. 将上面提到的访问查询放入代码块(cmd.CommandText = "INSERT INTO Users(Username, Password, Email, Address, Question, Answer) VALUES ('asdasd','Asd! 23asd','asdasd','asdasd','asdasd','asdasd')"; 并尝试运行它并再次出现相同的语法错误。

请有人帮助我...

【问题讨论】:

  • 您是否有任何特定的语法错误(近线...)?还是只是一般的语法错误?
  • 你在 nr 6 中错过了一次paranteces。我不知道这是否重要,但是......
  • 根据您的查询生成的“(”在用户之后丢失。
  • 那么,如果你使用Access,为什么要添加MySQL标签?
  • 您是否在代码之外尝试过此请求?

标签: c# ms-access


【解决方案1】:

密码是保留关键字

  cmd.CommandText = "INSERT INTO Users (Username, Password, Email, Address...

试试这个

cmd.CommandText = "INSERT INTO Users (Username, [Password], Email, Address...

【讨论】:

    【解决方案2】:

    你可以试试这个吗?

     "INSERT INTO Users (Username, Password, Email, Address, Question, Answer) 
        VALUES(
        '" + txtUsernameRP.Text + "',
        '" + txtPasswordRP.Text + "',
        '" + txtEmailRP.Text + "',
        '" + txtAddressRP.Text + "',
        '" + txtQuestionRP.Text + "',
        '" + txtAnswerRP.Text + "');";
    

    【讨论】:

    • 我已经尝试删除“(用户名、密码、电子邮件、地址、问题、答案)”,它对我来说很好..
    【解决方案3】:

    我猜你的问题出在 int i 上,请尝试删除它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2012-05-05
      • 1970-01-01
      相关资源
      最近更新 更多