简体   繁体   English

从OleDbDataReader中的MS ACCESS读取具有特殊名称的属性

[英]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". 当accdb数据库的字段名称带有字符“-”,“%”,“ /”或字段名称为“ Level”时,我无法读取accdb数据库。 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. 我还尝试了myReader["\Level"], myReader["'Macro-activity'"] -完全忽略转义字符。

Every answer would be very appreciated. 每个答案将不胜感激。

PS: I can not change the name of attributes. PS:我无法更改属性名称。

Escaping of columns names in Access uses braces, [ & ] , not single quotes. Access中的列名转义使用大括号[ & ]而不是单引号。

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. 单引号用于字符串,因此在select语句中使用“ Level”将返回文本“ Level”作为结果集中的未命名列。 If you try myReader.GetItem(1) it will return "Level" for every row. 如果尝试使用myReader.GetItem(1) ,它将为每一行返回"Level"

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM