【问题标题】:C# - dbf External table is not in the expected formatC# - dbf 外部表不是预期的格式
【发布时间】:2017-02-27 06:37:26
【问题描述】:

我在这里挣扎。我正在尝试从 dbf 文件中获取数据。使用下面的连接字符串和代码。

DataTable YourResultSet = new DataTable();
        const string path = "D:\\Sample\\Database\\client.dbf";
        string conStr = String.Format("Provider = Microsoft.Jet.Oledb.4.0; Data Source = {0}; Extended Properties = \"dBase IV\"", Path.GetDirectoryName(path));
        var connection = new OleDbConnection(conStr);
        connection.Open();

        var command = new OleDbCommand(string.Format("select id from {0}", Path.GetFileName(path)), connection);

        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var str = (string)reader["id"];
            }
        }

        connection.Close();

很少有 dbf 文件可以读取,大部分 dbf 文件无法获取。执行代码时出现 “外部表不是预期格式”的错误。

即使我使用不同的连接字符串,如 vfopledb、vfoledb1、jet、ace 等。

我的机器是 64 位的,我使用的是 vs2013。请帮帮我。

不要否定,因为我已经尝试了关于同一问题的所有堆栈溢出答案。

【问题讨论】:

  • 您是否尝试过在您的选择语句中使用表名,例如select id from client?你得到同样的错误吗?哪一行导致错误?
  • 是的,我也尝试过该查询。但没有改善。会有同样的错误。执行 reader 语句时会出现错误。

标签: c# oledb visual-foxpro dbf dbase


【解决方案1】:

使用 VFPOLEDB 驱动程序。 Jet 或 ACE 驱动程序无法识别所有 dbf 表。 VFPOLEDB 驱动程序是 32 位的,这意味着您应该在 C# 项目(属性/构建目标平台)中以 x86 为目标。

void Main()
{
    DataTable YourResultSet = new DataTable();
    string path = @"D:\Sample\Database\client.dbf";

    using (OleDbConnection connection =
    new OleDbConnection("Provider=VFPOLEDB;Data Source=" + Path.GetDirectoryName(path)))
    using (OleDbCommand command = new OleDbCommand(
    string.Format("select id from {0}", Path.GetFileNameWithoutExtension(path)), connection))
    {
    connection.Open();
    YourResultSet.Load(command.ExecuteReader());
    connection.Close();
    }

    Form f = new Form();
    DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, DataSource=YourResultSet};
    f.Controls.Add(dgv);
    f.Show();
}

【讨论】:

  • 您好,感谢您的宝贵回答。我已尝试使用您的示例代码,并将目标 CPU 版本设置为 x86。但错误是 d:\sample\database\client.dbf is not a table.。请帮帮我
  • 要使其正常工作,.dbf 应该是 VFP 已知格式的 xBase 表。也许不是,或者标头可能已损坏。您可以将文件通过电子邮件发送给我,以便我检查您是否愿意(deu dot edu dot tr 的 cetin dot basoz)
  • 顺便说一句,如果它是 VFP 已知的 DBF,您可以使用 fox.wikis.com/wc.dll?Wiki~NotATable~WIN_COM_API 上的代码(VFP 代码)修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 2010-11-11
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多