使用sqlserver 2005中的asp检索unicode

时间:2022-06-01 13:05:05

I'm using the following code to retrieve a message from the database and then write it out to a html page:

我正在使用以下代码从数据库中检索消息,然后将其写入html页面:

Dim strDSN, cnn, cmd
strDSN = "Driver={SQL Server};" & "Server=(local)\sql2k5;" & ...
set cnn = Server.CreateObject("ADODB.Connection")
cnn.ConnectionString = strDSN
cnn.CursorLocation = adUseClient
cnn.Open
set cmd = Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = cnn
cmd.CommandText = "select Message from tblRecipients where ID = 72"
cmd.CommandType = adCmdText
set getRecordset = Server.CreateObject("ADODB.Recordset")
getRecordset.Open cmd, , adOpenKeyset, adLockReadOnly

Response.write getRecordset("Message")

however all I get is "???????". I checked it wasn't the browser, that is what is actually written out.

但我得到的只是“???????”。我检查过它不是浏览器,实际上是写出来的。

to insert the message I ran update tblRecipients set Message = N'Добре' where ID = 72 in SQL server manager.

插入我运行的消息更新tblRecipients在SQL服务器管理器中设置Message =N'Добре',其中ID = 72。

Any help on how to get it to display properly?

有关如何正确显示的任何帮助?

4 个解决方案

#1


1  

Your db is probably storing data in something other than utf-8, (maybe utf-16 or ucs 2 ?), while your asp page (by default) expects utf-8. That's why your data appears correct from SQL management studio, but not from an ASP page. utf-8 data in the DB looks like funky characters when viewed from SQL Management Studio.

您的数据库可能存储的数据不是utf-8(可能是utf-16或ucs 2?),而您的asp页面(默认情况下)需要utf-8。这就是为什么您的数据在SQL管理工作室中显示正确,而不是从ASP页面显示。从SQL Management Studio查看时,数据库中的utf-8数据看起来像时髦的字符。

I'm still trying to figure out how to store my data in utf-16 and allow asp pages to display it w/o explicitly converting to utf-8 in each sql statement. Changing the asp charset="utf-16" didn't seem to do the trick.

我仍然试图弄清楚如何在utf-16中存储我的数据,并允许asp页面显示它,而不是在每个sql语句中显式转换为utf-8。更改asp charset =“utf-16”似乎没有办法解决问题。

#2


1  

Welcome to internationalization. I've been battling this ALL day.

欢迎国际化。我整天都在为这一天而战。

Ron is correct. Your database collation is probably set to Latin-1 or something like that, which, I believe, is equivalent to ISO-8859-1.

罗恩是对的。您的数据库排序规则可能设置为Latin-1或类似的东西,我相信,它等同于ISO-8859-1。

However you are storing the data will need to change. For instance, if you are pasting special characters, like smart double quotes, you will need to convert them prior to saving them to the database. Conversely, if you don't convert the data going in, you will have to to convert it using a third-party tool when displaying the data straight from the database. I came across a COM object by ChilKat that, I believe does this. I can't find anything built into Classic ASP that would give the ability to convert from ISO-8859-1 (or anything for that matter) to UTF-8 or vice versa.

但是,您存储的数据将需要更改。例如,如果要粘贴特殊字符(如智能双引号),则需要在将它们保存到数据库之前进行转换。相反,如果您不转换进入的数据,则必须在直接从数据库显示数据时使用第三方工具对其进行转换。我遇到了ChilKat的COM对象,我相信这样做。我找不到经典ASP内置的任何东西,它能够将ISO-8859-1(或其他任何东西)转换为UTF-8,反之亦然。

Also, within your HTML, you will need to set the charset equal to the encoding type you wish to use.

此外,在HTML中,您需要将charset设置为与要使用的编码类型相等。

Best of luck.

祝你好运。

#3


0  

You might try using the SQL Native Client to see if that fixes it for you, it is a bit of a wild guess, but I know that it has resolved other issues with data types and Classic ASP.

您可以尝试使用SQL Native Client来查看是否为您修复了它,这是一个疯狂的猜测,但我知道它已经解决了数据类型和经典ASP的其他问题。

#4


0  

Solution: Man, this was a weird one. The problem was that the actual ASP file was encoded in ANSI. Apparently this is what encoding is used when the file is passed to the client (kinda makes sense if you remember that an asp file is just a file being passed to the client that is modified slightly by the asp engine). The solution was to change the encoding of the ASP file to utf-8. Thus, when the utf-8 characters are written into the file on its way to the client, they match the encoding of the rest of the file and are read by the browser correctly.

解决方案:伙计,这是一个奇怪的问题。问题是实际的ASP文件是用ANSI编码的。显然这就是当文件传递给客户端时使用的编码(如果你记得asp文件只是传递给客户端的文件,有点被asp引擎修改,那就有意义了)。解决方案是将ASP文件的编码更改为utf-8。因此,当utf-8字符在到达客户端的途中写入文件时,它们与文件其余部分的编码匹配,并由浏览器正确读取。

Hope this helps the next person.
Cheers,
Andy.

希望这有助于下一个人。干杯,安迪。

#1


1  

Your db is probably storing data in something other than utf-8, (maybe utf-16 or ucs 2 ?), while your asp page (by default) expects utf-8. That's why your data appears correct from SQL management studio, but not from an ASP page. utf-8 data in the DB looks like funky characters when viewed from SQL Management Studio.

您的数据库可能存储的数据不是utf-8(可能是utf-16或ucs 2?),而您的asp页面(默认情况下)需要utf-8。这就是为什么您的数据在SQL管理工作室中显示正确,而不是从ASP页面显示。从SQL Management Studio查看时,数据库中的utf-8数据看起来像时髦的字符。

I'm still trying to figure out how to store my data in utf-16 and allow asp pages to display it w/o explicitly converting to utf-8 in each sql statement. Changing the asp charset="utf-16" didn't seem to do the trick.

我仍然试图弄清楚如何在utf-16中存储我的数据,并允许asp页面显示它,而不是在每个sql语句中显式转换为utf-8。更改asp charset =“utf-16”似乎没有办法解决问题。

#2


1  

Welcome to internationalization. I've been battling this ALL day.

欢迎国际化。我整天都在为这一天而战。

Ron is correct. Your database collation is probably set to Latin-1 or something like that, which, I believe, is equivalent to ISO-8859-1.

罗恩是对的。您的数据库排序规则可能设置为Latin-1或类似的东西,我相信,它等同于ISO-8859-1。

However you are storing the data will need to change. For instance, if you are pasting special characters, like smart double quotes, you will need to convert them prior to saving them to the database. Conversely, if you don't convert the data going in, you will have to to convert it using a third-party tool when displaying the data straight from the database. I came across a COM object by ChilKat that, I believe does this. I can't find anything built into Classic ASP that would give the ability to convert from ISO-8859-1 (or anything for that matter) to UTF-8 or vice versa.

但是,您存储的数据将需要更改。例如,如果要粘贴特殊字符(如智能双引号),则需要在将它们保存到数据库之前进行转换。相反,如果您不转换进入的数据,则必须在直接从数据库显示数据时使用第三方工具对其进行转换。我遇到了ChilKat的COM对象,我相信这样做。我找不到经典ASP内置的任何东西,它能够将ISO-8859-1(或其他任何东西)转换为UTF-8,反之亦然。

Also, within your HTML, you will need to set the charset equal to the encoding type you wish to use.

此外,在HTML中,您需要将charset设置为与要使用的编码类型相等。

Best of luck.

祝你好运。

#3


0  

You might try using the SQL Native Client to see if that fixes it for you, it is a bit of a wild guess, but I know that it has resolved other issues with data types and Classic ASP.

您可以尝试使用SQL Native Client来查看是否为您修复了它,这是一个疯狂的猜测,但我知道它已经解决了数据类型和经典ASP的其他问题。

#4


0  

Solution: Man, this was a weird one. The problem was that the actual ASP file was encoded in ANSI. Apparently this is what encoding is used when the file is passed to the client (kinda makes sense if you remember that an asp file is just a file being passed to the client that is modified slightly by the asp engine). The solution was to change the encoding of the ASP file to utf-8. Thus, when the utf-8 characters are written into the file on its way to the client, they match the encoding of the rest of the file and are read by the browser correctly.

解决方案:伙计,这是一个奇怪的问题。问题是实际的ASP文件是用ANSI编码的。显然这就是当文件传递给客户端时使用的编码(如果你记得asp文件只是传递给客户端的文件,有点被asp引擎修改,那就有意义了)。解决方案是将ASP文件的编码更改为utf-8。因此,当utf-8字符在到达客户端的途中写入文件时,它们与文件其余部分的编码匹配,并由浏览器正确读取。

Hope this helps the next person.
Cheers,
Andy.

希望这有助于下一个人。干杯,安迪。