比如数据库里有两个关联的表,要实现以下的格式:
=======================
A
A1 A2 A3 A4 A5
B
B1 B2 B3 B4
C
C1 C2 C3 C4
......
......
=======================
如果是ConsoleApplication可以用下面的代码实现
SqlConnection Conn = new SqlConnection("server=.;uid=sa;pwd=chengbo;database=northwind;");
SqlDataAdapter daOrders = new SqlDataAdapter("SELECT OrderID FROM Orders", Conn);
SqlDataAdapter daOrderDetails = new SqlDataAdapter("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn);
DataSet ds = new DataSet();
DataTable dtOrders = ds.Tables.Add("Orders");
DataTable dtOrderDetails = ds.Tables.Add("OrderDetails");
Conn.Open();
daOrders.Fill(dtOrders);
daOrderDetails.Fill(dtOrderDetails);
Conn.Close();
ds.Relations.Add("OrdersAndDetails", dtOrders.Columns["OrderID"], dtOrderDetails.Columns["OrderID"]);
foreach(DataRow drParent in dtOrders.Rows)
{
Console.WriteLine(drParent["OrderID"]);
foreach(DataRow drChild in drParent.GetChildRows("OrdersAndDetails"))
{
Console.WriteLine("{0} {1}\t", drChild["UnitPrice"], drChild["Quantity"]);
}
}
SqlDataAdapter daOrders = new SqlDataAdapter("SELECT OrderID FROM Orders", Conn);
SqlDataAdapter daOrderDetails = new SqlDataAdapter("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn);
DataSet ds = new DataSet();
DataTable dtOrders = ds.Tables.Add("Orders");
DataTable dtOrderDetails = ds.Tables.Add("OrderDetails");
Conn.Open();
daOrders.Fill(dtOrders);
daOrderDetails.Fill(dtOrderDetails);
Conn.Close();
ds.Relations.Add("OrdersAndDetails", dtOrders.Columns["OrderID"], dtOrderDetails.Columns["OrderID"]);
foreach(DataRow drParent in dtOrders.Rows)
{
Console.WriteLine(drParent["OrderID"]);
foreach(DataRow drChild in drParent.GetChildRows("OrdersAndDetails"))
{
Console.WriteLine("{0} {1}\t", drChild["UnitPrice"], drChild["Quantity"]);
}
}
但是WEB页怎么办呢?可以用两个Repeater实现
.aspx
<asp:repeater id="parentRepeater" runat="server">
<headertemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
</headertemplate>
<itemtemplate>
<tr>
<td bgcolor="#8c6979" height="30" style="PADDING-LEFT: 15px; FONT-SIZE: 12px; COLOR: #ffffff">
<%# DataBinder.Eval(Container.DataItem,"OrderID") %>
</td>
</tr>
<tr>
<td height="1"></td>
</tr>
<tr>
<td bgcolor="#c9a9b8" style="PADDING:5PX; ">
<asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("OrdersAndDetails") %>'>
<itemtemplate>[ <%# DataBinder.Eval(Container.DataItem,"[\"UnitPrice\"]") %> - <%# DataBinder.Eval(Container.DataItem,"[\"Quantity\"]") %> ]</itemtemplate>
</asp:repeater><br>
</td>
</tr>
<tr>
<td height="1"></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
<asp:repeater id="parentRepeater" runat="server">
<headertemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
</headertemplate>
<itemtemplate>
<tr>
<td bgcolor="#8c6979" height="30" style="PADDING-LEFT: 15px; FONT-SIZE: 12px; COLOR: #ffffff">
<%# DataBinder.Eval(Container.DataItem,"OrderID") %>
</td>
</tr>
<tr>
<td height="1"></td>
</tr>
<tr>
<td bgcolor="#c9a9b8" style="PADDING:5PX; ">
<asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("OrdersAndDetails") %>'>
<itemtemplate>[ <%# DataBinder.Eval(Container.DataItem,"[\"UnitPrice\"]") %> - <%# DataBinder.Eval(Container.DataItem,"[\"Quantity\"]") %> ]</itemtemplate>
</asp:repeater><br>
</td>
</tr>
<tr>
<td height="1"></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
.aspx.cs
SqlConnection Conn = new SqlConnection("server=.;uid=sa;pwd=chengbo;database=northwind;");
SqlDataAdapter daOrders = new SqlDataAdapter("SELECT OrderID FROM Orders", Conn);
SqlDataAdapter daOrderDetails = new SqlDataAdapter("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn);
DataSet ds = new DataSet();
DataTable dtOrders = ds.Tables.Add("Orders");
DataTable dtOrderDetails = ds.Tables.Add("OrderDetails");
Conn.Open();
daOrders.Fill(dtOrders);
daOrderDetails.Fill(dtOrderDetails);
Conn.Close();
//添加Relation
ds.Relations.Add("OrdersAndDetails", dtOrders.Columns["OrderID"], dtOrderDetails.Columns["OrderID"]);
this.parentRepeater.DataSource = dtOrders;
//调用Page类的DataBind方法,隐式调用两个Repeater对象实例的DataBind方法
this.Page.DataBind();
SqlConnection Conn = new SqlConnection("server=.;uid=sa;pwd=chengbo;database=northwind;");
SqlDataAdapter daOrders = new SqlDataAdapter("SELECT OrderID FROM Orders", Conn);
SqlDataAdapter daOrderDetails = new SqlDataAdapter("SELECT OrderID, UnitPrice, Quantity FROM [Order Details]", Conn);
DataSet ds = new DataSet();
DataTable dtOrders = ds.Tables.Add("Orders");
DataTable dtOrderDetails = ds.Tables.Add("OrderDetails");
Conn.Open();
daOrders.Fill(dtOrders);
daOrderDetails.Fill(dtOrderDetails);
Conn.Close();
//添加Relation
ds.Relations.Add("OrdersAndDetails", dtOrders.Columns["OrderID"], dtOrderDetails.Columns["OrderID"]);
this.parentRepeater.DataSource = dtOrders;
//调用Page类的DataBind方法,隐式调用两个Repeater对象实例的DataBind方法
this.Page.DataBind();
记住要在aspx文件顶部上加入
<%@ Import Namespace="System.Data" %>