1

I am using SQL Server 2012 Express as part of my asp.net development workstation. Database and table collation is set to Latin1_General_CI_AI. Attribute types are nvarchar(max) and varchar(max).

When I pass a string containing Serbian Latin characters žđšćč žđćčžđšćčŽĐŠĆČ, the model from my asp.net application passes the string as it is, but the database is not accepting it, writing ždšcc ždccždšccŽÐŠCC into the table instead.

I also tried with Cyrillic string, but ????? ????? is written into database.

I am using MVC5 and EF6. I don't have parameters or datareader. I am just passing the model into the framework, and EF is doing everything else.

3

You have to specify that you are inserting wide character data:

CREATE TABLE #t (id INT,c1 VARCHAR(MAX),c2 NVARCHAR(MAX));
INSERT INTO #t VALUES(1,'žđšćč žđćčžđšćčŽĐŠĆČ','žđšćč žđćčžđšćčŽĐŠĆČ');
INSERT INTO #t VALUES(2,N'žđšćč žđćčžđšćčŽĐŠĆČ',N'žđšćč žđćčžđšćčŽĐŠĆČ');
SELECT * FROM #t;
DROP TABLE #t;

Result:

Only column c2 with id=2 is correct

  • Any idea how to do that in asp.net model? – onedevteam.com Apr 14 '14 at 14:41
  • Explicitly state it as NVARCHAR parameter, as demonstrated in this question. – Twinkles Apr 14 '14 at 14:49
  • that would work in regular asp.net, but i'm using MVC5 and EF6, and i don't have parameters or datareader. I'm just passing model to framework, and EF is doing everything else. – onedevteam.com Apr 14 '14 at 15:21
2

I don't think collation is your problem as that is related to sorting and ordering (although you might get a problem later using ORDER BY and so on), but you mention you have some VARCHAR fields. Those will not accept Unicode characters (which Cyrillic certainly are): They will need to be NVARCHAR throughout to do that.

Next, how are you inserting your data? Take a look at this SQLFiddle example. Note that the Serbian latin characters work in both VARCHAR and NVARCHAR columns (an explanation of why can be found in this Stack Overflow post), but the Cyrillic characters do not. Also note that the strings are prefixed with an N which identifies that string as containing National characters (2-byte Unicode, to all intents and purposes).

You need to ensure that you declare your columns correctly and that strings are prefixed with N and if you use parameters or variables they too must be of the correct type. If you wish to deal with anything other than standard Latin characters it is generally easier to use NVARCHAR as a matter of course. Yes, each character requires 2 bytes instead of 1 for storage but that's the price that must be paid to store Unicode data.

0

č is not part of Latin 1. You will need to use either ISO-8859-2 or Windows-1250 or start using Unicode at all (if applyable the Unicode-version is the future safe version). In oyur case you might have to reconfigure you database here.

Maybe just a link https://en.wikipedia.org/wiki/Latin-1

  • but how to set sql server collation to unicode? on msdn, recommended collation for serbian latin is Latin1_General_CI_AI – onedevteam.com Apr 14 '14 at 14:29
  • 1
    AFAIU it's the default, not the recommended. – frlan Apr 14 '14 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.