SQL UPDATE doesn't work with foreign languages

2019-08-10 06:46发布

问题:

the UPDATE gives ???? if the updater field was written in Arabic and this is my query:

UPDATE  students 
SET first_name = 'الاسم' , last_name = 'الاسم الاخير' , 
    father_name = 'الاسم الاخير' , mother_name = '', 
    birth_date = '1/1/1990 12:00:00 AM' , education_level = '' , 
    address = '' , notes = '' 
WHERE student_id = 33

And here is the result of the update:

student_id  first_name  last_name   mother_name     father_name   birth_date    
33           ?????      ?????          ??????       ???????????   1990-01-01

//the answer is great and thank you people, another question is that I am using this UPDATE syntax in my C# program

command.CommandText = "UPDATE  students SET " +
        "first_name = " + "'" + first_name + "'" + " , last_name = " + "'" + last_name + "'" +
         " , father_name = " + "'" + father_name + "'" + " , mother_name = " + 
        "'" + mother_name + "'" + ", birth_date = " + "'" + birth_date + "'" +
        " , education_level = " + "'" + education_level + "'"  +
        " , address = " + "'" + address + "'" + " , notes = " + "'" + notes + "'" +
        " WHERE student_id = " + id ;

//how to use the character N

回答1:

You have forgotten the N prefix before your string literals which is required so they will be treated as nvarchar rather than varchar

SET first_name = N'الاسم' etc.

without that the text is coerced into whatever characters the code page of your default collation can deal with.



回答2:

Create the database with this collation Arabic_CI_AS, you won't need to put N before the Arabic characters.