0

I have a problem that when I am trying to insert a Japanese word to my database. It insert them, but when I check the word, it looks like this: "??????"

So I explored in stackoverflow and tried many solution but failed and I still have same problem. Here is my code if someone can help:

Here is the word I enter into a text box:

ケンガンアシュラ

Database shows it as:

????????????

Model:

 [Display(Name = "Alternative Name")]
 [StringLength(200)]
 public string Alternative_Name { get; set; }

At SQL Server it has a datatype of

nvarchar(200)

Here is my view:

<div class="form-group">
    <h4>@Html.LabelFor(model => model.Alternative_Name, new { @class = "control-label col-md-3" })</h4>
    <div class="col-md-6">
        @Html.EditorFor(model => model.Alternative_Name, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Alternative_Name, "", new { @class = "text-danger" })
    </div>
</div>

I tried to update it using migration and changed Unicode to true:

  AlterColumn("dbo.Post", "Alternative_Name", c => c.String(maxLength: 200, unicode: true));

But nothing works, the database keeps showing it as ?????.

UPDATE: In my case the solution simply in the model builder or model class which explain the relation btw tables and attributes the Unicode was set to false so I changed it to true and solved:

 modelBuilder.Entity<Post>()
            .Property(e => e.Alternative_Name)
            .IsUnicode(true);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dummies EBooks
  • 175
  • 3
  • 12
  • 1
    Whatever is passing that string to the database needs to prefix the string with an N. Compare `SELECT 'ケンガンアシュラ', N'ケンガンアシュラ';` Does this thing generate a SQL command at some point? What does it look like? Can you intercept it and edit it? Maybe you should be calling a stored procedure instead of letting this thing generate ad hoc SQL. – Aaron Bertrand Oct 18 '18 at 17:19
  • Prefix values with N while inserting. INSERT INTO INFORMATIONS(SOME_TEXT) VALUES(N'your chars') – user4321 Oct 18 '18 at 17:20
  • I'm using Linq and simply i don't handle the value when i add to db since i'm using asp mvc entity so i just add the model to db with all the values once – Dummies EBooks Oct 18 '18 at 17:26
  • i think this link is helpful for you https://stackoverflow.com/questions/12632240/entityframework-update-or-insert-chinese-or-non-english-text – Manish Singh Oct 26 '18 at 13:17

3 Answers3

1

String in .Net are by default unicode which should pass the Japanese to the DB as NVarchar

Use data annotations on your model to tell.net what the type is using Column(TypeName ="NVarchar")]

Column(TypeName ="NVarchar")]
[Display(Name = "Alternative Name")]
 [StringLength(200)]
 public string Alternative_Name { get; set; }
China Syndrome
  • 953
  • 12
  • 24
0

In The SQL Server DataBase you should be at the column that you want to insert data in it set type as NVARCHAR(3000).

MBARK T3STO
  • 329
  • 1
  • 11
-1

The column should be in nvarchar

create table yourTableName(yourColumnName nvarchar(50));

INSERT INTO yourTableName(yourColumnName) VALUES(N'ケンガンアシュラ');
user4321
  • 605
  • 3
  • 14
  • 37