Microsoft jet数据库引擎在读取dbf文件时找不到对象



我面临着一个非常奇怪的问题。我已经编写了通过oledb连接读取dbf文件的类。我已经从网上下载了dbf文件,它正在正确读取所有数据。

DBF文件位置:E:\Projects\SLAVE.DBF

我面临以下2个问题

1) 当我尝试读取其他dbf文件时,它只读取其表字段。它不是在读取表字段数据。E:Projectsline75.dbf

2) 我面临的另一个问题是,当我把这些文件放在适当的位置时,我有DBF文件,然后我得到的异常

microsoftjet数据库引擎找不到所需的对象。你是吗缺少某些指令或路径。E:ProjectsSDW_plnParcel.dbf

我完全不明白为什么它正确读取从互联网下载的SLAVE.DBF,为什么它没有读取line75.dbf的TABLE FIELDS DATA,为什么它在SDW_plnParcel.dbf. 上抛出异常

我的类和这个类的一个函数如下:

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;
}

在处理.DBF文件时,我使用Microsoft的Visual Foxpro OleDb Provider 总是能获得更好的结果

简化格式的连接字符串

var connString = @"Provider=VFPOLEDB.1;Data Source=C:SomePathToData;";

现在,与其做数据读取器——只是为了确保您可以获得/看到您所期望的内容,不如尝试使用DataAdapter。。。

var da = new OleDataAdapter( yourSqlCmdObject, yourConnection)
var dt = new DataTable();
da.Fill(dt);

它应该将查询中的所有列和所有行拉入正确的数据列类型。。。然后您可以循环浏览所有的列名、行等

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
}

其中,您可以根据需要进行调整。但是,如果您只需要一个SPECIFIC列(或有限的列),请更改查询以量化它。

Select OneField from YourTable...

最新更新