问题
I am facing very strange issue. I have written class to which reads dbf file through oledb
connection. I have downloaded dbf file from internet and it is reading all data correctly.
DBF file location: E:\Projects\SLAVE.DBF
I am facing following 2 issues
1) When I try to read other dbf file then it is reading only its table fields. it is not reading table fields data.
E:\Projects\line75.dbf
2) The other issue I am facing I have DBF files when I put these files in location then i am getting exception that
microsoft jet database engine does not find required object. Are you missing some directive or path.
E:\Projects\SDW_plnParcel.dbf
I am totally confused why it is reading SLAVE.DBF
downloaded from internet correct, why it is not reading TABLE FIELDS DATA of line75.dbf
and why it is throwing exception on SDW_plnParcel.dbf.
My class and one function for this class is as follows:
public class dbfHandler
{
public dbfHandler()
{
this.dbfTable = new DataTable();
}
public void initconnection(String filepath) // initialise dbconnection
{
String[] splitString = filepath.Split('\\');
this.filename = splitString[splitString.Length - 1];
splitString = splitString.Where(w => w != splitString[splitString.Length - 1]).ToArray();
String folderPath = String.Join("\\", splitString);
this.dbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folderPath + ";Extended Properties=dBase III");
this.dbConnection.Open();
}
public List<String> getcolumnvalues(int fieldIndex, List<int> rowIndexes)
{
List<String> columnvalues = new List<string>();
try
{
if(this.dbConnection.State == ConnectionState.Open)
{
string mySQL = "select * from " + this.filename; // dbf table name
OleDbCommand MyQuery = new OleDbCommand(mySQL, this.dbConnection);
OleDbDataReader reader = MyQuery.ExecuteReader();
int rowCount = 0;
while(reader.Read())
{
bool match = rowIndexes.Any(item => item == rowCount);
if(match == true)
{
String value = reader.GetValue(fieldIndex).ToString();
columnvalues.Add(value);
}
rowCount++;
}
reader.Close();
}
}
catch(Exception e)
{
throw e;
}
return columnvalues;
}
private String filename;
private DataTable dbfTable;
private OleDbConnection dbConnection;
}
回答1:
When dealing with .DBF files, I have always had better results working with Microsoft's Visual Foxpro OleDb Provider
The connection string in simplified format
var connString = @"Provider=VFPOLEDB.1;Data Source=C:\SomePathToData;";
Now, instead of doing the data reader -- just to make sure you can get / see what you are expecting, try using a DataAdapter...
var da = new OleDataAdapter( yourSqlCmdObject, yourConnection)
var dt = new DataTable();
da.Fill(dt);
It should pull all columns from your query and all rows into proper data column types... Then you could cycle through all the column names, rows, etc..
foreach( DataColumn dc in dt.Columns )
var tmp = dc.ColumnName;
foreach( DataRow dr in dt.Rows )
{
object x = dr[0]; // get VALUE from column 0
x = dr["SpecificColumn"]; // if you KNOW the column name
}
Of which, you could tweak as needed. But if you only need a SPECIFIC column (or limited columns), change your query to quantify that.
Select OneField from YourTable...
来源:https://stackoverflow.com/questions/33687000/the-microsoft-jet-database-engine-could-not-find-object-while-reading-dbf-file