What is the difference between a DataTable and a dataset

Source: Internet
Author: User

code example:

string sqlcoon = "server=.;D atabase=abc;uid=sa;pwd=123456 ";
SqlConnection coon = new SqlConnection (sqlcoon);
String sql = "SELECT * from Smartercloud_unit";
SqlCommand cmd = new SqlCommand (sql,coon);
Coon. Open ();
//SqlDataReader
//sqldatareader reader = cmd. ExecuteReader (commandbehavior.closeconnection);
//datatable table = new DataTable ();
//table. Load (reader);
//messagebox.show (table. Rows.Count.ToString ());
//this.datagridview1.datasource = table;


SqlDataAdapter
SqlDataAdapter adptr = new SqlDataAdapter (SQL, Coon);
DataSet ds = new DataSet ();
Adptr. Fill (ds, "Smartercloud_unit");
This.dataGridView1.DataSource = ds. Tables[0];

DataSet: DataSet. Usually contains multiple DataTable, when used, dataset["table name"] Get DataTable

DataTable: Data table.
One:
SqlDataAdapter Da=new SqlDataAdapter (CMD);
DataTable dt=new DataTable ();
Da. Fill (DT);
-----------------
Put the data results directly into the DataTable,
Two:
SqlDataAdapter Da=new SqlDataAdapter (CMD);
DataSet dt=new DataSet ();
Da. Fill (DT);
----------------
Data results in a dataset, to use that DataTable, you can: Dataset[0]
More common usage:
SqlDataAdapter Da=new SqlDataAdapter (CMD);
DataSet dt=new DataSet ();
Da. Fill (DT, "Table1");
When used: This takes the DataTable:
dataset["Table1"]

From. NET 2.0, you can solve the conversion problem with one line of code:

DataReader to DataTable

Dim dr as SqlDataReader = cmd. ExecuteReader (commandbehavior.closeconnection)

Dim dt as DataTable = New DataTable ()

Dt. Load (DR)

DataTable to DataReader

Dim dt Ad DataTable = ...

Dim dr as DataTableReader = dt. CreateDataReader ()

Note: 1. When using the Load method, if data already exists in the DataTable, the old and new data will be merged. (new data refers to read from DataReader)

2. DataTableReader implements the IDataReader and uses the interface as much as possible.

3. Datasets also have similar Load and CreateDataReader methods. (with sample source code)

See the MSDN documentation http://msdn2.microsoft.com/en-us/library/5fd1ahe2.aspx and http://msdn2.microsoft.com/en-us/library/ System.data.dataset.createdatareader.aspx

What is the difference between a DataTable and a dataset

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.