using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create the connection and DataAdapter for the Authors table. //创建用于连接Authors表的connection和DataAdapter对象 SqlConnection cnn = new SqlConnection("server=(local);database=pubs; Integrated Security=SSPI"); SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors", cnn); //Create and fill the DataSet. //创建并填充DataSet对象 DataSet ds = new DataSet(); cmd1.Fill(ds, "authors"); //Create a second DataAdapter for the Titles table. //为Titles表创建第二个DataAdapter对象 SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor", cnn); cmd2.Fill(ds, "titles"); //Create the relation between the Authors and Titles tables. //为Authors和Titles表创建关联(Relation) ds.Relations.Add("myrelation", ds.Tables["authors"].Columns["au_id"], ds.Tables["titles"].Columns["au_id"]); //Bind the Authors table to the parent Repeater control, and call DataBind. //调用DataBind方法将Authors绑定到parent Repeater控件上 parentRepeater.DataSource = ds.Tables["authors"]; Page.DataBind(); //Close the connection. cnn.Close(); } } 上面是后台, 下面是前台,注意导入命名空间的代码:<%@ Import Namespace="System.Data" %> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater id="parentRepeater" runat="server"> <itemtemplate> <b> <%# DataBinder.Eval(Container.DataItem, "au_id") %> </b> <br> <asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>' > <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[/"title_id/"]")%><br> </itemtemplate> </asp:repeater> </itemtemplate> </asp:Repeater> </div> </form> </body> </html>