The ADO.NET DataSet contains DataTableCollection
and their DataRelationCollection. It represents a collection of data retrieved from the Data Source. We can use Dataset in
combination with DataAdapter Class.
The DataSet object offers disconnected data source architecture. The Dataset
can work with the data without knowing the source of the data coming from. That
is, the Dataset can work with a disconnected mode from its Data Source.
The Dataset contains the copy of the data requested. The Dataset
contains more than one Table at a time. We can set up Data Relations between these tables
within the DataSet. The DataAdapter
Object allows us to fill DataTables in
a DataSet. We can use Fill method of the DataAdapter for populating data in a Dataset.
The DataSet can be filled either from a data source or dynamically. A DataSet
can be saved to an XML file and then loaded back into memory very easily. A
DataSet can read and write data and schema as XML documents.
The data and schema can then be transported across HTTP and
used by any application, on any platform that is XML-enabled.
Dataset with Sql Server
The DataSet contains
the copy of the data requested through the SQL
statement. The DataSet is a memory-resident representation of data that provides
a consistent relational programming model regardless of the data source.
We can use Dataset in combination with SqlDataAdapter Class. Build and fill each
DataTable in a DataSet with data from a data source using a DataAdapter. The
SqlDataAdapter object allows us to populate Data Tables in a DataSet.
Example
string strConnection = "Server = abc\\SQLEXPRESs; Database=Employee; Trusted_Connection=True";
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from employee", con);
DataSet ds = new DataSet();
da.Fill(ds, "Employee");
Tables in a Dataset
The DataSet contains DataTableCollection and
their DataRelationCollection.
The DataTableCollection contains zero or more DataTable objects. The Dataset
may comprise data for one or more members, corresponding to the number of rows.
The SqlDataAdapter Object allows
us to populate DataTables in a DataSet. Build and fill each DataTable in a
DataSet with data from a data source using a DataAdapter.
Example
DataTable dt = ds.Tables["Employee"];
Dataset rows count
The DataSet Object contains
copy of the data requested through the SQL
statement. The DataSet consists of DataTableCollection and
their DataRelationCollection.
The DataTableCollection contains zero or more DataTable objects. A DataSet
represents a complete set of data.
The Dataset may comprise data for one or more members,
corresponding to the number of rows. The DataRow
Class represents the actual data contained in a table. We use the
DataRow and its properties and methods to retrieve, evaluate, and manipulate
the data in a table.
Example
MessageBox.Show("Number
of row(s) - " + ds.Tables[0].Rows.Count);
How to find Column Definition
The data inside Table is in the form of Rows and Columns. The DataRow class represents the
actual data contained in a table. We use the DataRow and its properties and methods to
retrieve, evaluate, and manipulate the data in a table. In some situations we
have to find the column headers in a DataTable. There is a ColumnsCollection Object in the
Datatable hold the column Definitions.
Example
foreach (DataColumn column in dt.Columns)
{
MessageBox.Show(column.ColumnName);
}
Leave Comment
2 Comments