如何使用ASP classic在数据库中读写utf-8字符?

发布于 2024-12-03 07:04:03 字数 2182 浏览 1 评论 0原文

我在插入时遇到问题在我的数据库中获取 UTF-8 字符。插入正确进行,但在显示时不显示 UTF-8 字符。下面是我的代码。你能告诉我哪里错了吗?

文件: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>

文件: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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-10 07:04:03

我不知道你正在使用哪个 SQL 服务器,但如果你使用的是 MS SQL 服务器,你应该在 unicode 字符串之前有一个前缀。 N'myString'

前缀表示后续字符串采用 Unicode。如果不为 Unicode 字符串常量添加 N 前缀,SQL Server 将在使用该字符串之前将其转换为当前数据库的非 Unicode 代码页。

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

示例:

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

也是ASP 文件会影响字符串的处理方式。
尝试将 .ASP 文件以 utf-8 格式保存在磁盘上。

另:请参阅此线程:内部字符串编码

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文