Insert or update 'űő' into a table

Insert or update 'űő' into a table

Postby zsolt » Fri 05 Jul 2013 14:33

Hello,

I have used dotConnect for Oracle.

I have oracle database (11g) with NLS_NCHAR_CHARACTERSET = AL16UTF16 settings.

I use this test code in PL/Sql:
Code: Select all
create table a (b nvarchar2(1000));
insert into a values('űő');
select * from a;


'ű' and 'ő' characters appear correctly.

But ¿¿ appear instead of 'ű' and 'ő', if I insert data to the table with dotConnector.

Test code:
Code: Select all
string sConnectionString = "User Id=<user>;Password=<pwd>;Server=<server>;Connection Timeout=30;Max Pool Size=150;Direct=True;Service Name=<srvname>;Unicode=true;";
OracleConnection connection = new OracleConnection(sConnectionString);
connection.Unicode = true;

connection.Open();

OracleCommand cmd = connection.CreateCommand();
cmd.CommandText = "insert into a (b) values (:data)";
cmd.Parameters.AddWithValue(":data", "űő");
cmd.ExecuteNonQuery();

OracleCommand cmd2 = new OracleCommand("select * from a", connection);
var read = cmd2.ExecuteReader();
while (read.Read())
   Console.WriteLine(String.Format("{0}", read[0]));
read.Close();
connection.Close();


Is this bug or did I make some mistakes?

I tried it at windows 7 x64 and debian linux too and it worked similarly.

Thanks
zsolt
 
Posts: 2
Joined: Fri 05 Jul 2013 14:22

Re: Insert or update 'űő' into a table

Postby Pinturiccio » Mon 08 Jul 2013 13:39

You are using the AddWithValue method for adding a parameter. But you haven't specified the type of the parameter. Thus, the type is defined automatically based on the type of the value. As the parameter value has the String type, the VarChar value is assigned to the property of the OracleDbType parameter. To pass a Unicode value to the database, you should manually specify the OracleDbType property value for your parameter. For this, add one line to your code after the line with the AddWithValue method:
Code: Select all
OracleParameter par = cmd.Parameters.AddWithValue(":data", "űő");
par.OracleDbType = OracleDbType.NVarChar;

Now a Unicode value will be added to the database.

The following line will output a non-Unicode value:
Code: Select all
Console.WriteLine(read[0]);

The fact is that the OutputEncoding property value of the Console class is equal to ASCII by default, so Unicode characters will not be displayed. To make sure that the Unicode value is read, you can read it, for example, to a file and replace part of the code:

Code: Select all
while(read.Read())
Console.WriteLine(read[0]);

with

Code: Select all
while (read.Read())
System.IO.File.AppendAllText("C:\\unicode.txt",read.GetString(0) + Environment.NewLine);
Pinturiccio
Devart Team
 
Posts: 1933
Joined: Wed 02 Nov 2011 09:44

Re: Insert or update 'űő' into a table

Postby zsolt » Tue 09 Jul 2013 13:55

It works.
Thank you very much.
zsolt
 
Posts: 2
Joined: Fri 05 Jul 2013 14:22


Return to dotConnect for Oracle