Search code examples
c#sqloledboledbcommand

C# - UPDATE SET WHERE Query issues (OleDb)


I have a Access 2000 (*.mdb) file which is edited by OleDb Queries.

At the moment I would like to UPDATE a table into a database which has only two columns as you can see in the picture below

enter image description here

Every column of this database is of type String

This code, thows an OleDbException:

OleDbConnection con;
OleDbCommand cmd;

private void UpdateExistingRow()
{
    string strProvider = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + ";"
    string strSql = "UPDATE OPTIONS SET VAL='C:\\Edo\\Desktop\\Logo.png' WHERE IMP='LogoPath'";
    con = new OleDbConnection(strProvider);
    cmd = new OleDbCommand(strSql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

It seems simple, I know, but I'm not able to find a solution.
Thank You.


Solution

  • The answer was not as obvious as we initially thought, but I think I found it: IMP is a reserved keyword in Access SQL (or more likely: an operator). Strangely though it doesn't show up in the JET documentation.

    So you need to make sure the DB Engine knows it's a column name by escaping your colum names with []:

    UPDATE [OPTIONS] SET VAL='C:\\Edo\\Desktop\\Logo.png' WHERE [IMP] = 'LogoPath'