Thursday, March 29, 2012

Can't store chinese in SQL Database

update Food set FoodName = ' 杏仁' where ID = 100

in database as ?

Hi SuperM,

You should use Encoding support from your .NET Application.

To Encoding:

Encoding target = Encoding.GetEncoding( "GB18030" );
Byte[] buffer = target.GetBytes( text );
return Convert.ToBase64String( buffer );

From Encoding:

Encoding target = Encoding.GetEncoding( "GB18030" );
Byte[] buffer = Convert.FromBase64String( text );
return target.GetString( buffer );

You could use GB18030 to simplified or Big5 to traditional.

Later, value converted you send to store procedure.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||Or set the collation settings in SQL Server. If this is SQL Server 2000, you need to run the rebuildm.exe to change the current collation settings. But this will affect other databases in your server|||

Hi bass,

"But this will affect other databases in your server".

This could be very dangerous.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

SuperM wrote:

update Food set FoodName = ' 杏仁' where ID = 100

in database as ?

I would recommend defining the data column as NVarchar, NChar, or NText, when at all possible when working with non-ascii characters.

Jimmy

|||I know what you mean but that should have been a part of the planning phase so that it would be considered in the deployment. Our applications use different collation settings as they are being used in different countries. So before we even create the databases, the SQL Sever settings have been defined properly.|||

thanks,

the field needs to store chinese which must define as NVarchar, NChar, or NText

The Insert SQL as

insert into Food (FoodName) values (N'杏仁' )

The Update SQL as

update Food set FoodName = N' 杏仁' where ID = 100

No comments:

Post a Comment