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 );