sql2005的xml字段类型在.net中的应用

 今天要介绍的就是sql2005的XML字段类型在.net中的应用。调用过程是:先运用并行化的办法把XML字段类型中的数据转换成Model对象,对Model对象操作后,再运用串行化的方法把Model对象转变成XML格式,最后存储到数据库中。


   我认为如果把复杂的业务关系数据存储在XML字段中,可简化数据库的设计,方便业务的处理。
  
   这里写了个小demo:
  
   假如我们有很多店铺信息,每个店铺都有一个ShopID, 所以我们就把同一店铺的信息放在以ShopID命名的文件夹下,当一台服务器放不下时,我们就部署多台,这样每台服务器存储的店铺是不一样的。这些服务器就构成了一个服务器群。出于需要,我们要把这个群复制多个,部署在不同的地区(注意,各个群的信息是相同的)。为了完成这个目的,我们先设计了数据模型 MServerGroup(服务器群信息),MServer(服务器群下的服务器信息),MServerShop(服务器对应的店铺):
/// <summary>
   
/// 服务器群信息
   
/// </summary>
   
/// <remarks>
   
/// 用于存放点播文件服务器群的信息,比如主站的,北京站的,上海站的;各个站的数据相同.
   
/// 服务器群的目的是分散数据库的压力.
   
/// 目前只有主站的.
   
/// </remarks>

   [Serializable()]
   
public class MServerGroup : BaseModelEntity
   
{
   
private
  
   
constructor
  
   
property
   }

  
  
   
/// <summary>
   
/// 服务器群下的服务器信息
   
/// </summary>
   
/// <remarks>
   
/// 用于存放点播文件的服务信息
   
/// </remarks>

   [Serializable()]
   
public class MServer : BaseModelEntity
   
{
   
private
  
   
constructor
  
   
property
   }

   
  
   
/// <summary>
   
/// 服务器对应的店铺
   
/// </summary>
   
/// <remarks>
   
/// 用于存放和服务器对应的店铺
   
/// </remarks>

   [Serializable()]
   [XMLRoot(ElementName 
= "Shop", Namespace = "http://www.linkedu.com.cn/MServerShop.xsd")]
   
public class MServerShop : BaseModelEntity
   
{
   
private
  
   
constructor
  
   
property
   }


为了对模型的集合信息进行描述,我们有设计了MServerGroupCollection(服务器群信息集合),MServer(服务器群下的服务器信息),MServerShopCollection(服务器对应的店铺集合)
 /// <summary>
   
/// 服务器群信息集合
   
/// </summary>
   
/// <remarks>

   [Serializable()]
   [XMLRoot(
"ServerGroups")]
   
public class MServerGroupCollection : List<MServerGroup>
   
{
   
/// <summary>
   
/// 服务器群信息集合
   
/// </summary>

   public MServerGroupCollection()
   
{
   
this._MServerGroups = new List<MServerGroup>();
   }

  
   
private List<MServerGroup> _MServerGroups;
  
   
public List<MServerGroup> MServerGroups
   
{
   
get
   
{
   
return this._MServerGroups;
   }

   
set
   
{
   
this._MServerGroups = value;
   }

   }

   }

  
   
/// <summary>
   
/// 服务器群下的服务器信息集合
   
/// </summary>

   [XMLRoot("Servers")]
   [Serializable()]
   
public class MServerCollection : List<MServer>
   
{
   
/// <summary>
   
/// 服务器群下的服务器信息集合
   
/// </summary>

   public MServerCollection()
   
{
   
this._MServers = new List<MServer>();
   }

  
   
private List<MServer> _MServers;
  
   
public List<MServer> MServers
   
{
   
get
   
{
   
return this._MServers;
   }

   
set
   
{
   
this._MServers = value;
   }

   }

   }

  
   
/// <summary>
   
/// 服务器对应的店铺集合
   
/// </summary>

   [Serializable()]
   [XMLRoot(ElementName 
= "Shops", Namespace = "http://www.linkedu.com.cn/MServerShop.xsd")]
   
public class MServerShopCollection 
   
{
   
private List<MServerShop> _MServerShops;
  
   [XMLElement(
"Shop")]
   
public List<MServerShop> MServerShops
   
{
   
get
   
{
   
return this._MServerShops;
   }

   
set
   
{
   
this._MServerShops = value;
   }

   }

   
/// <summary>
   
/// 服务器对应的店铺集合类
   
/// </summary>

   public MServerShopCollection()
   
{
   
this._MServerShops = new List<MServerShop>();
   }

  
   }


经分析,服务器对应的店铺信息可用XML存储,设计格式如下(用xsd描述,设计好后,我们把它创建到数据库中)
 CREATE XML SCHEMA COLLECTION [dbo].[MServerShop] AS 
  N
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:t="http://www.linkedu.com.cn/MServerShop.xsd" targetNamespace="http://www.linkedu.com.cn/MServerShop.xsd" elementFormDefault="qualified">
  <xsd:element name="Shops">
   <xsd:complexType>
   <xsd:complexContent>
   <xsd:restriction base="xsd:anyType">
   <xsd:sequence>
   <xsd:element name="Shop" type="t:ServerShop" minOccurs="0" maxOccurs="unbounded" />
   </xsd:sequence>
   </xsd:restriction>
   </xsd:complexContent>
   </xsd:complexType>
  </xsd:element>
  <xsd:complexType name="ServerShop">
   <xsd:complexContent>
   <xsd:restriction base="xsd:anyType">
   <xsd:sequence />
   <xsd:attribute name="ShopID" type="xsd:int" use="required" />
   <xsd:attribute name="ShopName" type="xsd:string" use="required" />
   </xsd:restriction>
   </xsd:complexContent>
  </xsd:complexType>
  </xsd:schema>
'
最后,我设计了(服务器群信息 ES_ServerGroup),(服务器群下的服务器信息 ES_Server)的数据表, 在 ES_Server 数据表中,我们把服务器对应的店铺信息放在ES_Server数据表下用XML表示,并加入上边设计的xsd约束。
CREATE TABLE [dbo].[ES_ServerGroup](
   
[ServerGroupID] [int] NOT NULL,
   
[ServerGroupName] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
   
CONSTRAINT [PK_ES_SERVERGROUP] PRIMARY KEY CLUSTERED 
  (
   
[ServerGroupID] ASC
  )
WITH (IGNORE_DUP_KEY = OFFON [PRIMARY]
  ) 
ON [PRIMARY]
  
  
CREATE TABLE [dbo].[ES_Server](
   
[ServerID] [int] NOT NULL,
   
[ServerGroupID] [int] NULL,
   
[ServerName] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
   
[IP] [nvarchar](15) COLLATE Chinese_PRC_CI_AS NULL,
   
[DomainName] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NULL,
   
[Dir] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
   
[Url] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
   
[ServerShops] [XML](CONTENT [dbo].[MServerShop]NULL,
   
CONSTRAINT [PK_ES_SERVER] PRIMARY KEY CLUSTERED 
  (
   
[ServerID] ASC
  )
WITH (IGNORE_DUP_KEY = OFFON [PRIMARY]
  ) 
ON [PRIMARY]
 下一步,我开始设计数据访问接口,然后设计数据访问层和业务层,最后设计表现层。
  为了演示方便,Demo中我省去了接口的书写和业务层,我在表现层直接调用了数据访问层。数据访问层代码如下:
 /// <summary>
   
/// 服务器群下的服务器信息数据访问层
   
/// </summary>
   
public class DServer
   {
   #region constructor
   
public DServer()
   {
   }
   #endregion
  
   #region 
public method
  
   #region 得到当前
  
   #region 得到某服务器信息 MServer GetMServer(
int _ServerID)
   
/// <summary>
   
/// 得到某服务器信息
   
/// </summary>
   
/// <param name="_ServerID">服务器的ServerID</param>
   
/// <returns>得到某服务器信息</returns>
   
public MServer GetMServer(int _ServerID)
   {
   DataProvider dp 
= SqlTools.HelpWWW.DataProviderUse;
   using (IDbConnection conn 
= dp.GetConnection())
   {
   Common.DataAccess.ORMapping.IConvert ic 
= dp.GetIConvert();
   Common.DataAccess.ORMapping.Mapping m 
= ic.GetNewMapping();
   m.AddSql("
select * from ES_Server where ");
   m.AddSql(" ServerID
=");
   m.AddSql("ServerID", _ServerID);
   using (IDataReader idr 
= m.ExecuteReader(conn))
   {
   
if (idr.Read())
   {
   
return new MServer(_ServerID, idr["ServerName"].ToString(), idr["IP"].ToString(), idr["DomainName"].ToString(), idr["Dir"].ToString(), idr["Url"].ToString(), (int)idr["ServerGroupID"], idr["ServerShops"].ToString());
   }
   }
   
return null;
   }
   }
   #endregion
  
   #region 关于MServerShop的操作
  
   #region 得到服务器对应的店铺集合 MServerShopCollection GetMServerShop(
int _ServerID)
   
/// <summary>
   
/// 得到服务器对应的店铺集合
   
/// </summary>
   
/// <param name="_ServerID">服务器的ServerID</param>
   
/// <returns>得到服务器对应的店铺数组</returns>
   
public MServerShopCollection GetMServerShop(int _ServerID)
   {
   DataProvider dp 
= SqlTools.HelpWWW.DataProviderUse;
   using (IDbConnection conn 
= dp.GetConnection())
   {
   Common.DataAccess.ORMapping.IConvert ic 
= dp.GetIConvert();
   Common.DataAccess.ORMapping.Mapping m 
= ic.GetNewMapping();
   m.AddSql("
select ServerShops from ES_Server where ");
   m.AddSql(" ServerID
=");
   m.AddSql("ServerID", _ServerID);
   string XMLstr 
= m.ExecuteScalar(conn).ToString();
   
return Common.Utilities.SerializationHelper<MServerShopCollection>.FromXML(XMLstr);
   }
   }
   #endregion

  
 
posted @ 2007-12-27 13:14  LutzMark  阅读(273)  评论(0编辑  收藏  举报