Skip to content

How to insert unicode data in sql using c#

Ankush

Asked Mar 14, 2015, 2:52 AM EDT1 answer
Share
LinkedInX
 I am developing a windows application in which the input is in marathi language. I have the code to insert data in sql server.
Code
create database Test
Code
create table marathi(Names nvarchar(22))
Code
insert marathi SELECT N'???????'     ---- Right Query
Code
insert marathi SELECT '???????'         ---- Wrong Query
Code
select *from marathi
just check once the above query in sql, you will understand it. If u write the query using N prefix then in sql it shows the data properly, but if don't use N prefix then it shows ??????? in sql server.
But now my problem is that I am trying to do same from my window application. I wrote the following code.
Code
SqlCommand slc = con.CreateCommand();
Code
slc.CommandText = "Insert into marathi values(@Names)"
Code
slc.Parameters.Add("@Names", SqlDbType.NVarChar, 22);
Code
slc.Parameters["@Names"].Value = textBox1.Text;
Code
slc.ExecuteNonQuery();
How can I achieve this? Please Help....

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Afzaal Ahmad Zeeshan
    0
    Unicode data is supported in .NET framework, and all applications supported or shipped with .NET framework; SQL Server is alongwith it. The actual problem is the encoding of your data, while inserting it, storing it and then decoding it back in your application. You should always consider using UTF-8 encoding to store the data. It is a variable-length data, and would be supported in all of the applications (if they support Unicode).


    A similar article was written by me, which covers SQL Server databases too. You can read that article here. Hopefully that article would clarify the basic concept of Unicode character encodings in .NET framework. 
    0