具有UTF-8的NVarChar存储在SQL Server 2008中的麻烦 [英] Having trouble with UTF-8 storing in NVarChar in SQL Server 2008

查看:190
本文介绍了具有UTF-8的NVarChar存储在SQL Server 2008中的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 System.Net.WebClient 从网站提取数据,当数据回来的一切分析,看上去除非音符的字母不错。例如,当它返回一个电子时,SQL Server 2008将其保存为

I'm pulling data using System.Net.WebClient from a web site, and when the data comes back everything parses and looks good except letters with accents. For example, when it returns an é, SQL Server 2008 saves it as é.

只是需要弄清楚如何将这些UTF-8字符转换成一些SQL Server可以读取。我将其存储在一个 NVARCHAR(MAX)的数据类型。

Just need to figure out how to convert these UTF-8 characters into something SQL Server can read. I'm storing it in an NVARCHAR(MAX) datatype.

我使用LINQ到SQL插入到数据库中,如果你是好奇。

I'm using Linq-to-SQL to insert into the database if you were curious.

这是我能做到将其转换为正确的格式?

Any thoughts on what I could do to convert it to the proper format?

推荐答案

想通了!当使用WebClient类,我是下载数据为字符串。

Figured it out! When using the WebClient class, I was downloading the data as a string.

我的原始配置...

System.Net.WebClient wc = new WebClient();
string htmlData = wc.DownloadString(myUri);



我试图这个数据转换成UTF-16 ...由于从它的当前的字符串,但微软工作在UTF-16,它已处理了自身的转换。

I tried to convert this data into a UTF-16...from it's current string, but since Microsoft operates in UTF-16, it had handled the conversion on its own.

相反,我打开我的做法,从像这样的数据读取实际byte []数组...

Instead, I switched my approach to reading the actual byte[] array from the data like so...

System.Net.WebClient wc = new WebClient();
string htmlData = UTFConvert(wc.DownloadData(myUri));

private string UTFConvert(byte[] utfBytes)
{
    byte[] isoBytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utfBytes);
    return Encoding.Unicode.GetString(isoBytes);
}

这解决了问题,和SQL正确地看到这一切的口音了。 yippee的。

This fixed the problem, and SQL correctly see's the accents in everything now. Yippee.

干杯一切,感谢您的帮助!

Cheers all, and thanks for your help!

这篇关于具有UTF-8的NVarChar存储在SQL Server 2008中的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆