I'm trying to read a MS Access database using the OleDbConnection class in C#.

So far I'm able to connect to a DB and read a specific table and every row of it. I can get without any problem the fields which are based on a string. But some of columns are foreign keys (numeric) and I get the numeric value instead of the string one (as it can be seen in MS Access).

Here is a shorted example of how I proceed:

public void ReadDB(OleDbConnection connection) 
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter("Select * From T_MYTABLE", connection);
    DataSet localData = new DataSet();

    foreach (DataRow dataRow in localData.Tables[0].Rows)
        string name = dataRow["APP_NAME"].ToString(); 
        string status = dataRow["APP_STATUS"].ToString();


Actually the variable name contains the expected value but status is the numeric value of the enum representing the status.

If we look the data with MS Access we have for example :

Example MS Access table

And the Status is defined as follows : Enum definition

So actually for this row I get :

  • name = "MyApplication" which is correct
  • status = 83 which should be "growth"

Is there a way to get the string value instead? Should I use another way to read the table (instead of using DataSet) ?

2 Answers

Mike Yinger On

You can convert a string value into an enum using Enum.Parse

MyEnum status= (MyEnum) Enum.Parse(typeof(MyEnum), dataRow["APP_STATUS"].ToString());

See here for more details.

Gauravsa On

In .NET Core and .NET > 4 there is a generic parse method:

  Enum.TryParse(dataRow["APP_STATUS"].ToString(), out MyEnum status);

This will also guard against dbnull situation.

Also, its a good idea to have a default enum value if APP_STATUS doesnt map to enum.

 if (string.IsNullOrWhiteSpace(dataRow["APP_STATUS"].ToString()))
    return MyEnum.None;
 Enum.TryParse(dataRow["APP_STATUS"].ToString(), out MyEnum status);