Currently, I am in the process of updating all of our Delphi 2007 code base to Delphi XE2. The biggest consideration is the ANSI to Unicode conversion, which we've dealt with by re-defining all base types (char/string) to ANSI types (ansichar/ansistring). This has worked in many of our programs, until I started working with the database.
The problem started when I converted a program that stores information read from a file into an SQL Server 2008 database. Suddenly simple queries that used a string to locate data would fail, such as:
SELECT id FROM table WHERE name = 'something'
The name
field is a varchar
. I found that I was able to complete the query successfully by prefixing the string name with an N
. I was under the impression that varchar
could only store ANSI characters, but it appears to be storing Unicode?
Some more information: the name field in Delphi is string[13]
, but I've tried dropping the [13]
. The database collation is SQL_Latin1_General_CP1_CI_AS
. We use ADO to interface with the database. The connection information is stored in the ODBC Administrator.
NOTE: I've solved my actual problem thanks to a bit of direction from Panagiotis. The name we read from our map file is an array[1..24] of AnsiChar
. This value was being implicitly converted to string[13]
, which was including null characters. So a name with 5 characters was really being stored as the 5 characters + 8 null characters in the database.