Instance code for filling multiple tables (using DataRelation) with DataAdapter

Source: Internet
Author: User

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.
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.