Default. aspx
Copy codeThe Code is as follows:
View Code
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! 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> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
</Div>
<Asp: Label ID = "lbText" runat = "server"> </asp: Label>
</Form>
</Body>
</Html>
Default. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Data. SqlClient;
Using System. Text;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String connectionString = ConfigurationSettings. etettings ["strCon"];
SqlConnection mycon = new SqlConnection (connectionString); // create a database connection
String sqlCategory = "select ID, C_Name from Photo_Category"; // query information in the album category table
String sqlPhoto = "select CategoryID, Title from Photo"; // query information in the album table
SqlDataAdapter da = new SqlDataAdapter (sqlCategory, mycon); // create a data adapter
DataSet ds = new DataSet (); // create a DataSet
Try
{
If (mycon. State. Equals (ConnectionState. Closed ))
{Mycon. Open ();} // explicitly opens the database connection
Da. Fill (ds, "Photo_Category"); // Fill in the album category table
Da. SelectCommand. CommandText = sqlPhoto;
Da. Fill (ds, "Photo"); // Fill in the album information table
}
Finally
{
Mycon. Close (); // closes the database connection explicitly
}
// Create a DataRelation object and associate the relationship between tables
DataRelation relat = new DataRelation ("Photo_Category", ds. tables ["Photo_Category"]. columns ["ID"], ds. tables ["Photo"]. columns ["CategoryID"]);
Ds. Relations. Add (relat); // Add the relationship between tables.
StringBuilder builder = new StringBuilder ("");
Foreach (DataRow row in ds. Tables ["Photo_Category"]. Rows)
{
Builder. Append ("<B> ");
Builder. Append (row ["C_Name"]. ToString ());
Builder. Append ("</B> <ul> ");
DataRow [] childRows = row. GetChildRows (relat );
Foreach (DataRow childRow in childRows)
{
Builder. Append ("<li> ");
Builder. Append (childRow ["Title"]. ToString ());
Builder. Append ("</li> ");
}
Builder. Append ("</ul> ");
}
LbText. Text + = builder. ToString (); // output the running result to the page.
}
}