C#打开DBF文件 [英] C# Open DBF file

查看:142
本文介绍了C#打开DBF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,打开一个DBF文件 - 我需要打开它,阅读一切并对其进行处理。我试了几个解决方案(ODBC / OLEDB),几个连接字符串,但没有那么远的工作。

I'm having a problem opening a DBF file - I need to open it, read everything and process it. I tried several solutions (ODBC/OLEDB), several connection string, but nothing worked so far.

现在的问题是,当我执行SQL命令把一切从文件中说,没有东西回来了 - 任何行。什么是更奇怪,DBF文件的内容被打开被删除。

The problem is, when I execute the SQL command to get everything from the file, nothing gets returned - no rows. What's even more odd, the content of the DBF file being opened get deleted.

见code,我有:

public override bool OpenFile(string fileName, string subFileName = "")
{
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(fileName) + ";Extended Properties=dBASE IV;User ID=;Password=;");
    try
    {
        if (con.State == ConnectionState.Closed) { con.Open(); }
        OleDbDataAdapter da = new OleDbDataAdapter("select * from " + Path.GetFileName(fileName), con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        int i = ds.Tables[0].Rows.Count;
        return true;
    }
    catch
    {
        return false;
    }             
}

我调试的code,看着正在运行结束在Windows资源管理器中的文件。当它达到该行:

I debugged the code and watched the file being opend in Windows Explorer. When it reached this line:

da.Fill(ds);

的文件的大小下降到只有几个字节(从几百KB)。

the size of the file dropped to only a few Bytes (from hundreds of kB).

我的下一个想法是使只读DBF文件。然而,这从外部驱动器意外的异常引起的。

My next thought was to make the DBF file read only. That however cause an "unexpected exception from an external driver".

所以我的问题是 - 什么的挫折感?我敢肯定,该文件的没有的腐败,这是一些DB的直接出口。 (不,我没有访问该数据库)。我还可以在微软Office中打开该文件没有任何问题。

So my question is - what the heck? I'm sure the file is not corrupt, it is a direct export from some DB. (No, I do not have access to that DB). I can also open that file in MS Office no problem.

我不能共享DBF文件 - 它包含的机密数据

推荐答案

两件事情......只是因为它的.dbf文件扩展名可能夜间平均的一个dBASE IV文件。它实际上可能是Visual FoxPro中。这就是说,我会考虑下载和从微软安装Visual FoxPro的OLEDB驱动程序下载 。接下来,OleDbConnection的指向具有实际表的路径(你已经有一个)。

Two things... just because its a .DBF file extension might night mean its a Dbase IV file. It might actually be that of Visual Foxpro. That said, I would look into downloading and installing the Visual Foxpro OleDB driver from Microsoft download. Next, the OleDbConnection is pointing to the path that has the actual tables (you already have that).

查询本身,不应该关心的延伸,所以我会改变你的电话得到的只是然后通过Path.GetFileNameWithoutExtension

The query itself, shouldn't care about the extension, so I would change your call to get just then name via "Path.GetFileNameWithoutExtension"

有可能是两者的组合中。

It might be a combination of the two.

连接字符串VFP提供商

Connection string for VFP provider

"Provider=VFPOLEDB.1;Data Source=" + FullPathToDatabase

这篇关于C#打开DBF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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