2

When trying to save a string into my DB table, they get saved as question marks.

I have already checked out this question (and many more): Why is sql server storing question mark characters instead of Japanese characters in NVarchar fields?

Unfortunately, nothing wielded any result for me. I have had the Collation set to "SQL_Latin1_General_CP1_CI_AS" at the start, but I changed it to "Japanese_CI_AS" later on. Still no change.

My code looks similar to this (sorry for not having the real code, it's on my laptop):

string query = "INSERT INTO ArticlesTBL (ArticleTitle, ArticleContent, 

ArticleType, ArticleImg, ArticleBrief,  ArticleDateTime, ArticleAuthor, ArticlePublished, ArticleHomeDisplay, ArticleViews)";
query += " VALUES (@ArticleTitle, @ArticleContent, @ArticleType, @ArticleImg, @ArticleBrief, @ArticleDateTime, @ArticleAuthor, @ArticlePublished, @ArticleHomeDisplay, @ArticleViews)";

SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@ArticleTitle", ArticleTitleTextBox.Text);
myCommand.Parameters.AddWithValue("@ArticleContent", ArticleContentTextBox.Text);
// ... other parameters
myCommand.ExecuteNonQuery();

ArticleContent would be the string with the japanese characters.

My database was created on a MS SQL Server 2014 and I'm using Visual Studio 2015 to write this small application.

Community
  • 1
  • 1
Kohnarik
  • 377
  • 2
  • 5
  • 19
  • What is the data type of the columns? – Patrick Hofman Dec 01 '15 at 07:38
  • 1
    follow this cmd.Parameters.Add(new SqlParameter("@ArticleContent", SqlDbType.NVarChar)).Value = ArticleContentTextBox.Text – dawncode Dec 01 '15 at 07:39
  • @PatrickHofman Sorry. The datatype of the columns are VarChar. Using NVarChar would solve this problem? – Kohnarik Dec 01 '15 at 07:42
  • @dawncode Thank you. I will try it out when I have access to my laptop again (today evening). – Kohnarik Dec 01 '15 at 07:43
  • Another thing to bear in mind - changing the collation on the database does not change the collation on individual columns, unless they were set to the default collation. It would be worth double checking the columns in your tables to make sure you have consitent collation all the way through. This has bitten me before! – BeanFrog Dec 01 '15 at 08:46
  • @BeanFrog Thank you very much for that valuable tip! I'll make sure to set everything correctly. – Kohnarik Dec 01 '15 at 09:05

1 Answers1

3

You have to use the Unicode supporting nvarchar data types. According to MSDN:

If you store character data that reflects multiple languages, always use Unicode data types (nchar, nvarchar, and ntext) instead of the non-Unicode data types (char, varchar, and text).

If you want to use the full UTF-8 code page, you need to use the nvarchar data type.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I see. Thank you very much! I will try it out and approve your answer as the correct answer if it works. – Kohnarik Dec 01 '15 at 07:44