将 UTF-8 字符串经典 ASP 转换为 SQL 数据库 [英] Convert UTF-8 String Classic ASP to SQL Database

查看:29
本文介绍了将 UTF-8 字符串经典 ASP 转换为 SQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在正确转换法语字符时遇到了问题.基本上,我有一个将数据发送到 SQL 数据库的表单.然后,在另一个页面上,检索来自该数据库的数据并将其显示给用户.但是数据(字符串)以奇怪的损坏字符显示,因为另一页表单中的输入是法语.我通过使用以下函数将字符串转换为正确的字符集克服了这个问题.但是,显然更好的解决方案是首先转换它,然后将其发送到数据库.下面是将从数据库检索到的字符串转换为适当字符集的代码:

So I was having an issue with converting French characters correctly. Basically, I have a form which sends data to an SQL Database. Then, on another page, data from this DB is retrieved and displayed to the user. But the data (strings) were being displayed with wierd corrupt characters because the input in the form on the other page was in French. I overcame this problem by using the following function which converters a string to the correct charset. HOWEVER, obviously the better solution is to convert it FIRST and then send it to the database. Now here's the code to convert a string retrieved from a DB to the appropriate charset:

Function ConvertFromUTF8(sIn)

    Dim oIn: Set oIn = CreateObject("ADODB.Stream")

    oIn.Open
    oIn.CharSet = "WIndows-1252"
    oIn.WriteText sIn
    oIn.Position = 0
    oIn.CharSet = "UTF-8"
    ConvertFromUTF8 = oIn.ReadText
    oIn.Close

End Function

我从这里得到这个函数:经典 ASP - 如何将 UTF-8 字符串转换为 UCS-2?

I got this function from here: Classic ASP - How to convert a UTF-8 string to UCS-2?

现在我的问题是,我使用什么函数预先转换字符串,然后将它们发送到数据库,以便在我检索它们时可以使用它们?

Now my question is, what function do I use to convert strings beforehand and then send them to the database, so that when I retrieve them they will be good-to-go?

尝试了保罗的方法:

所以有第 1 页和第 2 页.第 1 页包含一个表单,该表单在提交时将字符串发送到数据库,然后在第 2 页中检索.我尝试了 Paul 的解决方案,删除函数 ConvertFromUTF8 并将其保留为之前是(它返回了奇怪的芒果字符).之后,我在第 1 页和第 2 页的顶部添加了以下行.

So there's page 1, and page 2. Page 1 contains a form which, when submitted, sends the string to the DB which is then retrieved in page 2. I tried Paul's solution by removing the function ConvertFromUTF8 and leaving it to as it was before (it returned wierd mangolian characters). After that, I added the following line on top of Page 1 as well as Page 2.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

我在两个页面上也有以下内容:

I also have the following on both of the pages:

Response.CodePage = 65001 
Response.CharSet = "UTF-8" 

但是没有用:(

它有效!非常感谢大家的帮助!我需要做的就是在第 3 页的顶部添加CodePage = 65001"(我什至没有谈论),写入 DB 部分的位置.

推荐答案

Paul 的回答没有错,但不是唯一需要考虑的部分:

Paul's answer isn't wrong but it is not the only part to consider:

您需要完成这些步骤中的每一个,以确保获得一致的结果;

You will need to go through each of these steps to make sure that you are getting consistent results;

重要提示:必须在您的 Web 应用程序中的每个页面上执行这些步骤,否则您会遇到问题(Paul 的评论强调了这一点).

IMPORTANT: These steps have to be performed on each and every page in your web application or you will have problems (emphasized by Paul's comment).

  1. 每个页面都需要保存使用UTF-8编码仔细检查这一点,因为一些IDE将默认为Windows-1252(也经常被误称为ANSI").

  1. Each page needs to be saved using UTF-8 encoding double check this as some IDEs will default to Windows-1252 (also often misnamed as "ANSI").

每个页面都需要添加以下行作为页面的第一行,为方便起见,我将其与其他一些值一起放在包含文件中,以便我可以在每个页面中包含它们.

Each page will need the following line added as the very first line in the page, to make this easier I put this along with some other values in an include file so I can include them in each page as I go.

<%@Language="VBScript" CodePage = 65001 %>
<% 
  Response.CharSet = "UTF-8"
  Response.CodePage = 65001
%>

在 ASP 页面顶部使用(最好放在 web 根目录的 config 文件夹中)

Usage in the top of an ASP page (prefer to put in a config folder at the root of the web)

<!-- #include virtual="/config/page_encoding.asp" -->

Response.Charset = "UTF-8" 相当于在 HTTP content-type 标头中设置 ;charset.Response.CodePage = 65001 告诉 ASP 将所有动态字符串处理为 UTF-8.

Response.Charset = "UTF-8" is the equivalent of setting the ;charset in the HTTP content-type header. Response.CodePage = 65001 tell's ASP to process all dynamic strings as UTF-8.

页面中的包含文件也必须保存使用UTF-8编码(也请仔细检查这些).

Include files in the page will also have to be saved using UTF-8 encoding (double check these also).

按照这些步骤操作,您的页面将正常工作,您目前的问题是某些页面被解释为 Windows-1252 而其他页面被视为 UTF-8并且您最终会遇到编码不匹配的情况.

Follow these steps and your page will work, your problem at the moment is some pages are being interpreted as Windows-1252 while others are being treated as UTF-8 and you're ending up with a mis-match in encoding.

这篇关于将 UTF-8 字符串经典 ASP 转换为 SQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆