简体   繁体   中英

reading attributes with special names from MS ACCESS in OleDbDataReader

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["\Level"], myReader["'Macro-activity'"] - it totally ignores escape characters.

Every answer would be very appreciated.

PS: I can not change the name of attributes.

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.

我发现了这一点,并且可以满足我的要求:

if ((myReader.GetValue(18) == DBNull.Value) || string.Compare(myReader.GetValue(18).ToString(), "0") == 0)....

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