开发者

How to read and write utf-8 characters in database using ASP classic?

I am facing a problem inserting & fetching UTF-8 characters in my database. The inserts happen properly but while displaying it doesn't show the UTF-8 characters. Below is my code. Can you please tell me where am I going wrong?

File: utf8_test.html

        <html><head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>UTF8</title>
    </head>
    <body><DIV align=center>
        <form action="utf8_insert.asp" method="post"><table>
            <tr><td>UTF-8:</td><td><input type="text" name="utf8" id="utf8"/></td></tr>
            <tr><td align="center" colspan="2"><input type="submit" value="submit"></input></td></tr>         
        </table></form>
    </DIV></body></html>

File : utf8_insert.asp

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <% option explicit
    dim objConn, objRS , strSql, input, test
    Response.CodePage = 65001 
    Response.CharSet = "utf-8" %>
    <html>
    <head>开发者_运维技巧<title>Test UTF </title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <head/>
    <body>
    <h1>Test UTF </h1>
    <%
        'File saved in ANSI - also works in UTF-8
        input = Request.Form("utf8")
        test = "多语言测试 test"
        response.write "Test: " + test + "</br></br>"
        session("norm_dsn") ="dsn=norm_dsn;driver=SQL Server;uid=username;pwd=password"
        set objConn=server.CreateObject("ADODB.Connection")
        objConn.Open session("norm_dsn")
        strSql = "INSERT INTO test_utf8 (text, date_log) VALUES ('" + input +"', sysdate)"
        objConn.execute(strSql)
        set  objRS = Server.CreateObject("ADODB.RECORDSET")
        strSql="select * from test_utf8 order by date_log"
        set objRS=objConn.execute(strSql)
        while NOT objRS.EOF
            response.write objRS("text") & "</br>"
            objRS.MoveNext 
        wend
        objRS.close
        set objRS = nothing 
        objConn.Close
        set objConn=nothing
    %>
    </body></html>


I dont know which SQL-server you are using, but if you are using MS SQL server you should have a prefix before unicode strings. N'myString'.

The prefix denotes that the subsequent string is in Unicode. If you do not prefix a Unicode string constant with N, SQL Server will convert it to the non-Unicode code page of the current database before it uses the string.

http://support.microsoft.com/kb/239530

Example:

insert into myTable (columnName) values(N'My unicode string goes here')

Also the encoding of the ASP-file can affect how strings are handled. Try saving the .ASP-file as utf-8 on disk.

Also: See this thread: internal string encoding

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜