views:

227

answers:

2

I have a DBF file and a index file. I want to read index file and search records satisfy some condition. (for example: search records which its StudentName begin with "A" by using Student.DBF and StudentName.idx)

How do I do this programmatically?

A: 

It would be easiest to query via OleDB Connection

using System.Data.OleDb;
using System.Data;


OleDbConnection oConn = new OleDbConnection("Provider=VFPOLEDB.1;Data Source=C:\\PathToYourDataDirectory"); 
OleDbCommand oCmd = new OleDbCommand(); 
oCmd.Connection = oConn; 
oCmd.Connection.Open(); 
oCmd.CommandText = "select * from SomeTable where LEFT(StudentName,1) = 'A'"; 

// Create an OleDBAdapter to pull data down
// based on the pre-built SQL command and parameters
OleDbDataAdapter oDA = new OleDbDataAdapter(oCmd);
DataTable YourResults
oDA.Fill(YourResults);
oConn.Close(); 


// then you can scan through the records to get whatever
String EachField = "";
foreach( DataRow oRec in YourResults.Rows )
{
  EachField = oRec["StudentName"];
  // but now, you have ALL fields in the table record available for you

}
DRapp
A: 

I dont have the code off the top of my head, but if you do not want to use ODBC, then you should look into reading ESRI shape files, they consist of 3 parts (or more) a .DBF (what you are looking for), a PRJ file and a .SHP file. It could take some work, but you should be able to dig out the code. You should take a look at Sharpmap on codeplex. It's not a simple task to read a dbf w/o ODBC but it can be done, and there is a lot of code out there for doing this. You have to deal with big-endian vs little-endian values, and a range of file versions as well.

if you go here you will find code to read a dbf file. specifically, you would be interested in the public void ReadAttributes( Stream stream ) method.

Muad'Dib