In this example, we will talk about using datalist to bind controls. Because I just learned this stuff, please give me some advice.
Asp tutorial. net code
Private void dataload ()
{
Dataset ds = new dataset ();
String SQL = "select * from partent ";
Sqlconnection con = new sqlconnection ("data source = pc-200912151837; initial catalog = test; integrated security = true ");
Sqldataadapter da = new sqldataadapter (SQL, con );
Da. fill (ds, "xinghao ");
String sql2 = "select * from parent ";
Sqldataadapter da2 = new sqldataadapter (sql2, con );
Da2.fill (ds, "parent ");
// This sentence is the most important. It seems that the relationship between two tables is established by id, and then the data is loaded.
Ds. relations. add ("myreation", ds. tables ["son"]. columns ["son_id"], ds. tables ["parent"]. columns ["parent_id"]);
Datalist. datasource = ds. tables ["son"]. defaultview;
Datalist. databind ();
}
Database tutorial code:
Create table partent
(Partent_id int not null primary key, primary key id
Partent_name varchar (50) not null
)
Create table son
(
Son_id id not null primary key, primary key tid
Son_name varchar (50) not null,
Partent_id not null // id associated with partent
)