How do i read a dbase file and apply different decoding?

烂漫一生 提交于 2019-12-12 12:25:40

问题


I have a dbf file endcoded as 866 codepage (DOS)

Using the code below, I'm trying to read it. Problem is that strings I get are formed as if the file was in code page 1252. I've checked other questions on SO and other forums with no luck so far. Looking for ideas on hot to read it propperly.

var ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\PathtoFile\;Extended Properties=""dBase 5.0""";
var  dBaseConnection = new System.Data.OleDb.OleDbConnection(ConnectionString );

dBaseConnection.Open();

var dBaseCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM FileName",dBaseConnection);
var dBaseDataReader =  dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess);


while( dBaseDataReader.Read()){

Encoding.GetEncoding(866).GetString(Encoding.GetEncoding(1252).GetBytes(dBaseDataReader.GetString(2)).Dump();  // Does not help 
}

回答1:


Where is the file from? What other file extensions are in the same folder (eg FPT/CDX/IDX/NTX)? That should give us a clue as to whether it is VFP or dBase or Clipper or something else.

If it's Visual Foxpro (VFP) data then you should install the VFPOLEDB provider from http://www.microsoft.com/en-gb/download/details.aspx?id=14839 and use one of the following connection strings taken from http://www.connectionstrings.com/visual-foxpro#vfp-ole-db-provider.

Database container (.DBC):

Provider=vfpoledb;Data Source=C:\MyDbFolder\MyDbContainer.dbc;Collating Sequence=machine;

Free table directory:

Provider=vfpoledb;Data Source=C:\MyDataDirectory\;Collating Sequence=general;

Connect to a single DBF-file:

Provider=vfpoledb;Data Source=C:\MyDataDirectory\MyTable.dbf;Collating Sequence=machine;

More info on the VFPOLEDB provider at http://msdn.microsoft.com/en-us/library/aa975609%28v=vs.71%29.aspx

Out of interest how do you know it is encoded as codepage 866? The byte at offset 29 in Visual Foxpro DBFs stores the codepage mark. See http://msdn.microsoft.com/en-us/library/aa975386%28v=vs.71%29.aspx




回答2:


I don't see provements that you got 1252 coded data. Your code trying to covnert from 1252 to 866 codepage failed. Thus it is not in 1252 codepage. I currently fixed the problem that driver returned non-single byte string. May be it is your problem too.

Solution:

Check the value of HKLM\SOFTWARE\Microsoft\Jet\4.0\Engines\Xbase\BDE key. It must be 2. If not or the key does not exist (this was my case), create DWORD parameter and set it to 2.

More info about this key you can find here: http://support.microsoft.com/kb/307455/en-us



来源:https://stackoverflow.com/questions/13026489/how-do-i-read-a-dbase-file-and-apply-different-decoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!