如何使用ASP classic在数据库中读写utf-8字符?
我在插入时遇到问题在我的数据库中获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你正在使用哪个 SQL 服务器,但如果你使用的是 MS SQL 服务器,你应该在 unicode 字符串之前有一个前缀。
N'myString'
。前缀表示后续字符串采用 Unicode。如果不为 Unicode 字符串常量添加 N 前缀,SQL Server 将在使用该字符串之前将其转换为当前数据库的非 Unicode 代码页。
http://support.microsoft.com/kb/239530
示例:
也是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:
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