Groups | Blog | Home
all groups > dotnet ado.net > april 2004 >

dotnet ado.net : Unicode problem: Can read, but not store Chinese characters in database!


Jon Skeet [C# MVP]
4/30/2004 2:24:46 PM
[quoted text, click to view]

See http://www.pobox.com/~skeet/csharp/debuggingunicode.html for some
hints.

[quoted text, click to view]

Are you sure that type should be OleDbType.Char rather than
OleDbType.WChar?

(In SqlClient.* you'd use SqlDbType.NChar.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Morten
4/30/2004 3:03:18 PM
Hi there!

I'm building a VB.NET application that connects to a MS SQL 2000 Server
through ADO.NET.

NOTE: My application can correctly read and show Chinese characters
retrieved from the database (they are stored in a 'nchar' column).

However, when I try to store some Chinese characters through an INSERT or
UPDATE statement, the database only receives a questionmark ('?') for each
Chinese character.

I have tried through both System.Data.OleDb.* and System.Data.SqlClient.*,
but they both yield the same unsatisfying result.

My code is something like (here with System.Data.OleDb):

------------------------------------------
Dim cn As New System.Data.OleDb.OleDbConnection("Data Source=XXXXX;
Provider=SQLOLEDB; Initial Catalog=XXXXX; User Id=XXXXX; Password=XXXXX;")
Dim cmd As New System.Data.OleDb.OleDbCommand("UPDATE myTable SET
chineseText=? WHERE id=1;", cn)
Dim par As New System.Data.OleDb.OleDbParameter("name",
System.Data.OleDb.OleDbType.Char)
par.Value = myChineseTextBox.Text
cmd.Parameters.Add(par)
cmd.ExecuteNonQuery()
cn.Close()
------------------------------------------

Oh yeah: I'm using the .NET 1.0 framework.

Please help me, since this is driving me nuts. Am I missing something in the
connection string, or should I do something special to my objects, so they
don't drop the Chinese characters?

Cheers,

~Morten ;-)

v-kevy NO[at]SPAM online.microsoft.com
5/3/2004 1:06:34 AM
Thanks for Jon's quick response!

Hi Morten,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you were unable to insert records with
Chinese characters. If there is any misunderstanding, please feel free to
let me know.

Based on the code you have provided, I think the problem has something to
do with the type you specified for the parameter. The type currently used
is System.Data.OleDb.OleDbType.Char. This type doesn't support Unicode. So
please try to use System.Data.OleDb.OleDbType.WChar. If you are using the
Sql provider, please use System.Data.SqlClient.SqlDbType.NChar.

For more information, please check the following links:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdataoledboledbtypeclasstopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatasqldbtypeclasstopic.asp

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Jon Skeet [C# MVP]
5/5/2004 11:40:21 AM
[quoted text, click to view]

Absolutely. That's part of the point of using them. In fact, there's no
guarantee that you'll end up ever having a full SQL statement -
depending on the driver/protocol, the parameters may end up being sent
up as they are, *as* parameters.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Morten
5/5/2004 12:24:24 PM
Hi Kevin & Jon!

Thanks a lot for your responses. -- Sorry for not being back before, but
I've been busy with other [less interesting!] work....

Anyway: yes, it was the column type that was wrong in my code. After
changing it to wchar / nchar it works fine with whistles, horns and
everything.

Just a related question: When submitting INSERT or UPDATE statements to a
database in the "parameter way" (i.e. not writing the actual data values
directly in the statement), can I be sure that my values are correctly
wrapped in quotes (or whatever is needed), so I don't get security flaws
through SQL injections?

Cheers,

~Morten


"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> skrev i en meddelelse
news:exIuqrKMEHA.2364@cpmsftngxa10.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button