简体   繁体   English

从 C# 中的 DBF (Visual FoxPro) 文件中读取特定列

[英]Reading specific columns from DBF (Visual FoxPro) file in C#

I have been using DbfDataReader to read DBF files in my C# application.我一直在使用DbfDataReader在我的 C# 应用程序中读取 DBF 文件。 So far, I can read column name, column index, and iterate through the records successfully.到目前为止,我可以成功读取列名、列索引并遍历记录。 There does not appear to be a way to read specific column data I'd like without using the column index.似乎没有一种方法可以在不使用列索引的情况下读取我想要的特定列数据。 For example, I can get at the FIRSTNAME value with a statement like:例如,我可以使用如下语句获取 FIRSTNAME 值:

using DbfDataReader;    
var dbfPath = "/CONTACTS.DBF";
using (var dbfTable = new DbfTable(dbfPath, EncodingProvider.UTF8))
{
     var dbfRecord = new DbfRecord(dbfTable);
     while (dbfTable.Read(dbfRecord))
     {
          Console.WriteLine(dbfRecord.Values[1].ToString()); // would prefer to use something like dbfRecord.Values["FIRSTNAME"].ToString()
          Console.WriteLine(dbfRecord.Values[2].ToString()); // would prefer to use something like dbfRecord.Values["LASTNAME"].ToString()
     }
  }

Where 1 is the index of the FIRSTNAME column and 2 is the index of the LASTNAME column.其中1是 FIRSTNAME 列的索引, 2是 LASTNAME 列的索引。 Is there anyway to use "FIRSTNAME" (or the column name) as the key (or accessor) for what is essentially a name/value pair?无论如何使用“FIRSTNAME”(或列名)作为本质上是名称/值对的键(或访问器)? My goal is to get all of the columns I care about without having to first build this map each time.我的目标是获取我关心的所有列,而不必每次都先构建此地图。 (Please forgive me if the terms I am using are not exactly right). (如果我使用的条款不完全正确,请原谅我)。

Thanks so much for taking a look at this...非常感谢你看这个...

Use the DbfDataReader class as below:使用 DbfDataReader 类,如下所示:

var dbfPath = "/CONTACTS.DBF";
var options = new DbfDataReaderOptions
{
    SkipDeletedRecords = true,
    Encoding = EncodingProvider.UTF8
};

using (var dbfDataReader = new DbfDataReader.DbfDataReader(dbfPath, options))
{
    while (dbfDataReader.Read())
    {
        Console.WriteLine(dbfDataReader["FIRSTNAME"])
        Console.WriteLine(dbfDataReader["LASTNAME"])
    }
}

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

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