[英]Read UTF8(XML) Data from SQL Server 2005 the most efficient way
I want to read lots of data(single column nvarchar(max)) from SQL Server 2005 and deserialize it to an object. 我想从SQL Server 2005中读取大量数据(单列nvarchar(max))并将其反序列化为一个对象。 We are currently using the following, but this is not fast enough is there a better/efficient way to do it? 我们目前正在使用以下方法,但这还不够快,是否有更好/有效的方法呢?
using(MemoryStream stream = Encoding.UTF8.GetBytes((string)cmd.ExecuteScalar()))
{
XmlTextReader xmlReader;
DataContractSerializer deserializer;
deserializer = new DataContractSerializer(typeToDeserialize);
xmlReader = new XmlTextReader(stream);
return deserializer.ReadObject(xmlReader);
}
I've als tried to do it with an SqlDataReader and GetBytes but then I get exceptions. 我已经尝试使用SqlDataReader和GetBytes来做到这一点,但随后出现异常。
Thanx in advance. 提前感谢。
I haven't tested this myself, but SqlDataReader with a call to GetSqlBytes seems like a better choice since it exposes a Stream property which you should be able to pass directly to XmlTextReader? 我自己还没有测试过,但是调用GetSqlBytes的SqlDataReader似乎是一个更好的选择,因为它公开了一个Stream属性,您应该可以将该属性直接传递给XmlTextReader?
There's some information on MSDN about GetSqlBytes here . 在MSDN上,有一些关于GetSqlBytes的信息 。
Do you have the option of switching to use the XML
datatype? 您是否可以选择使用XML
数据类型? It stores the data in a binary format, and exposes XML
results as an XmlReader
instance. 它以二进制格式存储数据,并将XML
结果作为XmlReader
实例公开。 You're parsing the data as you read it from the database, but if you used an XML
column, it would already be parsed. 从数据库读取数据时,您正在解析数据,但是如果使用XML
列,则该数据已经被解析。
In the meantime, try something like this: 同时,尝试执行以下操作:
string s;
using (SqlConnection conn = new SqlConnection(""))
{
using (SqlCommand cmd = new SqlCommand("", conn))
{
s = (string) cmd.ExecuteScalar();
}
}
using (StringReader sr = new StringReader(s))
{
using (XmlReader reader = XmlReader.Create(sr))
{
DataContractSerializer deserializer =
new DataContractSerializer(typeToDeserialize);
return deserializer.ReadObject(reader);
}
}
XmlTextReader
anymore. 不再使用XmlTextReader
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.