简体   繁体   English

C#打开DBF文件

[英]C# Open DBF file

I'm having a problem opening a DBF file - I need to open it, read everything and process it. 我在打开DBF文件时遇到问题 - 我需要打开它,读取所有内容并进行处理。 I tried several solutions (ODBC/OLEDB), several connection string, but nothing worked so far. 我尝试了几个解决方案(ODBC / OLEDB),几个连接字符串,但到目前为止没有任何工作。

The problem is, when I execute the SQL command to get everything from the file, nothing gets returned - no rows. 问题是,当我执行SQL命令从文件中获取所有内容时,不返回任何内容 - 没有行。 What's even more odd, the content of the DBF file being opened get deleted. 更奇怪的是,正在打开的DBF文件的内容被删除。

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. 我调试了代码并在Windows资源管理器中观看了文件正在打开。 When it reached this line: 当它达到这条线时:

da.Fill(ds);

the size of the file dropped to only a few Bytes (from hundreds of kB). 文件的大小只下降到几个字节(从数百KB)。

My next thought was to make the DBF file read only. 我的下一个想法是让DBF文件只读。 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. 我确定文件没有损坏,它是从某些DB直接导出的。 (No, I do not have access to that DB). (不,我无法访问该数据库)。 I can also open that file in MS Office no problem. 我也可以在MS Office中打开该文件没问题。

I cannot share the DBF file - it contains confidential data. 我无法共享DBF文件 - 它包含机密数据。

Two things... just because its a .DBF file extension might night mean its a Dbase IV file. 两件事......仅仅因为它的.DBF文件扩展名可能在晚上意味着它是一个Dbase IV文件。 It might actually be that of Visual Foxpro. 它实际上可能是Visual Foxpro的。 That said, I would look into downloading and installing the Visual Foxpro OleDB driver from Microsoft download . 也就是说,我会考虑从Microsoft下载下载和安装Visual Foxpro OleDB驱动程序。 Next, the OleDbConnection is pointing to the path that has the actual tables (you already have that). 接下来,OleDbConnection指向具有实际表的路径(您已经拥有该表)。

The query itself, shouldn't care about the extension, so I would change your call to get just then name via "Path.GetFileNameWithoutExtension" 查询本身,不应该关心扩展,所以我会改变你的调用,然后通过“Path.GetFileNameWithoutExtension”获取名称

It might be a combination of the two. 它可能是两者的结合。

Connection string for VFP provider VFP提供程序的连接字符串

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM