设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

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

作者:佚名 出处:网络转载 时间:12-15 点击:

内容载入中...
     为了对模型的集合信息进行描述,我们有设计了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 = OFF) ON [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 = OFF) ON [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
    。

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息