Search code examples
c#sqlms-accessoledb

C# - OleDB Systax Error in From Clause


I am using Microsoft Access to create a "Desktop Database" and saved it as "new.mdb" into my C# Debug folder.

However, upon using the SELECT statement, my C# project throws an Exception.

This is my database enter image description here

And this is my code

   conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=new.mdb");
            conn.Open();
            dataset = new DataSet();
            string sqlStatement = "SELECT * FROM User";
            dbAdapter = new OleDbDataAdapter(sqlStatement, conn);
            oOrderDetailsCmdBuilder = new OleDbCommandBuilder(dbAdapter);
            dbAdapter.Fill(dataset);
            contactsTab = dataset.Tables[0];
            contactsTab.TableName = "User";
            rows = contactsTab.Rows;

The error upon executing that code is

Syntax error in FROM clause.

enter image description here

However, the query looks fine. Is there anything wrong?

Thanks!

EDIT :

OleDbCommand.ExecuteNonQuery (Example : Creating new table) works for this. I'm not sure why SELECT statement doesn't :/


Solution

  • User is a reserved keyword in SQL. So write SQL like below

     string sqlStatement = "SELECT * FROM [User]";