使用Delphi处理Unicode字符6 [英] Handling of Unicode Characters using Delphi 6
问题描述
它读取一个文件,根据规范解析文件,执行验证和上传到数据库( SQL Server 2008 Express Edition )
我们必须为具有双字节字符集(DBCS)的操作系统提供支持,例如日语操作系统
因此,我们将SQL Server中的数据库字段从varchar更改为nvarchar。
轮询在具有DBCS的操作系统中正常工作。如果
系统区域设置设置为日语/中文/韩文,并且操作系统具有相应的语言包,它也可以成功地用于非DBCS操作系统。
但是,如果语言环境设置为英文,则数据库包含双字节字符的垃圾字符。
我执行了一些测试,但未能识别解决方案。
eg如果我使用TStringList从 UTF-8 文件读取,并将其保存到另一个文件,那么Unicode数据将被保存。
但是,如果我使用文件的内容来运行使用TADOQuery组件的更新查询,则会显示垃圾字符。
数据库还包含垃圾字符。
PFB示例代码:
var
stlTemp:TStringList;
qry:TADOQuery;
stQuery:string;
begin
stlTemp:= TStringList.Create;
qry:= TADOQuery.Create(nil);
stlTemp.LoadFromFile('D:\DelphiUnicode\unicode.txt');
//stlTemp.SaveToFile('D:\DelphiUnicode\1.txt'); //这个工作。即使
// stlTemp.Strings [0]包含垃圾字符,如果看在
stQuery:='UPDATE dbo.receivers SET company ='+ QuotedStr(stlTemp.Strings [0 ])+
'WHERE receiver_cd = N'+ QuotedStr('Receiver');
// company是数据库中的nvarchar字段
qry.Connection:= ADOConnection1;
with qry do
开始
关闭;
SQL.Clear;
SQL.Add(stQuery);
ExecSQL;
结束
qry.Free;
stlTemp.Free
end;
上述代码在DBCS操作系统中正常工作。
我已经尝试使用字符串,widestring和UTF8String。但是,如果语言环境设置为英文,这在英文操作系统中不起作用。
请提供此问题的任何指针。
在非Unicode Delphi版本中,基础知识是您需要使用 WideString
s(Unicode)而不是 String
s(Ansi)。
忘记 TADOQuery.SQL
(TStrings),并使用 TADODataSet.CommandText
或$ code> TADOCommand.CommandText (WideString)或typecast TADOQuery
as TADODataSet
。例如:
stlTemp:TWideStringList; //< - Unicode字符串 - TNT或其他Unicode lib
qry:TADOQuery;
stQuery:WideString; //< - Unicode string
TADODataSet(qry).CommandText:= stQuery;
RowsAffected:= qry.ExecSQL;
您还可以使用 TADOConnection.Execute(stQuery)
直接执行查询。
请注意参数化查询: ADODB.TParameters。 ParseSQL
是Ansi。如果 ParamCheck
是true(默认情况下) TADOCommand.SetCommandText
- > AssignCommandText $ c如果您的查询是Unicode(
InitParameters
是Ansi),$ c>将导致
问题。
(请注意,您可以直接使用ADO Command.Parameters
- 使用?
chars作为参数的占位符,而不是Delphi的约定
:param_name
)。
QuotedStr
返回Ansi字符串。你需要一个宽版本的这个功能(TNT)
另外,As @Arioch'提到的 TNT Unicode控件
套件是你最好用来制作Delphi Unicode应用程序。
它具有在应用程序中成功管理Unicode任务所需的所有控件和类。
简而言之,您需要考虑宽:)
I have a polling application developed in Delphi 6. It reads a file, parse the file according to specification, performs validation and uploads into database (SQL Server 2008 Express Edition)
We had to provide support for Operating Systems having Double Byte Character Sets (DBCS) e.g. Japanese OS. So, we changed the database fields in SQL Server from varchar to nvarchar.
Polling works fine in Operating Systems with DBCS. It also works successfully for non-DBCS Operating systems, if the System Locale is set to Japanese/Chinese/Korean and Operating system has the respective language pack. But, if the Locale is set to english then, the database contains junk characters for the double byte characters.
I performed a few tests but failed to identify the solution.
e.g. If I read from a UTF-8 file using a TStringList and save it to another file then, the Unicode data is saved. But, if I use the contents of the file to run an update query using TADOQuery component then, the junk characters are shown. The database also contains the junk characters.
PFB the sample code:
var
stlTemp : TStringList;
qry : TADOQuery;
stQuery : string;
begin
stlTemp := TStringList.Create;
qry := TADOQuery.Create(nil);
stlTemp.LoadFromFile('D:\DelphiUnicode\unicode.txt');
//stlTemp.SaveToFile('D:\DelphiUnicode\1.txt'); // This works. Even though
//the stlTemp.Strings[0] contains junk characters if seen in watch
stQuery := 'UPDATE dbo.receivers SET company = ' + QuotedStr(stlTemp.Strings[0]) +
' WHERE receiver_cd = N' + QuotedStr('Receiver');
//company is a nvarchar field in the database
qry.Connection := ADOConnection1;
with qry do
begin
Close;
SQL.Clear;
SQL.Add(stQuery);
ExecSQL;
end;
qry.Free;
stlTemp.Free
end;
The above code works fine in a DBCS Operating system.
I have tried playing with string,widestring and UTF8String. But, this does not work in English OS if the locale is set to English.
Please provide any pointers for this issue.
In non Unicode Delphi version, The basics are that you need to work with WideString
s (Unicode) instead of String
s (Ansi).
Forget about TADOQuery.SQL
(TStrings), and work with TADODataSet.CommandText
or TADOCommand.CommandText
(WideString) or typecast TADOQuery
as TADODataSet
. e.g:
stlTemp: TWideStringList; // <- Unicode strings - TNT or other Unicode lib
qry: TADOQuery;
stQuery: WideString; // <- Unicode string
TADODataSet(qry).CommandText := stQuery;
RowsAffected := qry.ExecSQL;
You can also use TADOConnection.Execute(stQuery)
to execute queries directly.
Be extra careful with Parametrized queries: ADODB.TParameters.ParseSQL
is Ansi. If ParamCheck
is true (by default) TADOCommand.SetCommandText
->AssignCommandText
will cause
problems if your Query is Unicode (InitParameters
is Ansi).
(note that you can use ADO Command.Parameters
directly - using ?
chars as placeholder for the parameter instead of Delphi's convention :param_name
).
QuotedStr
returns Ansi string. You need a Wide version of this function (TNT)
Also, As @Arioch 'The mentioned TNT Unicode Controls
suite is your best fried for making Delphi Unicode application.
It has all the controls and classes you need to successfully manage Unicode tasks in your application.
In short, you need to think Wide :)
这篇关于使用Delphi处理Unicode字符6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!