如何在ASP.NET中访问SQL并显示汉字?
我有一个存储汉字的数据库。这很好用。该系统的代码是用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
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将数据保存到数据库时使用什么编码?在执行 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
上面的答案有帮助,这里是如何做到这一点...
网上有很多关于此的文章:
http://csharpindepth.com/Articles/General/Strings.aspx
http ://www.joelonsoftware.com/articles/Unicode.html
但我只想说这一点。当您保存表单回发中的字符时,字符将被编码。如果您不指定编码,系统会为您确定编码。
这可能会非常令人困惑!
中文字符在 ASP 中可能看起来不错,但在 ASP.NET 中则不然。
因此,为了避免出现问题,请始终声明您的编码!通常我们使用utf-8。
在 ASP 中,执行以下操作:
直接保存 SQL 时,确保将字符保存在 nvarchar、ntext 等中
确保 SQL 语句具有 N 字符
也在 HTML 中声明编码:
在 ASP.NET 中,将其添加到 web.config:
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:
When saving SQL directly, make sure the characters get saved in nvarchar, ntext etc
Make sure the SQL statement has the N character
Declare the encoding in HTML as well:
In ASP.NET, add this to web.config:
也许这个类可以帮助你:
System.Text.UTF8Encoding
Maybe this class can help you:
System.Text.UTF8Encoding