Microsoft jet 数据库引擎在读取 dbf 文件时找不到对象 [英] The Microsoft jet database engine could not find object while reading dbf file

查看:61
本文介绍了Microsoft jet 数据库引擎在读取 dbf 文件时找不到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 文件位置:E:\Projects\SLAVE.DBF

我面临以下两个问题

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

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) 我面临的另一个问题是,当我将这些文件放在适当的位置时,我有 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 数据库引擎未找到所需的对象.你是缺少一些指令或路径.E:\Projects\SDW_plnParcel.dbf

microsoft jet database engine does not find required object. Are you missing some directive or path. E:\Projects\SDW_plnParcel.dbf

我很困惑为什么它正确读取从 Internet 下载的 SLAVE.DBF,为什么它不读取 line75.dbf 的 TABLE FIELDS DATA 以及它为什么抛出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;
}

推荐答案

在处理 .DBF 文件时,我总是有更好的结果 微软的 Visual Foxpro OleDb 提供程序

When dealing with .DBF files, I have always had better results working with Microsoft's Visual Foxpro OleDb Provider

简化格式的连接字符串

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

现在,不要做数据读取器——只是为了确保你能得到/看到你所期待的,尝试使用 DataAdapter...

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...

这篇关于Microsoft jet 数据库引擎在读取 dbf 文件时找不到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆