ADO.NET OleDB and a very old dBASE IV file

I am reading a DBF file using OleDb as follows:

[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 in order, but there is a problem with the string data. The original database contains rows stored using CodePage = 852 , and I cannot find a way to read it correctly.

I tried to set CharSet / CodePage / CharacterSet in the extended properties of the connection string, but with no luck (in fact, an exception was thrown: it was not possible to find the installed ISAM).

I also tried to read it using the "vfpoledb" provider, still no luck.

For example, there is the string "FRANTIŠEK", but the str variable contains "FRANTIμEK".

Does anyone know how to do this? Thanks

+2
source share
2 answers

Well, after a few hours I managed to get the rows correctly. The trick is to read the columns of the row 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 problem is the length inside varbinary cast. But it does work. Hope this helps someone too.

+3
source

You can specify the locale identifier in the connection string. Try editing the text of the connection string as follows:

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

Source: https://habr.com/ru/post/1484892/


All Articles