Mark Segal Mark Segal - 3 months ago 39
C# Question

Unicode shows up incorrectly on Linq-To-Sql

My

ASP.NET
website has a
Linq-To-Sql
database connected with a connection string to an SQL 2008 DB.

Locally, when using an
MDF
file, everything worked fine. I had a "loader" method, looping though files (only once, via admin panel), parsing them and adding to the database. They contain Unicode Hebrew text. I had done this locally. Then, my local website would print out the correct Unicode Hebrew characters to the HTML document. My whole website is in Hebrew.



With the connection string provided by my shared hosting provider, dumping the SQL DB (without data!) to an
sql
, executing on server & loading it up from the files, I start seeing question marks (???) instead of Hebrew characters (the website itself, which is in Hebrew shows up correctly). The SQL database has
text
type fields.



The more funny thing is, that it has been working for some time correctly before showing up question marks (!).



How can I make the Hebrew characters show up correctly? Thank you!


Answer

The text data type in SQL server represents:

Variable-length non-Unicode data in the code page of the server and with a maximum string length of 2^31-1 (2,147,483,647). (emphasis mine)

So, because GoDaddy has a different code page on the server than you do, the unicode chars are getting mangled. text was never meant to contain unicode.
You need to use nvarchar(max) instead.

As DJ Kraze correctly noted, prefixing your strings with N might also be necessary (I thought it might help to add the explanation). But since you are using LINQ to SQL, that's not the problem in your case (good catch @Jon Hanna, I missed that tag).