In this blog, I’m explaining the concept of dataset and datatable.
DataSet
The data set represents a subset of the database. It is the class of System.Data namespace. It does not have a continuous connection to the database. To update the database a reconnection is required. DataSet contains DataTableCollection and their DataRelationCollection. ADO.net dataset contains data tables collections and their relationships it represent a complete set of data including tables that contain tables rows and constraints.
Example
// these lines for creating connection by passing server name, database name and the trusted connection mind.
SqlConnection con = newSqlConnection();
con.ConnectionString = @"Server = MINDSTICK-2\WRIINSTANCE; Database=school; Trusted_Connection=True";
// here connection open
con.Open();
// creeate a dataadapter and invoking a command
SqlDataAdapter da = newSqlDataAdapter("SELECT *FROM Student", con);
// creating a dataset object
DataSet ds = newDataSet();
da.Fill(ds, "Student"); // use the data adapter to fill the data
// close the connection
con.Close();
In this example, we understand how to use of dataset.
DataTable
A datatable is an in-memory representation of a single database table. Datatable is the collection of row and as well as table. Datatable is used to store data in memory in SQL database. Datatable contains a collection of constraints in database
// these lines for creating connection by passing server name, database name and the trusted connection mind.
SqlConnection con = newSqlConnection();
con.ConnectionString = @"Server = MINDSTICK-2\WRIINSTANCE; Database=school; Trusted_Connection=True";
// here connection open
con.Open();
// creeate a dataadapter and invoking a command
SqlDataAdapter da = newSqlDataAdapter("SELECT *FROM Student", con);
// creating a dataset object
DataSet ds = newDataSet();
da.Fill(ds, "Student"); // use the data adapter to fill the data
// create a datatable object
DataTable dt = newDataTable();
// select the table from dataset
dt = ds.Tables["Student"];
// close the connection
con.Close();
In this example, we understand how to use of datatable.
Leave Comment