字符串和 UTF16
String对象中存储的数据总是用UTF16编码吗?
我问这个是因为我的数据库确实以非 Unicode 存储非英语。我认为数据将无法读取,因为它是以错误的编码读取的。
谢谢
Is the data stored in String object always encoded with UTF16?
I am asking this because my database does stores non English in non Unicode. and I assumed that the data will not be readable because it is read in wrong encoding.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
.NET 字符串内部采用 UTF-16 格式,是的...但重要的是数据如何在 .NET 和数据库之间传输。
只要字符可以用 Unicode 表示,并且驱动程序执行适当的转换,就应该没问题。如果您尝试表示无法用 Unicode 表示的文本,您很可能会遇到一些有趣的行为。
Internally .NET strings are in UTF-16, yes... but what's important is how the data is transferred between .NET and your database.
So long as the characters can be represented in Unicode, and the driver performs the appropriate conversion, you should be fine. If you're trying to represent text which can't be represented in Unicode, you may well run into some interesting behaviour.
是的,.NET 字符串始终以 UTF-16 编码 - 除了表示 2 字节字符的代理对之外。
Yes, .NET strings are always encoded in UTF-16 - with the exception of surrogate pairs that means 2 byte characters.
.NET 字符串始终是 Unicode。如果您的数据库是 unicode 就可以了,否则您需要将文本从任何格式转换为 unicode。
.NET Strings are ALWAYS Unicode. If your database is unicode you are fine, otherwise you will need to convert the text from whatever format it is in to unicode.
.NET 中字符(以及字符串)的内部存储是以 UTF-16 格式完成的。
您需要将字符串重新编码为数据库使用的编码。
请参阅
Encoding
类- 这是您可以用来将字符串从一种编码转换为另一种编码的方法。The internal storage of characters (and therefore strings) in .NET is done in UTF-16.
You will need to re-encode the string to the encoding used by your database.
See the
Encoding
class - this is what you can use to convert a string from one encoding to another.如果您将 ADO.NET 与 SqlDataCommands(或其他类型的 DataCommands)一起使用,则应该为您处理任何所需的转换,并且您无需担心它。
If you are using ADO.NET with SqlDataCommands (or other types of DataCommands), any required conversion should be handled for you, and you won't need to worry about it.