根据http://msdn.microsoft.com/en-us/library/office/aa211091(v=office.11).aspx,MS Jet数据库引擎没有“NUMBER”数据类型;然而,这就是我在我正在查询的表的MS Access设计视图中看到的 -
我正在检索查询的第一列的数据类型是“NUMBER”数据类型。
当我尝试将该值转换为Int32和Double时,我已经破解了这段代码:
// A set comprised of two columns are returned from a query in cmd
using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
{
while (oleDbD8aReader != null && oleDbD8aReader.Read())
{
//var accountID = oleDbD8aReader.GetString(0); // <-- this fails (it's a number, not a string)
//var accountID = oleDbD8aReader.GetInt32(0); // <-- this also fails!
var accountID = oleDbD8aReader.GetDouble(0); // <-- as does this!
var deptName = oleDbD8aReader.GetString(1);
. . .
}
}
为什么无法转换为string,int或double?我应该将它转换为什么呢?
答案 0 :(得分:2)
或者,您可以通过编程方式查看字段类型。使用Delphi和DAO代码看起来像这样:
uses
DAO_TLB; //may need importing first via Component|Import Component
procedure Foo;
var
Engine: DBEngine;
DB: Database;
Table: TableDef;
begin
Engine := CoDBEngine.Create;
DB := Engine.OpenDatabase('C:\Somewhere\Database.mdb',
{exclusively?}False, {read-only?}True, '');
Table := DB.TableDefs['MyTableName'];
case Table.Fields['MyFieldName'].Type_ of
dbBoolean: ;
dbByte: ;
dbInteger: ;
dbLong: ;
dbCurrency: ;
dbSingle: ;
dbDouble: ;
dbDate: ;
dbBinary: ;
dbText: ;
dbLongBinary: ;
dbMemo: ;
dbGUID: ;
dbBigInt: ;
dbVarBinary: ;
dbChar: ;
dbNumeric: ;
dbDecimal: ;
dbFloat: ;
dbTime: ;
dbTimeStamp: ;
end;
end;
我认为C#/ ADO.NET等价物是类似的。