To which C# data type does MS Access “Number” type correspond?

雨燕双飞 提交于 2019-12-14 03:14:15

问题


According to http://msdn.microsoft.com/en-us/library/office/aa211091(v=office.11).aspx, the MS Jet database engine has no "NUMBER" data type; yet that is what I see in the MS Access design view of the table that I'm querying - that the

data type of the first column I'm retrieving wit the query is of the "NUMBER" data type.

I've got this code that breaks, both when I try to convert that value to Int32 and to 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);    
        . . .
    }
}

Why does it fail to convert to string, int, or double? What should I convert it to instead?


回答1:


'Number' in the Access table designer is a collection of data types, not a specific type itself - you need to look at the field properties below the field list to see what the actual type is.

Alternatively, you could look the field type up programmatically. Using Delphi and DAO the code would look like this:

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;

I would assume the C#/ADO.NET equivalent would be similar-ish.



来源:https://stackoverflow.com/questions/19552970/to-which-c-sharp-data-type-does-ms-access-number-type-correspond

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!