FROM子句语法错误

本文关键字:错误 子句语法 FROM | 更新日期: 2023-09-27 18:05:19

这是我认为有效的代码,但它仍然在我的FROM子句中给我带来语法错误。有人能帮我理解我遗漏了什么吗?如果没有try'catch,它将高亮显示线int result = (int)cmd.ExecuteScalar();

string constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'jwhite'Documents'TrainingDB.accdb";
string cmdText = "SELECT COUNT(*) FROM USER WHERE Username=@p1 AND [Password]=@p2";
using (OleDbConnection con = new OleDbConnection(constring))
using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
{
   try
   {
      con.Open();
      cmd.Parameters.AddWithValue("@p1", textBox1.Text);
      cmd.Parameters.AddWithValue("@p2", textBox2.Text);
      int result = (int)cmd.ExecuteScalar();
      if (result > 0)
      {
         groupBox1.Visible = false;
         groupBox2.Visible = true;
         string commandText = "SELECT RIGHTS FROM USER WHERE Username=@p1 and [Password]=@p2";
         using (OleDbCommand command = new OleDbCommand(commandText, con))
         {
            command.Parameters.AddWithValue("@p1", textBox1.Text);
            command.Parameters.AddWithValue("@p2", textBox2.Text);
            string query = (string)command.ExecuteScalar();
            {
               if (query == "Administrator")
               {
                  toolStripMenuItem59.Enabled = true;
                  administratorToolStripMenuItem1.Enabled = true;
                  administratorToolStripMenuItem3.Enabled = true;
                  administratorToolStripMenuItem4.Enabled = true;
                  administratorToolStripMenuItem5.Enabled = true;
                  administratorToolStripMenuItem2.Enabled = true;
                  administratorToolStripMenuItem6.Enabled = true;
                  toolStripMenuItem92.Enabled = true;
                  toolStripMenuItem108.Enabled = true;
               }
            }
         }
      }
      else
         MessageBox.Show("Invalid Credentials, Please Try Again");
   }
   catch (Exception ex)
   {
      MessageBox.Show("Failed due to " + ex.Message);
   }
}

FROM子句语法错误

不写USER,而写[USER],因为USER是一个保留字。

请参阅此处的保留字列表

根据http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspxOleDbCommand不支持命名参数

OLE DB。NET提供程序不支持用于传递的命名参数SQL语句或由调用的存储过程的参数当CommandType设置为Text时使用OleDbCommand。在这种情况下必须使用问号(?(占位符。例如:

SELECT * FROM Customers WHERE CustomerID = ?

因此,OleDbParameter对象添加到OleDbParametersCollection的顺序必须直接对应于该参数的问号占位符在命令文本中的位置。

所以参数的顺序很重要。