且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的汉字不能正确显示在C#字符串中

更新时间:2023-11-14 11:39:10

到目前为止,该信息是:

So far the information is:

  1. 您正在使用直接SQL INSERT脚本插入数据库.
  2. 数据在数据库中似乎已损坏.

问题可能出在两个地方:

The problem might lie in two places:

  1. 在INSERT语句中,插入值是否以N为前缀?

  1. 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

希望这会有所帮助.