简体   繁体   中英

Error in Syntax of FROM clause

Here is my code that I thought worked but it continues to give me a syntax error in my FROM clause. Could someone help me understand what I am missing? Without the try\\catch , it highlights the line 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);
   }
}

Instead of USER write [USER] as USER is a reserved word.

See List of Reserved Words HERE

According to http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx OleDbCommand does not support named parameter

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

So order of parameter is important.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM