如何在ASP.NET中访问SQL并显示汉字?

发布于 2024-10-27 16:12:28 字数 983 浏览 6 评论 0原文

我有一个存储汉字的数据库。这很好用。该系统的代码是用vbscript ASP 编写的。这是一个示例 http://www.arrb.com.au/test.asp

在此处输入图像描述

我也通过 ASP.NET 访问该数据库,但字符不显示中文。

请参阅此处 http://www.arrb.com.au/Test-Page.aspx< /a>

在此处输入图像描述

为什么 ASP 可以工作而 ASP.NET 不能工作?

在 ASP.NET 中访问中文的代码类似于:

query = "select * from myTable ....";

SqlDataAdapter da = new SqlDataAdapter(query, Yart.SQLServerConnections.SqlConnection);
DataSet ds = new DataSet();
da.Fill(ds, "paragraphs");

Response.Write((string)ds.Tables["paragraphs"].Rows[0]["chinese"]);

我想这可能是我对字符串的转换可能会破坏编码?

请注意,在两个示例页面中我都有这个标签:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

I have a database that stores Chinese characters. This works fine. The code for this system is written in vbscript ASP. Here is a sample http://www.arrb.com.au/test.asp

enter image description here

I also access this database via ASP.NET but the characters do not come out in Chinese.

See here http://www.arrb.com.au/Test-Page.aspx

enter image description here

Why does ASP work and ASP.NET not work?

The code to access the Chinese in ASP.NET is similar to:

query = "select * from myTable ....";

SqlDataAdapter da = new SqlDataAdapter(query, Yart.SQLServerConnections.SqlConnection);
DataSet ds = new DataSet();
da.Fill(ds, "paragraphs");

Response.Write((string)ds.Tables["paragraphs"].Rows[0]["chinese"]);

I am thinking it might be my cast to a string that is destroying the encoding perhaps?

Note that in both sample pages I have this tag:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

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

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

发布评论

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

评论(3

那支青花 2024-11-03 16:12:28

将数据保存到数据库时使用什么编码?在执行 Response.Write 之前,尝试将字符串转换为使用相同的编码。

此处可用的编码列表... http://msdn.microsoft.com/en-us/library/system.text.encoding(v=VS.100).aspx

这是用于转换为正确类型的方法 http://msdn.microsoft.com/en-us/library/kdcak6ye.aspx

What encoding was used when saving the data to the database? Try converting your string to use the same encoding before doing a Response.Write.

List of encodings available here ... http://msdn.microsoft.com/en-us/library/system.text.encoding(v=VS.100).aspx

Here is the method to use for converting to the right type http://msdn.microsoft.com/en-us/library/kdcak6ye.aspx

唔猫 2024-11-03 16:12:28

上面的答案有帮助,这里是如何做到这一点...

网上有很多关于此的文章:

http://csharpindepth.com/Articles/General/Strings.aspx

http ://www.joelonsoftware.com/articles/Unicode.html

但我只想说这一点。当您保存表单回发中的字符时,字符被编码。如果您不指定编码,系统会为您确定编码。

这可能会非常令人困惑!

中文字符在 ASP 中可能看起来不错,但在 ASP.NET 中则不然。

因此,为了避免出现问题,请始终声明您的编码!通常我们使用utf-8。

在 ASP 中,执行以下操作:

<% @CodePage = 65001 %>
<%Option Explicit%>

<%
Session.CodePage = 65001 
Response.CodePage = 65001
Response.CharSet = "utf-8" 
%>

直接保存 SQL 时,确保将字符保存在 nvarchar、ntext 等中
确保 SQL 语句具有 N 字符

update [someTable] set [col]=N'...'

也在 HTML 中声明编码:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

在 ASP.NET 中,将其添加到 web.config:

<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
fileEncoding="utf-8"/>

The answers above helped, here is how to do it...

There are a lot of articles about this on the net:

http://csharpindepth.com/Articles/General/Strings.aspx

http://www.joelonsoftware.com/articles/Unicode.html

But suffice to say this. When you save characters from a form postback the characters will be encoded. If you do not specify an encoding it will be determined for you.

This can be very confusing!

Chinese characters may appear to look OK in ASP but not in ASP.NET

So to stop problems, always declare your encodings! Typically we use utf-8.

In ASP, do this:

<% @CodePage = 65001 %>
<%Option Explicit%>

<%
Session.CodePage = 65001 
Response.CodePage = 65001
Response.CharSet = "utf-8" 
%>

When saving SQL directly, make sure the characters get saved in nvarchar, ntext etc
Make sure the SQL statement has the N character

update [someTable] set [col]=N'...'

Declare the encoding in HTML as well:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

In ASP.NET, add this to web.config:

<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
fileEncoding="utf-8"/>
夜还是长夜 2024-11-03 16:12:28

也许这个类可以帮助你:

System.Text.UTF8Encoding

Maybe this class can help you:

System.Text.UTF8Encoding

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