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

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

database - Unicode in SQL Server 2012 Express

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...