Reputation: 175
I have som problems with reading from accdb database when it has fieldnames with characters like "-", "%", "/" or if the fieldname is "Level". When I have something like this:
string mySelectQuery = "SELECT 'Part_Time_%','Level',Level_change_reason FROM Employees";
OleDbConnection myConnection = new OleDbConnection(oledbConnectString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
it works well because '' characters cancel special characters and word meening. This is ok:
if ((myReader["Name"] == DBNull.Value) ....
if ((myReader["Surname"] == DBNull.Value) ....
But when I try this:
if ((myReader["Macro-activity"] == DBNull.Value) ...
if ((myReader["Level"] == DBNull.Value)....
it jumps to catch statement.
I also tried myReader["\u004cevel"], myReader["'Macro-activity'"]
- it totally ignores escape characters.
Every answer would be very appreciated.
P.S.: I can not change the name of attributes.
Upvotes: 0
Views: 304
Reputation: 175
I found this and it does what I want:
if ((myReader.GetValue(18) == DBNull.Value) || string.Compare(myReader.GetValue(18).ToString(), "0") == 0)....
Upvotes: 0
Reputation: 31394
Escaping of columns names in Access uses braces, [ & ]
, not single quotes.
string mySelectQuery = "SELECT '[Part_Time_%],[Level],Level_change_reason FROM Employees";
Single quotes are for a string, so having 'Level' in the select statement will return the text 'Level' as an unnamed column in your result set. If you try myReader.GetItem(1)
it will return "Level"
for every row.
Upvotes: 1