Read SQL Server field text value in Delphi XE with simultaneously character conversion
I have a SQL Server 2005 database with COLLATION SQL_Latin_General_CP1_CI_AS
and I want to run a query from Delphi XE via ADO. Data in SQL Server is Greek and Latin characters. But in Delphi I get unreadable character strings.开发者_如何转开发 How can I manage this problem with Delphi XE ?
Since you say that you have both Greek and Latin characters in the db I guess that you are already using nvarchar
in the db.
In Delphi you should then use TWideStringField for nvarchar
fields. TStringField is for varchar
(ansistring).
Field1 contains "γειά σου"
StringField := ADODataSet1.FieldByName('Field1') as TStringField;
ShowMessage(StringField.Value);
ShowMessage shows "?e??s??"
This works fine
WideStringField := ADODataSet1.FieldByName('Field1') as TWideStringField;
ShowMessage(WideStringField.Value);
Edit 1
If you have varchar
fields in db you should use TStringField
and you need to make sure that the "Language for non-Unicode programs" is Greek(Greece)
.
"Control Panel - Region and Language - Administrative - Change system locale..."
I have found that sometimes UTF-8 is stored in databases in VarChar
fields, usually from Java programs.
If you see things like â€"
, there's a good chance that's what is going on.
You could try
// Delphi 2009+
UTF8ToUnicodeString(RawByteString( db_value ))
// Delphi 2007 and older
UTF8Decode( db_value )
If this is the case, you can also use a sql function to convert the VarChar fields to NVarChar
精彩评论