Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use DataAdapter to populate multiple tables

Shulou Source: shulou.com Published: 2022-06-03 07:40:57 10月05日 Update

This article mainly introduces how to use DataAdapter to fill multiple tables, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Default.aspx

The code is as follows:

View Code

Untitled page

Default.aspx.cs

The copy 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.AppSettings ["strCon"]

SqlConnection mycon = new SqlConnection (connectionString); / / create a database connection

String sqlCategory = "select ID,C_Name from Photo_Category"; / / query the information in the album classification 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 open the database connection

Da.Fill (ds, "Photo_Category"); / / fill in the album classification table

Da.SelectCommand.CommandText = sqlPhoto

Da.Fill (ds, "Photo"); / / fill in the album information table

}

Finally

{

Mycon.Close (); / / explicitly close the database connection

}

/ / create a DataRelation object to 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 relationships between tables

StringBuilder builder = new StringBuilder ("")

Foreach (DataRow row in ds.Tables ["Photo_Category"] .Rows)

{

Builder.Append ("")

Builder.Append (row ["C_Name"] .ToString ()

Builder.Append ("")

DataRow [] childRows = row.GetChildRows (relat)

Foreach (DataRow childRow in childRows)

{

Builder.Append ("")

Builder.Append (childRow ["Title"] .ToString ()

Builder.Append ("")

}

Builder.Append ("")

}

LbText.Text + = builder.ToString (); / / output the run result to the page

}

}

Thank you for reading this article carefully. I hope the article "how to fill multiple tables with DataAdapter" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Data photo albums articles code information database multiple classification query value interest at the same time objects more friends title knowledge results editing industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Apple Shulou Tech Info macOS