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