dataset.relations属性

前几天想写个树形的XML,知道是用dataset.getxml()方法来实现

网上查了一些资料,查了几本书,想了好久终于搞定了!

下面就是我写的生成树形的XML:


 
1   using  System;
 
2   using  System.Data;
 
3   using  System.Configuration;
 
4   using  System.Collections;
 
5   using  System.Web;
 
6   using  System.Web.Security;
 
7   using  System.Web.UI;
 
8   using  System.Web.UI.WebControls;
 
9   using  System.Web.UI.WebControls.WebParts;
10   using  System.Web.UI.HtmlControls;
11   using  BLL;
12  
13   public   partial   class  Mytest : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
14   {
15     private string singername;
16     protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
17     {
18         singername = Request.QueryString["singer"];  //URL输入singer
19         singername = "周杰伦";       //这里为了方便我直接用姓名带入
20         DataSet ds = new DataSet("singertitle");  //新建一个dataset
21         DataTable singer = new DataTable();  // 新建一个datatable来放歌手
22         DataTable title = new DataTable();  //新建一个datatable来放歌手的专辑
23         BllSinger bllsinger = new BllSinger();   //BLL层
24         BllTitle blltitle = new BllTitle();
25         singer = bllsinger.GetSinger(singername);   //给singer  填充数据
26         title = blltitle.GetTitle(singername);   //给title填充数据
27         singer.TableName = "Siner";   //重命名表名,要不是默认的table1
28         title.TableName = "Title";    //同上;
29 
30         ds.Tables.Add(singer);    //把表填充到dataset里
31         ds.Tables.Add(title);   //同上
32 
33         ds.Relations.Add(new DataRelation("singertitle", singer.Columns["singerid"], 
ExpandedSubBlockStart.gifContractedSubBlock.gif
34         title.Columns["singerid"]));             /**////建立表间关系   singer的singerid 和title的singerid
35         ds.Relations[0].Nested = true;       //把结构嵌套在dataset里,这个很重要,我就因为这个搞了好久
36 
37         int titlecount;                     //用来获取歌手有几个title
38         titlecount = blltitle.GetCounttitle(singername);   //给titlecount赋值
39         DataTable []song = new DataTable[titlecount];  //创建一个datatable数组用来放专辑的(每个专辑一个datatable)
40         for (int i = 0; i < titlecount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
41         {
42             BllSong bllsong = new BllSong();
43             song[i] = bllsong.MyGetSongInfro(title.Rows[i]["专辑名称"].ToString());   //给专辑的datatable填充数据
44             ds.Tables.Add(song[i]);   //添加到dataset里
45             song[i].TableName = "title"+i+"song";  
46             ds.Relations.Add(new DataRelation("titletosong"+i, title.Columns["titleid"], song[i].Columns["titleid"]));
47                                     //建立表间关系
48             ds.Relations[i+1].Nested = true;  //把结构嵌套在dataset里
49 
50         }

51            
52         Response.Write(ds.GetXml());   //输出XML文件
53     }

54 }

55  
ContractedBlock.gifExpandedBlockStart.gif
56      由singer得到singer的信息 #region    由singer得到singer的信息
57       public DataTable GetSinger(string singername)
ExpandedSubBlockStart.gifContractedSubBlock.gif
58         {
ExpandedSubBlockStart.gifContractedSubBlock.gif
59             string[] p ="@singername" };
ExpandedSubBlockStart.gifContractedSubBlock.gif
60             string[] s ={ singername };
61             return Factory.DataAccess.Create().ExcuteDataTable("Mytest_GetSinger", p, s);
62         }

63    #endregion

64  
ContractedBlock.gifExpandedBlockStart.gif
65      由singer获取title的信息 #region 由singer获取title的信息
66         public DataTable GetTitle(string singername)
ExpandedSubBlockStart.gifContractedSubBlock.gif
67         {
ExpandedSubBlockStart.gifContractedSubBlock.gif
68             string[] p ="@singername" };
ExpandedSubBlockStart.gifContractedSubBlock.gif
69             string[] s ={ singername };
70             return Factory.DataAccess.Create().ExcuteDataTable("Mytest_GetTitle", p, s);
71         }

72     #endregion

73  
ContractedBlock.gifExpandedBlockStart.gif
74       由titlename获取专辑里的歌曲 #region  由titlename获取专辑里的歌曲
75         public DataTable MyGetSongInfro(string titlename)
ExpandedSubBlockStart.gifContractedSubBlock.gif
76         {
ExpandedSubBlockStart.gifContractedSubBlock.gif
77             string[] p ="@titlename"};
ExpandedSubBlockStart.gifContractedSubBlock.gif
78             string[] s ={ titlename};
79             return Factory.DataAccess.Create().ExcuteDataTable("Mytest_GetSong", p, s);
80         }

81      #endregion

82  



  

 这样就生成了一个树形的xml页

 

转载于:https://www.cnblogs.com/fly871117/archive/2008/08/26/1276752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值