.NET 字符编码问题

发布于 2024-08-09 11:21:14 字数 297 浏览 5 评论 0原文

我在 .NET 网站上遇到了一些问题,其中拉丁语 (åäö) 和拉脱维亚语 (āē) 字符需要共存。在数据库中,我在字段中使用 nvarcharntext,如果我将文本直接输入数据库,则一切正常。

问题是,当我使用执行 SQL UPDATE 的表单输入完全相同的文本时,拉脱维亚字符会转换为类似的拉丁字符,因此字符串“åäöāē”将保存为“åäöae”。

我尝试了一些不同的 CodePages,但尚未获得我想要的结果。有人有什么建议吗?

I've got some problems on a .NET site where both Latin (åäö) and Latvian (āē) characters would need to co-exist. In the database, I'm using nvarchar and ntext for my fields, and if I enter the texts directly into the database, everything works fine.

The problem is when I enter the exact same text using a form that does an SQL UPDATE, the Latvian chars get converted to similar Latin chars, so the string "åäöāē" would be saved as "åäöae".

I've tried a bit with different CodePages, but I'm not yet getting the results I want. Does anyone have any suggestions?

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

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

发布评论

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

评论(3

毁虫ゝ 2024-08-16 11:21:14

您的 SQL 语句是否将值作为 unicode 值传递到数据库?

例如,基本示例

INSERT YourTable VALUES ('Unicode Value')

应该是:

INSERT YourTable VALUES (N'Unicode Value')

显然,我建议您使用参数化 SQL/存储过程并将参数定义为 NVARCHAR,但您明白我的意思。

Is your SQL statement passing the value in to the db as a unicode value?

e.g. basic example

INSERT YourTable VALUES ('Unicode Value')

should be:

INSERT YourTable VALUES (N'Unicode Value')

Obviously I'd recommend you use parameterised SQL/stored procedure and define the parameter as an NVARCHAR, but you get what I'm saying.

成熟稳重的好男人 2024-08-16 11:21:14

通过分别查看管道的每个部分来了解发生了什么情况。

请参阅我关于调试 unicode 问题的文章并诊断哪里出了问题。您可能会遇到问题的地方:

  • 从用户处获取数据
  • 更新数据库
  • 从数据库中获取
  • 显示您获取的数据

独立检查每一项,切勿依赖任何特定字体等。 打印列出所涉及的 Unicode 代码点(根据文章)。

哦,无论您在何处指定它,请使用可以对所有内容进行编码的编码 - 我建议使用 UTF-8。

Work out what's going on by taking each piece of the pipeline separately.

See my article on debugging unicode problems and diagnose where things are going wrong. Places that you may be seeing problems:

  • Getting the data from the user
  • Updating the database
  • Fetching from the database
  • Displaying the data you've fetched

Check each of these independently, never relying on any particular font etc. Print out the Unicode code points involved (as per the article).

Oh, and wherever you get to specify it, use an encoding which can encode everything - I'd suggest UTF-8.

暖心男生 2024-08-16 11:21:14

Windows 窗体应用

在 Windows 窗体文本框中输入文本时,您需要使用 unicode 字体,例如 Arial Unicode MS。

网页

确保您使用 UTF-8 作为响应代码页。来自 Microsoft 的精彩文章正是关于此http://support.microsoft.com/kb/893663

UTF-8 将正确编码任何字符。请记住,数据库中的 NText 和 NVarChar 是 UTF-16 数据类型,因此从查询分析器查看数据可能会正确显示。

在 SQL 中

如果您动态构建 SQL,请确保使用 N 前缀,例如

INSERT INTO TABLE (Name, Number) VALUES (N'MyName', 1)

Windows Forms apps

When entering text into a windows forms textbox you need to use a unicode font such as Arial Unicode MS.

Web page

Make sure you are using UTF-8 as your response codepage. Great article from Microsoft on exactly this http://support.microsoft.com/kb/893663

UTF-8 will correctly encode any characters. Keep in mind, NText and NVarChar in your DB are UTF-16 datatypes, so viewing the data from Query Analyser might will show it correctly.

In your SQL

If you're constructing the SQL dynamically, make sure you use the N prefix e.g.

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