如何将 UTF-8 数据从经典 asp 表单发布转换为 UCS-2 以插入 SQL Server 2008 r2?

发布于 2024-10-01 01:21:50 字数 370 浏览 0 评论 0原文

我正在“现代化”一个使用 Access 2000 数据库的经典 asp 应用程序。

我在 SQL Server 2008r2 上重写了数据库,并将所有字段更改为使用较新的启用了 unicode 的 nchar、nvarchar、ntext 并导入了旧数据。我也从IIS 6切换到IIS 7

经典的asp是使用UTF-8收集和写入数据。

现在,应用程序在网页中正确显示旧数据,但当我触摸它时,即:更新或插入数据正在损坏。我假设在将数据写入 SQL Server 之前,我需要以某种方式将 UTF-8 数据从经典 asp 转换为 UCS-2。

但如何呢?

注意:似乎sql server在从access导入数据时自动将utf-8数据转换为可用的格式。

I am in the process of "modernizing" a classic asp application that uses a Access 2000 database.

I rewrote the database on SQL Server 2008r2 and changed all of the fields to use the newer unicode enabled nchar, nvarchar, ntext and imported the old data. I also switched to IIS 7 from IIS 6

The classic asp is collecting and writing data using UTF-8.

Now the application shows the OLD data correctly in the web pages but as son as I touch it ie: UPDATE or INSERT the data is getting corrupted. I assume I need to somehow convert the UTF-8 data from the classic asp to UCS-2 somehow before I write the data into SQL server.

But how?

NOTE: it seems that sql server auto converted the utf-8 data into a usable format when it imported the data from access.

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

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

发布评论

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

评论(1

风苍溪 2024-10-08 01:21:50

您必须通过在插入值前面添加 N 来告诉 SQL Server 2008 您正在发送 unicode 数据。所以就像这样

strTest = "Служба мгновенных сообщений"
strSQL = "INSERT INTO tblTest (test) VALUES (N'"&strTest&"')"

N 告诉 SQL Server 将内容视为 Unicode。并且不会损坏数据。

有关详细信息,请参阅 http://support.microsoft.com/kb/239530

这是在经典 ASP IIS 7 SQL Server 2008r2

CREATE TABLE [dbo].[tblTest](
    [test] [nvarchar](255) NULL,
    [id] [int] IDENTITY(1,1) NOT NULL

ASP 页上运行的测试代码

<%

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

strTest = Request("Test")

Set cnn = Server.CreateObject("ADODB.Connection")
strConnectionString = Application("DBConnStr")
cnn.Open strConnectionString



strSQL = "INSERT INTO tblTest (test) VALUES (N'"&strTest&"')"
Set rsData = cnn.Execute(strSQL)

%>
 <html xmlns="http://www.w3.org/1999/xhtml" charset="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title></title>

</head 
<body>
    <form action="test.asp" method="post" name="form1" >
        <br/><br/><br/><center>
<table border="1">
    <tr><td><b>Test SQL Write</b> </td></tr>
    <tr><td><input type="text" name="Test" style="width: 142px" Value="<%=strtext%>" /></td></tr>
    <tr><td><input type="Submit" value="Submit" name "Submit" /></td></tr></table> </center>
</form>


</body>
</html>

You have to tell SQL Server 2008 that you are sending in unicode data by adding an N to the front of your insert value. so its like this

strTest = "Служба мгновенных сообщений"
strSQL = "INSERT INTO tblTest (test) VALUES (N'"&strTest&"')"

The N tells SQL server to treat the Contents as Unicode. and does not corrupt the data.

See http://support.microsoft.com/kb/239530 for further info.

Here is test code Run on Classic ASP IIS 7 SQL Server 2008r2

CREATE TABLE [dbo].[tblTest](
    [test] [nvarchar](255) NULL,
    [id] [int] IDENTITY(1,1) NOT NULL

ASP Page

<%

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

strTest = Request("Test")

Set cnn = Server.CreateObject("ADODB.Connection")
strConnectionString = Application("DBConnStr")
cnn.Open strConnectionString



strSQL = "INSERT INTO tblTest (test) VALUES (N'"&strTest&"')"
Set rsData = cnn.Execute(strSQL)

%>
 <html xmlns="http://www.w3.org/1999/xhtml" charset="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title></title>

</head 
<body>
    <form action="test.asp" method="post" name="form1" >
        <br/><br/><br/><center>
<table border="1">
    <tr><td><b>Test SQL Write</b> </td></tr>
    <tr><td><input type="text" name="Test" style="width: 142px" Value="<%=strtext%>" /></td></tr>
    <tr><td><input type="Submit" value="Submit" name "Submit" /></td></tr></table> </center>
</form>


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