Wednesday, 31 January 2018

Multilingual .NET Application using SQL Server

I was tasked with a project and a very small portion of it was to insert Multilingual Characters into the SQL Server database using C# .NET

Although it’s a simple enough task once I googled and got information from here and there.
Here is my compilation of Step by Step guide, Hope this is helpful.

First Let’s Setup the Database table


SQL Query for creating Table 

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tbl_MultiLang](
    [Lang] [nvarchar](50) NULL,
    [Data] [nvarchar](50) NULL
) ON [PRIMARY]

GO

Let's setup a simple table with two columns, First is "Lang" of nvarchar that will have only English characters  and the second Data of nvarchar this will host Multilingual characters.



SQL Queries
Queries for Insert into table 
Key here is having "N" before inserting the Multilingual character or string

INSERT INTO tbl_MultiLang (Lang, Data) values(N'English',N'Hello')
INSERT INTO tbl_MultiLang (Lang, Data) values(N'Urdu',N'ہیلو')



SQL Output
Queries for Insert into table



C# Code
Following is the C# Code, illustrating the INSERT statement from the C# Application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
private void button1_Click_1(object sender, EventArgs e)
{
            SqlConnection conn = new SqlConnection("server=****;database=testdb;uid=****;password=****");
            SqlCommand cmd;
            try
            {
                conn.Open();
                cmd = new SqlCommand("INSERT INTO tbl_MultiLang (Lang, Data) values(N'"+ tboxLang.Text +"',N'"+ tboxData.Text +"')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error ", ex.Message);
            }
}



I haven't shown any read-back C# code from DB as it is straight-forward.

I found this article particularly interesting and also my Reference
http://installurdu.blogspot.sg/p/insert-urdu-characters-in-sql-server.html