【问题标题】:Capture and insert Unicode text (Cyrillic) into MS access database捕获 Unicode 文本(西里尔文)并将其插入 MS 访问数据库
【发布时间】:2014-02-26 22:33:08
【问题描述】:

我继承了一个旧的 Web 应用程序,它正在使用经典的 ASP 将表单中收集的数据写入 Access 2007 数据库。

现在他们需要它来收集西里尔字母的输入。

我完全不熟悉代码页/字符集,以及使用非拉丁字母。

我尝试将输入表单页面上的字符集更改为 ISO-8859-1 ,它似乎确实存储了字符的 ascii 值(例如:#1076;)。因此,浏览器可以很好地解释和读取,但在将数据导出到 excel 以传递给需要它的部门方面几乎没有用。

所以我的问题是:

有没有一种简单的方法可以从 Web 表单中捕获西里尔字符并将它们作为西里尔字符插入到我的访问表中?

或交替

access 数据库中是否有工具或设置可以将十进制值 (#1076;) 转换为 access 本身内的西里尔字符。

【问题讨论】:

  • 在 ASP 文件的开头使用<%@ CODEPAGE = 65001%>
  • 如果您始终使用 UTF-8 编码,这就是 'Codepage = 65001' 所做的,那么如果您使用表单输入西里尔文,它将在您的网页上输出为西里尔文,但是它在 Access 中可能看起来很奇怪,例如“Ласкаво просимо до Львова”可能看起来像“Ëàñêàâî ïðîñèìî äî Ëüâîâà” 问题是,您可以在 Access 中使用 UTF-8。这个问题可能会有所帮助 - stackoverflow.com/questions/5681078/… - 或者,您可以查看设置经典 ASP 页面以导出 Excel 电子表格
  • @John 只要他们以adVarWChar 的形式插入数据,ADO 就应该处理到 unicode 的转换。

标签: ms-access vbscript asp-classic ms-access-2007 cyrillic


【解决方案1】:

如果您的页面坚持使用 UTF-8,它们应该可以工作(但请参阅下面的重要说明)。虽然 Access 确实在内部将 Unicode 字符存储为 UTF-8,但 Access OLEDB 驱动程序会为您处理转换。

考虑以下示例脚本(其中65001 是 UTF-8 的“代码页”):

<%@ CODEPAGE = 65001 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Classic ASP Unicode Test</title>
</head>
<body bgcolor="white" text="black">
<%
Dim con, cmd, rst
Const adVarWChar = 202
Const adParamInput = 1
Set con = CreateObject("ADODB.Connection")
con.Mode = 3  ' adModeReadWrite
con.Open _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\_wwwdata\unicodeTest.mdb;"
If Len(Trim(Request.Form("word"))) > 0 Then
    Set cmd = CreateObject("ADODB.Command")
    cmd.ActiveConnection = con
    cmd.CommandText = "INSERT INTO vocabulary (word, language, english_equiv) VALUES (?,?,?)"
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, Request.Form("word"))
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, Request.Form("language"))
    cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, Request.Form("english_equiv"))
    cmd.Execute
    Set cmd = Nothing
End If
%>
<h2>Word list:</h2>
<table border=1>
    <tr>
        <th>word</th><th>language</th><th>english_equiv</th>
    </tr>
<%
Set rst = CreateObject("ADODB.Recordset")
rst.Open _
        "SELECT * FROM vocabulary ORDER BY ID", _
        con, 3, 3
Do Until rst.EOF
    Response.Write "<tr>"
    Response.Write "<td>" & rst("word").Value & "</td>"
    Response.Write "<td>" & rst("language").Value & "</td>"
    Response.Write "<td>" & rst("english_equiv").Value & "</td>"
    Response.Write "</tr>"
    rst.MoveNext
Loop
Response.Write "</table>"
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
%>
<h2>Add a new entry:</h2>
<form action="<% Response.Write Request.ServerVariables("SCRIPT_NAME") %>" method="POST">
<table>
    <tr>
        <td align="right">word:</td>
        <td><input type="text" name="word"></td>
    </tr>
    <tr>
        <td align="right">language:</td>
        <td><input type="text" name="language"></td>
    </tr>
    <tr>
        <td align="right">english_equiv:</td>
        <td><input type="text" name="english_equiv"></td>
    </tr>
    <tr>
        <td></td>
        <td align="center"><input type="submit" value="Submit"></td>
    </tr>
</table>
</body>
</html>

从 Access 数据库中名为 [vocabulary] 的表开始

当我们加载我们看到的 ASP 页面时

        

如果我们为俄语单词添加新条目

        

然后点击“提交”页面会刷新

        

如果我们检查 Access 中的表格,我们会看到

重要提示

请注意,您不应将 Access 数据库用作 Web 应用程序的后端数据存储; Microsoft强烈建议不要这样做(参考:here)。

【讨论】:

  • 轻微的错字应该是&lt;%@ CODEPAGE = 65001 %&gt; 它缺少@ ASP processing directive
  • @Lankymart 非常感谢您的指正!我已经更新了我的答案。
猜你喜欢
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2021-03-13
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多