Oct 24, 2006

Uncide code in ASP & MSSQL

ref: http://support.jodohost.com/showthread.php?t=2356

Unicode has two encoding formats: UTF-8 and UCS-2. Web browsers speak UTF-8. On the other hand, most DBMSes speak UCS-2. In MS SQL 2000 Server, searching and string comparison work robustly if Unicode data were stored only in UCS-8 format. However, Internet Explorer can display Unicode data only in UTF-8.

Thus, the requirement is to convert between UTF-8 and UCS-2. Fortunately, in Microsoft-based web application, IIS can take responsibility of Unicode format conversion by adding "<% Session.Codepage=65001 %>" or "<%@ CodePage=65001 %>" to the server-side ASP script responsible for database manipulation.

eg.
<%@ Language=VBScript CODEPAGE=65001 %>
<%Session.Codepage=65001 %>

For more details, please visit
INF: Storing UTF-8 Data in SQL Server
http://support.microsoft.com/default...&Product=sql2k


If your application uses Active Server Pages (ASP) and you are using Internet Information Server (IIS) 5.0 and Microsoft Windows 2000, you can add "<% Session.Codepage=65001 %>" to your server-side ASP script. This instructs IIS to convert all dynamically generated strings (example: Response.Write) from UCS-2 to UTF-8 automatically before sending them to the client.

If you do not want to enable sessions, you can alternatively use the server-side directive "<%@ CodePage=65001 %>".

Any UTF-8 data sent from the client to the server via GET or POST is also converted to UCS-2 automatically. The Session.Codepage property is the recommended method to handle UTF-8 data within a web application. This Codepage setting is not available on IIS 4.0 and Windows NT 4.0. For additional information, see the following article in the Microsoft Knowledge Base:



In query statement, should use
select * from [TABLE] where [FIELDS] = N'[SEARCH_TEXT]'


Stored Procedures should be use adVarWChar instead of adVarChar

附:
'---- DataTypeEnum Values ----
Const adEmpty = 0
Const adTinyInt = 16
Const adSmallInt = 2
Const adInteger = 3
Const adBigInt = 20
Const adUnsignedTinyInt = 17
Const adUnsignedSmallInt = 18
Const adUnsignedInt = 19
Const adUnsignedBigInt = 21
Const adSingle = 4
Const adDouble = 5
Const adCurrency = 6
Const adDecimal = 14
Const adNumeric = 131
Const adBoolean = 11
Const adError = 10
Const adUserDefined = 132
Const adVariant = 12
Const adIDispatch = 9
Const adIUnknown = 13
Const adGUID = 72
Const adDate = 7
Const adDBDate = 133
Const adDBTime = 134
Const adDBTimeStamp = 135
Const adBSTR = 8
Const adChar = 129
Const adVarChar = 200
Const adLongVarChar = 201
Const adWChar = 130
Const adVarWChar = 202
Const adLongVarWChar = 203
Const adBinary = 128
Const adVarBinary = 204
Const adLongVarBinary = 205
Const adChapter = 136
Const adFileTime = 64
Const adDBFileTime = 137
Const adPropVariant = 138
Const adVarNumeric = 139


===========================================
After import data from Text file to database, NText is used
using
cast([FIELD_NAME] as nvarchar(%SIZE%))
to convert the fileds into nvarchar