使用XmlReader迭代代替类型化DataSet和IDataReader解析关联数据

类别:.NET开发 点击:0 评论:0 推荐:
正在修改的第一个电子购物程序中需要处理购物订单和订单明细的读取/更新问题. 他们在数据库中分成两个表存储,Order和OrderDetails.现在要把他们的数据读取出来,并创建一个Order的数据实体供程序使用. 一开始的时候我考虑使用IDataReader读取数据,即打开两个数据库连接,一个用来读取Order,一个用来读取OrderDetails.后来感觉麻烦, 就开始考虑使用xml shema创建一个数据集,然后利用SqlServer的xml功能返回一个XmlReader,读取到这个类型化的DataSet中,但是因为 涉及到DataSet,担心性能的影响,写了一个小程序来测试其性能.调试过程中,缺发现,最快的方式应该是自己使用XmlReader迭代返回的Xml 字符串.程序如下: 首先在SqlServer中使用for xml的Sql语句生成Xml: ALTER PROCEDURE Shop_GetOrders2 ( @BlogID bigint ) AS SELECT 1 as Tag, NULL as Parent, O.OrderID as [Order!1!OrderID], O.userid as [Order!1!userid], O.useremail as [Order!1!useremail], O.UserMobile as [Order!1!UserMobile], O.userim as [Order!1!userim], O.receipt as [Order!1!receipt], O.city as [Order!1!city], O.address as [Order!1!address], O.postcode as [Order!1!postcode], O.usertel as [Order!1!usertel], O.deliverymethod as [Order!1!deliverymethod], O.deliverfee as [Order!1!deliverfee], O.paymethod as [Order!1!paymethod], O.realname as [Order!1!realname], case O.vip when 1 then 'true' when 0 then 'false' end as [Order!1!vip], O.BlogID as [Order!1!BlogID], O.UserTitle as [Order!1!UserTitle], O.InsertDate as [Order!1!InsertDate], NULL as [Detail!2!ProductID], NULL as [Detail!2!UnitPrice], NULL as [Detail!2!UnitScore], NULL as [Detail!2!Number] FROM Orders O WHERE O.BlogID = @BlogID UNION ALL SELECT 2 as Tag, 1 as Parent, O.OrderID as [Order!1!OrderID], NULL as [Order!1!userid], NULL as [Order!1!useremail], NULL as [Order!1!UserMobile], NULL as [Order!1!userim], NULL as [Order!1!receipt], NULL as [Order!1!city], NULL as [Order!1!address], NULL as [Order!1!postcode], NULL as [Order!1!usertel], NULL as [Order!1!deliverymethod], NULL as [Order!1!deliverfee], NULL as [Order!1!paymethod], NULL as [Order!1!realname], NULL as [Order!1!vip], NULL as [Order!1!BlogID], NULL as [Order!1!UserTitle], NULL as [Order!1!InsertDate], OD.ProductID as [Detail!2!ProductID], OD.UnitPrice as [Detail!2!UnitPrice], OD.UnitScore as [Detail!2!UnitScore], OD.Number as [Detail!2!Number] FROM Orders O, OrderDetails OD WHERE O.OrderID = OD.OrderID and O.BlogID = @BlogID FOR XML EXPLICIT 下面的程序则解析内容: [STAThread] static void Main(string[] args) { for(int i=0;i

本文地址:http://com.8s8s.com/it/it41909.htm