Delphi:ADOConnection,DBASE3和字符集(错误?) [英] Delphi: ADOConnection, DBASE3 and character set (bug?)

查看:111
本文介绍了Delphi:ADOConnection,DBASE3和字符集(错误?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi XE3,Win7教授.

Delphi XE3, Win7 Prof.

我需要写入DBASE 3(旧格式)文件以为类似DOS的应用程序(Clipper?)导出数据. 好的,我认为:MS DBASE驱动程序可以做到这一点.

I need to write into DBASE 3 (old format) files to export data for a DOS-like application (Clipper?). Ok, I thought: MS DBASE driver can do this.

但是我对匈牙利口音有疑问.

But I have problem with hungarian accents.

我尝试了以下连接字符串:

I tried this connection string:

Driver={Microsoft dBASE Driver (*.dbf)};DriverID=21;Dbq=c:\temp;Extended Properties=dBASE III;charSet=CP 852;Locale Identifier=1038;Character Set=CP 852;CODEPAGE=852

如我所见,它不能只写ANSI文件(DOS应用程序接受CP852字符).

As I saw it cannot write only ANSI files (the DOS app accepts CP852 chars).

我试图用AnsiToOEM转换内容,但是保存时丢失了一些字符.在记录中,我看到了很好的内容,但是保存的文件包含错误的重音符号. 测试文本为árvíztűrőtükörfúrógép". 结果中缺少í",ó",Ó".

I tried to convert the content with AnsiToOEM, but some characters lost on save. In the record I see good content, but the saved file contains wrong accents. The test text is "árvíztűrő tükörfúrógép". The "í", "ó", "Ó" is missing from the result.

我发现了一些奇怪的东西!

And I found some strange thing!

如果主窗体具有打开的ADOConnection(在DFM中,connected属性为true),那么我将从DBASE文件中读取好字符,并将其写入文件中-ANSI字符将正确转换. í"可以,ó"可以. 该ADOConnection对象可能与阅读器不同.

If the main form have an opened ADOConnection (the connected property is true in the DFM) then I will read good characters from the DBASE files, and I can write them into the file - the ANSI characters will be converted correctly. "í" is ok, "ó" is ok. This ADOConnection object could be different than the reader.

如果我在IDE模式下关闭此ADOConnection,打开的文件将不会转换,因此我会看到一些奇怪的带重音符号,而且我不会在文件中写上好文字.

If I close this ADOConnection in IDE mode, the opened files won't be converted, so I will see some strange accented chars, and I won't write good text into the file.

这很奇怪,因为如果我通过代码在FormCreate上打开此连接,则会出现问题... 如果资源流媒体从DFM中读取ADOConnection的活动(真"值)"connected"属性,则可以读写ADOQuery记录!

It is strange, because if I open this connection on FormCreate by code, the problem will appear... I can read and write the ADOQuery records if the resource streamer read the ADOConnection's active (True value) "connected" property from the DFM!

我不知道后台发生了什么,以及如何强制这个ADO字符转换例程正常工作,但是我浪费了很多时间去寻找一个可以正常工作的DBASE III导出器,而我发现的只是一个类似bug的东西...

I don't know what happened in the background, and how to force this ADO character transformation routine to work, but I wasted more days to find a working DBASE III exporter, and I have found only a buglike thing...

有人知道这是什么吗?为什么只有在DFM中连接了ADOConnection时,ADO字符编码器/解码器才能工作? 或者如何使用ADODB.Connection而不是ADOConnection对象来避免这种副作用?

Does anyone know what is this? Why the ADO character encoder/decoder works only if I had a connected ADOConnection in DFM? Or how I can use ADODB.Connection instead of ADOConnection object to avoid this side effect?

感谢每个想法!

推荐答案

我看到我需要设置代码页来修复ADO的字符串.

As I see I need to set the code page to fix the string for ADO.

var
  s: string;
  aStr1, aStr2: AnsiString;

begin
...

s := 'árvíztûrõ tükörfúrógép';
aStr1 := s;
SetLength(aStr2, Length(aStr1));
AnsiToOemBuff(PAnsiChar(aStr1), PAnsiChar(aStr2), Length(aStr1));
SetCodePage(RawbyteString(aStr2), 852, False); // THIS IS THE SOLUTION
ADOQuery1.FieldBYName('name').AsAnsiString := aStr2;

否则,某些事情将在后台再次转换我的AnsiString.

Otherwise something is converting my AnsiString again in the background.

这篇关于Delphi:ADOConnection,DBASE3和字符集(错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆