C# 打开 DBF 文件

发布于 2024-12-13 15:44:48 字数 1160 浏览 3 评论 0原文

我在打开 DBF 文件时遇到问题 - 我需要打开它、读取所有内容并处理它。我尝试了几种解决方案(ODBC/OLEDB)、几种连接字符串,但到目前为止没有任何效果。

问题是,当我执行 SQL 命令以从文件中获取所有内容时,没有返回任何内容 - 没有行。更奇怪的是,正在打开的 DBF 文件的内容被删除了。

查看我的代码:

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

我调试了代码并观察了在 Windows 资源管理器中打开的文件。当到达这一行时:

da.Fill(ds);

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

我的下一个想法是使 DBF 文件只读。然而,这会导致“外部驱动程序出现意外异常”。

所以我的问题是——到底是什么?我确信该文件没有损坏,它是从某些数据库直接导出的。 (不,我无权访问该数据库)。我也可以在 MS Office 中打开该文件,没有问题。

我无法共享 DBF 文件 - 它包含机密数据。

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.

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.

See the code I have:

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

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

da.Fill(ds);

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

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

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.

I cannot share the DBF file - it contains confidential data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2024-12-20 15:44:48

有两件事......仅仅因为它的 .DBF 文件扩展名可能意味着它是 Dbase IV 文件。它实际上可能是 Visual Foxpro 的。也就是说,我会考虑从 Microsoft 下载 下载并安装 Visual Foxpro OleDB 驱动程序。接下来,OleDbConnection 指向具有实际表的路径(您已经拥有该表)。

查询本身不应该关心扩展名,所以我会更改您的调用以通过“Path.GetFileNameWithoutExtension”获取名称,

它可能是两者的组合。

VFP 提供程序的连接字符串

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

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

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.

Connection string for VFP provider

"Provider=VFPOLEDB.1;Data Source=" + FullPathToDatabase
梦毁影碎の 2024-12-20 15:44:48

这不是确切的答案,但它将帮助您找到问题。

尝试提供内联连接字符串并选择查询以确保问题不在于构建这些字符串。捕获异常并检查其详细信息。

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=;Password=;"); // give your path directly 
try
{
    con.Open();
    OleDbDataAdapter da = new OleDbDataAdapter("select * from tblCustomers.DBF", con); // update this query with your table name 
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    int i = ds.Tables[0].Rows.Count;
    return true;
}
catch(Exception e)
{
    var error = e.ToString();
    // check error details 
    return false;
}

This is not the exact answer but it will help you to find the issue.

Try to give an inline connection string and select query to make sure problem is not with building those. Catch the exception and check the details of it.

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=;Password=;"); // give your path directly 
try
{
    con.Open();
    OleDbDataAdapter da = new OleDbDataAdapter("select * from tblCustomers.DBF", con); // update this query with your table name 
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    int i = ds.Tables[0].Rows.Count;
    return true;
}
catch(Exception e)
{
    var error = e.ToString();
    // check error details 
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文