Use DataAdapter to populate multiple tables (using DataRelation) Instance Code _ Practical Tips

Source: Internet
Author: User
Tags create database

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
}

}

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.