Solve the Problem of displaying garbled characters when inserting Chinese data in MSSQL!

Source: Internet
Author: User

In the past two days, when I write an SQL statement to insert data into the database, garbled characters are displayed. English and numbers are displayed normally.

First, I will confirm whether the field type in the data table is incorrect. After confirmation, I first set the field to nvarchar. This is correct, in principle, Chinese characters can be displayed normally.

Baidu once, the solution on the Internet is to set the website code, but there is still a garbled problem after this setting, this method failed.

After repeated confirmation, the problem still occurs in the SQL syntax. To display Chinese information, you need to add the letter "N" before the value to be inserted. This solves the problem.

Note: N indicates Unicode, indicating unicode encoding.

For example, the Code is as follows:

View code

 1 string strsql1 = string.Format("insert into T_Blog(UserName,Password,Gender,ReallyName,Brithday,Address,PostCode,Subject,Email,HomePhone,MobiePhone,QQ,Question,Answer,RegTime,IP) values (N'{0}','{1}',N'{2}',N'{3}','{4}',N'{5}','{6}',N'{7}','{8}','{9}','{10}','{11}',N'{12}',N'{13}','{14}','{15}')"
2 , txtUserName.Text.Trim().ToString()
3 , txtRePwd.Text.Trim().ToString()
4 , ddlGender.SelectedValue.ToString()
5 , txtReallyName.Text.Trim().ToString()
6 , txtBrithday.Value.Trim().ToString()
7 , txtAddress.Text.Trim().ToString()
8 , txtPostCode.Text.Trim().ToString()
9 , txtSubject.Text.Trim().ToString()
10 , txtEmail.Text.Trim().ToString()
11 , txtHomePhone.Text.Trim().ToString()
12 , txtMobiePhone.Text.Trim().ToString()
13 , txtQQ.Text.Trim().ToString()
14 , txtQuestion.Text.Trim().ToString()
15 , txtAnswer.Text.Trim().ToString()
16 , DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
17 , Request.UserHostAddress
18 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.