質問

i'm reading DBF file using OleDb this way :

[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();
}

Everything seems to be OK but there's problem with string data. The source database contains strings saved with CodePage=852 and I can't find way to properly read it.

I've tried to set CharSet/CodePage/CharacterSet into the connection string's extended properties but without any luck (in fact, exception was thrown: Couldn't find installable ISAM).

I've also tried to read it using 'vfpoledb' provider, still no luck.

For example there's string "FRANTIŠEK" but the str variable contains "FRANTIµEK".

Does anybody know how to do it ? Thanks

役に立ちましたか?

解決

Well, after few hours I've managed to get strings in proper way. The trick is in reading string columns as 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();
}

The only one problem is with the length inside varbinary CAST. But it works. Hope this helps someone too.

他のヒント

You can specify the locale identifier in the connection string. Try editing your connection string text to the following:

"Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties=\"dBase IV\";Locale Identifier=852;"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top