简体   繁体   English

以最有效的方式从SQL Server 2005读取UTF8(XML)数据

[英]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);
    }
}
  1. Don't use XmlTextReader anymore. 不再使用XmlTextReader
  2. Why convert the string to bytes and back to string? 为什么将字符串转换为字节然后又转换回字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C#将UTF8数据插入SQL Server 2005中的图像字段 - Inserting UTF8 data into image field in sql server 2005 from c# 从多个XML文件读取两个节点的最有效方法? - Most efficient way to read two nodes from multiple XML files? 从Xml文件读取的最有效和缓存的方法 - The Most efficient and cached way to read from Xml files 使用SqlDataReader(C#)从SQL Server读取许多字节的最有效方法是什么 - What is the most efficient way to read many bytes from SQL Server using SqlDataReader (C#) 将UTF8数据插入SQL Server 2008 - Insert UTF8 data into a SQL Server 2008 在SQL Server 2005 IMAGE列中存储20 Meg文件的最有效方法 - Most efficient way to store a 20 Meg file in a SQL Server 2005 IMAGE Column 将数据从数据库传输到服务器再传输到客户端的最有效方法是什么? - What is the most efficient way to transfert data from Database to server to client? C#+ SQL Server-最快/最有效的方式将新行读入内存 - C# + SQL Server - Fastest / Most Efficient way to read new rows into memory 从单独的XML文件中读取最有效的组合或连接对象的方法 - Most Efficient Way of Combining or Joining Objects Read From Separate XML Files 存储从套接字读取的数据以供另一个线程读取的最有效方法是什么? - What is the most efficient way of storing data read from a socket to be read by another thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM