为什么我的汉字不能正确显示在C#字符串中 [英] Why are my Chinese characters not displayed correctly in c# string
问题描述
我正在将中文和英文文本存储在SQL Server 2005数据库中并在网页上显示它,但是中文显示不正确.我一直在阅读有关该主题的文章,并做了以下工作:
I am storing Chinese and English text in an SQL Server 2005 database and displaying it on a webpage, but the Chinese is not being displayed correctly. I have been reading about the subject and have done the following:
- 在我的INSERT语句中的文本之前使用N
- 将字段类型设置为nvarchar
- 将页面的字符集设置为UTF-8
当我将汉字直接插入页面时(即不要从数据库中获取它们),汉字会正确显示在页面中
Chinese characters are being displayed in the page correctly when I insert them directly into the page i.e. don't get them from the database
这些应该显示的字符:全澳甲流确诊病例已破100
These are the characters that should be displayed:全澳甲流确诊病例已破100
这是从数据库中检索文本时显示的内容:全澳ç" ²æµç¡®è¯Šç—…例已ç´1001
This is what is displayed when the text is retrieved from the database: 全澳ç"²æµç¡®è¯Šç—…ä¾‹å·²ç ´1001
这似乎与c#中的字符串处理方式有关,因为可以用经典的asp正确检索和显示中文
This seems to be something that is related to how strings are handled in c# because the Chinese can get retrieved and displayed correctly in classic asp
我还有什么需要做的事情才能将数据从数据库中取出,转换成字符串并正确地在aspx页面上输出?
Is there anything else I need to do to get the data out of the database, into a string and output correctly on an aspx page?
推荐答案
到目前为止,该信息是:
So far the information is:
- 您正在使用直接SQL INSERT脚本插入数据库.
- 数据在数据库中似乎已损坏.
问题可能出在两个地方:
The problem might lie in two places:
-
在INSERT语句中,插入值是否以N为前缀?
In your INSERT statement, did you prefix the insert value with N?
插入#tmp值(N'全澳甲流确诊病例已破100')
INSERT INTO #tmp VALUES (N'全澳甲流确诊病例已破100')
如果在值前加上N,则String对象是否保存正确的数据?
If you prefix the value with N, does the String object hold the correct data?
字符串sql ="INSERT INTO #tmp VALUES(N'" + value +')"
String sql = "INSERT INTO #tmp VALUES (N' " + value + "')"
在这里,我假设 value 是一个String对象.
Here I assume value is a String object.
此String对象是否包含正确的汉字?
Does this String object hold the correct Chinese characters?
尝试打印出它的值并查看.
Try print out its value and see.
已更新:
我们假设INSERT查询的结构如下:
Let's assume the INSERT query is constructed as below:
String sql = "INSERT INTO #tmp VALUES (N' " + value + "')"
我认为值包含汉字.
您直接将汉字赋值了吗?喜欢
Did you assign the Chinese characters into value directly? Like
String value = "全澳甲流确诊病例已破100";
上面的代码将起作用.但是,如果您进行了任何中间处理,都会引起问题.
The above code shall work. However, if you have done any intermediate processing, it will cause problem.
我之前做了一个本地化的TC项目;以前的架构师已经完成了几次ASP所需的编码转换;但它们会在.NET中造成问题:
I did a localized TC project before; the previous architect had done several encoding conversions which are necessary in ASP; but they will create problem in .NET:
String value = "全澳甲流确诊病例已破100";
Encoding tc = Encoding.GetEncoding("BIG5");
byte[] bytes = tc.GetBytes(value);
value = Encoding.Unicode.GetString(bytes);
上述转换是不必要的.在.NET中,只需直接分配即可:
The above conversions are unnecessary. In .NET, simply direct assignment will work:
String value = "全澳甲流确诊病例已破100";
这是因为String常量和String对象本身是Unicode兼容的.
That is because String constants and the String object itself are Unicode compliant.
框架库,例如File IO,在读取未使用Unicode编码的文件时,会将外部编码转换为Unicode;换句话说,框架将为您完成这项肮脏的工作.您无需经常执行手动编码转换.
The framework library, such as File IO, when reading a file which is not encoded in Unicode, they will convert the foreign encoding to Unicode; in other words, the framework will do this dirty job for you. You do not need to perform manual encoding conversion most of time.
更新:理解ASP用于将数据插入SQL服务器.
Update: Understood that ASP is used to insert data into an SQL server.
我写了一小段ASP,将一些中文字符插入SQL数据库,并且可以正常工作.
I have written a small piece of ASP to insert some Chinese chars into SQL database and it works.
我有一个名为"trans"的数据库,并且在其中创建了一个表"temp". ASP页以UTF-8编码.
I have a database named "trans" and I created a table "temp" inside. The ASP page is encoded in UTF-8.
<html>
<head title="Untitled">
<meta http-equiv="content-type" content="text/html";charset="utf-8">
</head>
<body>
<script language="vbscript" runat="server">
If Request.Form("Button1") = "Submit" Then
SqlQuery = "INSERT INTO trans..temp VALUES (N'" + Request.Form("Text1") + "')"
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = *********
cn.Properties("Initial Catalog").Value = "TRANS"
cn.Properties("User ID").Value = "sa"
cn.Properties("Password").Value = **********
cn.Properties("Persist Security Info").Value = False
cn.Open
cn.Execute(SqlQuery)
cn.Close
Set cn = Nothing
Response.Write SqlQuery
End If
</script>
<form name="form1" method="post" action="input.asp">
<input name="Text1" type="text" />
<input name="Button1" value="Submit" type="submit" />
</form>
</body>
</html>
该表在我的数据库中定义如下:
The table is defined as belows in my database:
create table temp (data NVARCHAR(100))
多次提交ASP页面,我的表包含正确的中文数据:
Submit the ASP page several times and my table contains proper Chinese data:
select * from trans..temp
data
----------------
test
测试
全澳甲流确诊病例已破100
希望这会有所帮助.
这篇关于为什么我的汉字不能正确显示在C#字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!