简体   繁体   中英

SQL Server and Chinese character on INSERT/SELECT query

I use SQL Server 2014 with collation SQL_Latin1_General_CP1_CI_AS .

I have a C# program that inserts Chinese character into my database, for example :

"你","好".

In SQL Server Management Studio, I can see it clearly and I can also search on it through N"你".

The issue is that for some character, it didn't work :

"〇", "㐄". 

When my C# program start to insert this two characters, I have a CONSTRAINT UNIQUE exception raised (because I put it into my database a unique constraint for Chinese character).

InnerException = {"Violation of UNIQUE KEY constraint 'AK_Dictionary_Chinese'. Cannot insert duplicate key in object 'dbo.Dictionary'. The duplicate key value is (㐄).\\r\\nThe statement has been terminated."

And here is my issue : it seems that these two Chinese character (and I have around 70 similar issue) are not well converted into UTF8 and I encounter issue. If I remove the unique constraint, then of course I can insert it into my database and can see it through SQL Server Management Studio. But when I search for the character using N"〇", the database answer me multiple matches : "〇", "㐄"...

So how can I deal with that ? I tried to change the collation for the Chinese one but I have the same issue...

Thanks for your help.

HOW can I add the Chinese characters in my c# program?

My entity object :

public partial class Dictionary
{
    public int Id { get; set; }
    public string Chinese { get; set; }
    public string Pinyin { get; set; }
    public string English { get; set; }
}

I just add a new entity object to my database and call SaveChanges();

var word1 = new Dictionary()
{
   Chinese = "〇",
   Pinyin = "a",
   English = "b",
};

var word2 = new Dictionary()
{
   Chinese = "㐄",
   Pinyin = "c",
   English = "bdsqd",
};

 // We insert it into our Db
 using (var ctx = new DBEntities())
 {
   ctx.Dictionaries.Add(word1);
   ctx.Dictionaries.Add(word2);
   ctx.SaveChanges();
 }

If you want to try at home, here is a small sql script that reproduce the issue. You can execute it through SQL MANAGEMENT STUDIO :

DECLARE @T AS TABLE
(
    ID INT IDENTITY PRIMARY KEY,
    Value  NVARCHAR(256)
);  

INSERT INTO @T(Value) VALUES (N'〇'), (N'㐄');

SELECT * FROM @T;


SELECT * FROM @T WHERE Value = N'〇';

I found the answer. In fact, the Chinese character is "traditional" and SQL SERVER need a bit of help. Collation was the right idea, but I had to specify Chinese traditional for that (to find it I just query the sample provided with all possible collation....).

DECLARE @T AS TABLE
(
    ID INT IDENTITY PRIMARY KEY,
    Value  NVARCHAR(256)
) ;  

INSERT INTO @T(Value) VALUES (N'〇'), (N'㐄');

SELECT * FROM @T;

SELECT * FROM @T WHERE Value = N'㐄' COLLATE Chinese_Traditional_Pinyin_100_CI_AS;

Sorry for making lose your time, I really tried with Chinese collation but not the traditional one (I didn't know that it was traditional character...).

Fixed.

Try this without specifying Collation every time!

DECLARE @T AS TABLE
(
    ID INT IDENTITY PRIMARY KEY,
    Value  NVARCHAR(256)
) ;  

INSERT INTO @T(Value) VALUES (N'〇'), (N'㐄'), (N'㐄〇'), (N'〇㐄');

SELECT * FROM @T;

SELECT * FROM @T  WHERE CAST(Value AS varbinary) like CAST(N'〇%' AS varbinary)
SELECT * FROM @T  WHERE CAST(Value AS varbinary) like CAST(N'㐄%' AS varbinary)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM