ADO.. NET OleDB和非常旧的dBASE IV文件

本文关键字:dBASE IV 文件 非常 NET OleDB ADO | 更新日期: 2023-09-27 18:06:09

我正在使用OleDb读取DBF文件:

[TestMethod]
public void TestMethod2()
{
  const string path = @"D:'VL816183.DBF";
  var connection = new OleDbConnection(string.Format("Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties='"dBase IV'"", Path.GetDirectoryName(path)));
  connection.Open();
  var command = new OleDbCommand(string.Format("select MNO from {0}", Path.GetFileName(path)), connection);
  using (var reader = command.ExecuteReader())
  {
    while (reader.Read())
    {
      var str = (string)reader["MNO"];
    }
  }
  connection.Close();
}

一切似乎都很好,但有字符串数据的问题。源数据库包含用CodePage=852保存的字符串,我找不到正确读取它的方法。

我试图将CharSet/CodePage/CharacterSet设置为连接字符串的扩展属性,但没有任何运气(事实上,抛出异常:找不到可安装的ISAM)。

我也试过使用'vfpoledb'提供程序来阅读它,仍然没有运气。

例如,有字符串"FRANTIŠEK",但str变量包含"FRANTIµEK"。

有人知道怎么做吗?由于

ADO.. NET OleDB和非常旧的dBASE IV文件

好吧,几个小时后,我设法以适当的方式获得字符串。诀窍在于将字符串列读取为varbinary(length):

[TestMethod]
public void TestMethod2()
{
  const string path = @"D:'KN_Vzorka_2012'VL816183.DBF";
  var connection = new OleDbConnection(string.Format("Provider=vfpoledb;Data Source={0};Extended Properties='"dBase IV'";Locale Identifier=852;", Path.GetDirectoryName(path)));
  connection.Open();
  var command = new OleDbCommand(string.Format("select cast(MNO as varbinary(20)) as MNO FROM {0}", Path.GetFileName(path)), connection);
  using (var reader = command.ExecuteReader())
  {
    while (reader.Read())
    {
      var arr = (byte[])reader["MNO"];
      var str = Encoding.GetEncoding(852).GetString(arr);
    }
  }
  connection.Close();
}

唯一的问题是varbinary CAST内部的长度。但它是有效的。

您可以在连接字符串中指定区域设置标识符。尝试将连接字符串文本编辑为以下内容:

"Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties='"dBase IV'";Locale Identifier=852;"