An example of the differences between DataSet and DataTable is presented

  • 2020-12-19 20:58:38
  • OfStack

DataSet: Data sets. Generally contains multiple DataTable, when used, dataset[" table name "] gets DataTable

DataTable: Data tables.

1:
SqlDataAdapter
da=new
SqlDataAdapter(cmd);
DataTable
dt=new
DataTable();
da.Fill(dt);
-----------------
I'm just going to put the data in
datatable,

2:
SqlDataAdapter
da=new
SqlDataAdapter(cmd);
DataSet
dt=new
DataSet();
da.Fill(dt);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
The data result is placed in dataset. To use that datatable, you can do this: dataset[0]

More common usage:
SqlDataAdapter
da=new
SqlDataAdapter(cmd);
DataSet
dt=new
DataSet();
da.Fill(dt,"table1");
When used: datatable:
dataset["table1"]

Specific applications:
SqlConnection con = new SqlConnection("server=.;database=StuCourseDb1;uid=sa;pwd=xhz;");
SqlDataAdapter sda = new SqlDataAdapter("select * from student", con);
DataSet ds = new DataSet();
sda.Fill(ds, "StuTable");
this.GridView1.DataSource = ds.Tables["StuTable"];
this.GridView1.DataBind();
ds.Dispose();
con.Close();
con.Dispose();

Related articles: