简体   繁体   中英

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

I have been using DbfDataReader to read DBF files in my C# application. 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:

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. Is there anyway to use "FIRSTNAME" (or the column name) as the key (or accessor) for what is essentially a name/value pair? 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:

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"])
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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