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

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

问题描述

所以我是有正确转换的字符法国的一个问题。基本上,我有将数据发送到SQL数据库的形式。然后,在另一页上,从这个数据库检索数据并显示给用户。但由于其他网页上的表单的输入是法语中的数据(串)进行显示用奇怪的腐败人物。我用下面的函数转换器一个字符串正确的字符集克服了这个问题。然而,很明显更好的解决办法是先转换,然后将其发送到数据库。现在,这里的code从数据库中检索到相应的字符集字符串转换:

 功能ConvertFromUTF8(SIN)    昏暗的OIN:设置OIN =的CreateObject(的ADODB.Stream)    oIn.Open
    oIn.CharSet =的Windows-1252
    oIn.WriteText罪
    oIn.Position = 0
    oIn.CharSet =UTF-8
    ConvertFromUTF8 = oIn.ReadText
    oIn.Close结束功能

我从这里得到这个功能:<一href=\"http://stackoverflow.com/questions/916118/classic-asp-how-to-convert-a-utf-8-string-to-ucs-2/920405#920405\">Classic ASP - 如何将UTF-8字符串转换为UCS-2 <​​/A>

现在我的问题是,什么功能做我用字符串转换事前,然后将它们发送到数据库,这样当我找回它们,他们会好到去?

试过保罗的方法:

所以这是第1页和第2页第1页包含了一个形式,提交时,发送字符串,然后在第2我想保罗的解决方案页面移除功能ConvertFromUTF8,并把它留给作为检索数据库这是之前(它返回奇怪mangolian字符)。在那之后,我加在第1页的顶部以下行以及第2页。

 &LT;%@ LANGUAGE =VBSCRIPTcodePAGE =65001%&GT;

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

 响应。codePAGE = 65001
Response.Charset的=UTF-8

但它没有工作:(

编辑:它的工作!谢谢你这么多大家的帮助!
所有我需要做的是添加codePAGE = 65001第3页(我甚至没有谈),在写入到数据库的一部分发生了上面。


解决方案

保罗的回答是没有错的,但它不是唯一的部分故事

您将需要通过每个步骤,以确保您得到一致的结果;


  

重要:这些步骤必须在Web应用程序的每一个页面上进行,否则将有问题(由保罗的评论强调)



  1. 每个页面必须的保存 UTF-8 编码仔细检查这是有些IDE将默认为视窗-1252

  2. 每个页面都需要下面的行添加为页面的第一行,使它更容易些我在一个包含文件中把这个与其他值一起,所以我可以将它们包含在每一页,因为我去

    包含文件 - page_encoding.asp

    &LT;%@ LANGUAGE =VBSCRIPTcodePAGE = 65001%GT;
    &LT;%
      Response.Charset的=UTF-8
      响应。codePAGE = 65001
    %GT;

    在ASP页的顶部使用(preFER将在config文件夹中的网站的根目录)

    &LT;! - #包括虚拟=/配置/ page_encoding.asp - &GT;

    Response.Charset的=UTF-8是设置的同等学历;在HTTP字符集 内容类型头。
    响应。codePAGE = 65001 告诉的ASP来处理所有动态字符串为 UTF-8


  3. 在页面中包含文件也将必须的保存 UTF-8 编码(仔细检查这些也)。


请按照这些步骤,你的页面会工作,此刻你的问题是某些网页被PTED为间$ P $的Windows 1252 而有些则是被视为 UTF-8 ,你就结束了一个不匹配的编码。

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

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?

Tried Paul's Method:

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" 

But it didn't work :(

Edit: it works!, thank you so much everyone for your help! All I needed to do was add "CodePage = 65001" on top of Page 3 (which I didn't even talk about), where the writing to the DB part was happening.

解决方案

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

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

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. Each page needs to be saved using UTF-8 encoding double check this as some IDEs will default to Windows-1252.
  2. 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.

    Include File - page_encoding.asp

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

    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" 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.

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

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天全站免登陆