Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DataAdapter to populate multiple tables

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report