3

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!

Phantom
  • 119
  • 2
  • 11
  • You don't need anything more - SQL Server is perfectly capable of handling Unicode. Please **show us HOW** you insert your data from C# – marc_s Jul 05 '12 at 10:29
  • http://msdn.microsoft.com/en-us/library/ms143726 – Raphaël Althaus Jul 05 '12 at 10:31
  • @marc_s sorry, this is how i insert the data from c# : String command4 = "INSERT INTO tweets ([username], [createdDate], [tweet]) Values ('" + item.user.screen_name + "' , '" + item.created_at + "' , '" + escapedString + "')"; SqlCommand insertTweet = new SqlCommand(command4, connectionstring); insertTweet.ExecuteNonQuery(); – Phantom Jul 05 '12 at 11:02
  • Also show how you're retrieving it that displays ??????? – MartW Jul 05 '12 at 11:03
  • @CodeByMoonlight I execute the statement Select * from tweets to execute in the sql express and then am able to see the table – Phantom Jul 05 '12 at 11:05
  • Please **do not** put code samples or sample data into comments - since you cannot format it, it's **extremely hard** to read it.... Instead: **update** your question by editing it to provide that additional information! Thank you. – marc_s Jul 05 '12 at 11:52
  • Whoops..sorry not really familiar :( I will update now – Phantom Jul 05 '12 at 12:26

1 Answers1

3

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

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193