I have a UNICODE string (chinese) which I want to convert back to MBCS so as to add it as a parameter to an SQL query. (the column in SQL Server in varchar and so this conversion is necessary for me).
How can I convert to MBCS in c#? Please help.
Thanks, Praseo
“MBCS” could be a number of encodings. For the China locale, it would be code page 936, a GB-variant, and you could encode to its bytes using:
If you aren't specifically talking about GB encoding and want whatever multibyte encoding is the default for your current system, then you can just use the Default (“ANSI”) encoding which will be that MBCS encoding:
Now to get your byte array into the query you'll have to either:
1) best, use a parameter with a byte-based SqlDbType, eg:
2) orif you have to insert it directly into the query string, encode the bytes as a hex literal, eg:
(this is non-ANSI-standard SQL Server syntax)