Default.aspx
Copy Code code 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 ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
<asp:label id= "Lbtext" runat= "Server" ></asp:Label>
</form>
</body>
Default.aspx.cs
Copy Code code 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.appsettings["Strcon"];
SqlConnection mycon = new SqlConnection (connectionString);//CREATE DATABASE connection
String sqlcategory = "Select Id,c_name from Photo_category";//Query album classification table information
String Sqlphoto = "Select Categoryid,title from Photo";//Query album table information
SqlDataAdapter da = new SqlDataAdapter (Sqlcategory, mycon);//Create Data adapter
DataSet ds = new DataSet ();//Create Data set
Try
{
if (mycon. State.equals (connectionstate.closed))
{mycon. Open (); }//explicitly open a database connection
Da. Fill (ds, "Photo_category");//Filled album classification table
Da.SelectCommand.CommandText = Sqlphoto;
Da. Fill (ds, "Photo");//Filled album information table
}
Finally
{
Mycon. Close ()//explicitly shut down the database connection
}
Creating DataRelation objects, associating relationships between tables
DataRelation Relat = new DataRelation ("Photo_category", ds. tables["Photo_category"]. columns["ID"],ds. tables["Photo"]. Columns["CategoryID"]);
Ds. Relations.Add (Relat);//Add Table relationships
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 ()//To output the run results to the page
}
}