Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 764
Phantom Avatar asked May 25 '26 00:05

Phantom


1 Answers

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

like image 137
Stefan Steinegger Avatar answered May 26 '26 13:05

Stefan Steinegger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!