Storing Unicode Data in Databases
This post is in continuation to my earlier post VB Forms in Hindi Unicode Fonts following a few comments regarding improper display of stored data.
Let’s first use an MS Access Database (referred in code as D:\uni.mdb), containing a single table “unitable” which itself contains only one text field “unistring”.
The code for storing data to tables and retrieving them are referred from Database - How can I add a record to a database? and Database - How can I fill a combobox/Listbox with values in a database?, which may be referred for more info on relevant task.

As visible above, the form contains a text box (’txtNewString’) for user Input, a button (’cmdAdd’) labelled ‘Add’, on clicking which the text from the textbox is inserted into the table.
Another button (’cmdRetrieve’) labelled ‘Retrieve’, on press, retrieves all the data from the table to populate the listbox (’lstRetrievedData’).
As also discussed in VB Forms in Hindi Unicode Fonts, these controls should be capable of accepting and displaying Unicode Data, which the default VB controls are not capable of. However, corresponding controls available with “Microsoft Forms 2.0 Object Library” allow unicode data for which just right-click the toolbox, click on ‘components…’ and check “Microsoft Forms 2.0 Object Library”. Now use these controls instead.
The code below demonstrate adding and retrieving data from table on press of relevant buttons. Go through the code… nothing new!! all that you would have written for English based forms…
Private Sub cmdAdd_Click()
Dim oADOCon As ADODB.Connection
Dim strQuery As String
Set oADOCon = New ADODB.Connection
oADOCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\uni.mdb;User ID=;Password=;"
strQuery = "insert into unitable values('" & Form1.txtNewString & "')"
oADOCon.Execute strQuery
txtNewString = ""
Set oADOCon = Nothing
End Sub
Private Sub cmdRetrieve_Click()
Dim oADOCon As ADODB.Connection
Dim oADORS As ADODB.Recordset
Dim strQuery As String
Set oADOCon = New ADODB.Connection
Set oADORS = New ADODB.Recordset
oADOCon.CursorLocation = adUseClient
oADOCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\uni.mdb;User ID=;Password=;"
strQuery = "SELECT unistring FROM unitable"
oADORS.Open strQuery, oADOCon, adOpenForwardOnly, adLockReadOnly, adCmdText
lstRetrievedData.Clear
Do While Not oADORS.EOF
lstRetrievedData.AddItem oADORS.Fields("unistring")
oADORS.MoveNext
Loop
Set oADOCon = Nothing
Set oADORS = Nothing
End Sub
Work’s fine! Now let’s try the above code with an MS-SQL database… Create a similar table in an existing database or to a new database (I used existing Northwind database) with a single text field.
The code should normally work by making changes to line(s) below.
oADOCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\uni.mdb;User ID=;Password=;"
oADOCon.Open "DSN=YourDSN;database=Northwind;uid=user;pwd=pwd"
But it doesn’t and the text is saved as ?????? and retrieved as such, since the data types char, varchar, text are designed to accept only 8-bit characters and not the unicode ones. Data types nchar, nvarchar and ntext corresponding to the previously mentioned data types do however allow unicode characters, so we will have to ensure that the data type of the single field in our table is changed accordingly. ‘n’ in these data types is elaborated by MySQL as ‘National’.
That’s not all!, Now that we have made arrangents for correct storage at the database level, we also need to ensure that the data sent by us is recognized by the database as Unicode (or National, as described by MySQL). For this we need to change the insert script by adding a single character ‘N’ just before the unicode string as :
strQuery = "insert into unitable values('" & Form1.txtNewString & "')"
strQuery = "insert into unitable values(N'" & Form1.txtNewString & "')"
That’s all. By now you can create database Apps for storing hindi text using MS Access, MS-SQL (both tested) and MySQL (not tested, but documentation seems to support my belief). You can also try other SQL servers also. I would later sometime test this with Oracle and if any issue found, would post it.

Excellent Sample!! Great Job!!!Thanks very much!!! I shall refere this article to many.
Thanks very Much once again!!!
Comment by philip — April 13, 2007 @ 5:44 am
Hello
Well Im developing software for Middle East(Arabic). Im using VB 6.0, the requirement is some fields (textbox) are of english and some are ment for arabic…on the same form, I want to change the locale(input) automatically when it comes to Arabic Field and vice verse.
How can I do it??
If you feel this question is out of topic to this article kindly move it!!(sorry!!) but please get me a word by my email.
regards
phil
Comment by phil — April 13, 2007 @ 1:25 pm
Hi Phil,
There is nothing such as out of topic… your requirement is genuine… we do/may get such requirements… I will revert asap
Comment by Jalaj — April 16, 2007 @ 5:05 am
[...] : This post is in response to comment by Phil on the post Storing Unicode Data in Databases. Got any such problem please post on any relevant [...]
Pingback by Switching IME during runtime « Jalaj — April 16, 2007 @ 6:39 am
Excellent article. But whenever I tried to save the unicode data in oracle DB, it saves as ??? while for access DB and SQL server, it works fine. Could you pl give any solution why it is saving ??? instead of actual unicode data
Comment by Shailendra — May 23, 2007 @ 6:04 am
Very Informative Information.
As Shaliendra raises the question that when we are trying to do the same in oracle database, it is storing as ???. The Field datatype in the database in NVARCHAR. Could you pl give any solution why it is like this.
Comment by Amit Pandey — May 28, 2007 @ 6:51 am
@Amit & Shailendra,
You may refer http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96529/ch6.htm to check what you are missing out.
The link above confirms “N” added before the field data works as it did with MS-SQL and MySQL. There is also a TO_NCHAR Function that you may try using.
The above link is a good source of relevant information. Hope it could help you.
Comment by Jalaj — May 29, 2007 @ 4:30 am
how to insert unicode data using c#. pls provide c# code
Comment by sadashiv — June 11, 2007 @ 7:45 pm
[...] The Blog Revisited - 7 Databases allow datatypes that can store unicode data, then why was it that while MS-Access successfully stored Unicode data whereas MS-SQL server failed when I first tried. See how to handle Unicode data in Storing Unicode Data in Databases. [...]
Pingback by The Blog Revisited - 7 « Jalaj — November 16, 2007 @ 4:46 am
PHP and Mysql
insert into table1 values(1,N’bc’);
successfully insert into the table1 but the problem is value insert using N cannot be retrieve correctly using PHP and Mysql
Comment by Rasin — December 6, 2007 @ 7:44 am
Wonderful..Thanks for the solution
Comment by Saradaga.com — January 5, 2008 @ 10:41 am
This is good. But how can we input hindi characters in the textbox. Is there any way ?
Comment by Robs — January 13, 2008 @ 3:10 pm
gdeg
Comment by ghyy — March 16, 2008 @ 6:30 am
Thanks a lot!
Very clear and useful.
Comment by Andrei — April 9, 2008 @ 7:06 pm
When I try to insert unicode text in MySQL database using c# still ????? inserted into database. I try by using prefix N but still not working.
Comment by jayawant — May 12, 2008 @ 5:07 am