Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am inserting some unicode characters such as Chinese characters using a c# program into a SQL Server 2012 database.

The column I am trying to insert is nvarchar, but am still getting ??????? instead of the correct characters. I am getting tweets from twitter and inserting it into a database.

I am using SQL Server 2012 Express. Must I install any language packages or change the settings? I am not really familiar with SQL Server.

This is how i use c# to insert the codes into database:

String command4 = "INSERT INTO  tweets ([username], [createdDate], [tweet]) Values ('" + item.user.screen_name + "' , '" + item.created_at + "' ,N'" + escapedString + "')";

SqlCommand insertTweet = new SqlCommand(command4, connectionstring);

insertTweet.ExecuteNonQuery();

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
918 views
Welcome To Ask or Share your Answers For Others

1 Answer

When passing the characters as SQL strings, always precede with an "N":

N'Unicode'

instead of just

'Unicode'

Or in your case:

@"INSERT INTO tweets ([username], [createdDate], [tweet]) 
Values (N'" + item.user.screen_name + "' , '" + item.created_at + "' , N'" + escapedString + "')"

By the way, you should use parameters. It would solve many problems at once (like the one you have, date and number formats, security...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...