MS Access对"C#"数据类型进行“访问".类型对应? [英] To which C# data type does MS Access "Number" type correspond?

查看:197
本文介绍了MS Access对"C#"数据类型进行“访问".类型对应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://msdn .microsoft.com/en-us/library/office/aa211091(v = office.11​​).aspx ,MS Jet数据库引擎没有"NUMBER"数据类型;但这就是我在查询表的MS Access设计视图中看到的-

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

我正在检索的第一列数据类型是查询的"NUMBER"数据类型.

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

当我尝试将该值转换为Int32和Double时,我的代码都中断了.

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?

推荐答案

'Number'是数据类型的集合,而不是特定类型本身-您需要查看字段列表下方的字段属性看看实际的类型是什么.

'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.

或者,您可以通过编程方式查找字段类型.使用Delphi和DAO的代码如下所示:

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;

我认为C#/ADO.NET的等效形式类似.

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

这篇关于MS Access对"C#"数据类型进行“访问".类型对应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆