Unit 4 - ADO - NET & Database4
Unit 4 - ADO - NET & Database4
NET &
Database
UNIT - 4
ADO.NET
ADO.NET (ActiveX Data Object for .NET) is an object-oriented set of
libraries that allows you to interact with data sources.
Commonly, the data source is a database, but it could also be a text file,
an Excel spreadsheet, or an XML file.
SqlCommand object allows you to specify what type of interaction you want to
perform with a database.
SqlCommand object can be used to support disconnected architecture.
For example, you can do select, insert, modify, and delete commands on rows of
data in a database table.
Creating a SqlCommand Object
Similar to other C# objects, you instantiate a SqlCommand object via the
new instance declaration, as follows:
SqlCommand cmd = new SqlCommand
("select CategoryName from Categories", conn);
for instantiating a SqlCommand object. It takes a string parameter that holds
the command you want to execute and a reference to a SqlConnection object.
The following are important built in methods uses in the Command Object to
execute the SQL statements.
Data Reader
DataReader object allows forward-only, read-only access to a database.
Using DataReader is the connected way of accessing data and an open connection must be
available first.
Each provider has its own version of DataReader which inherits to
the System.Data.Common.DbDataReader base class.
DataReader cannot be created directly from code, they can created only by calling
the ExecuteReader method of a Command Object.
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
Connection Object can contain only one DataReader at a time and the connection in the
DataReader remains open, also it cannot be used for any other purpose while data is being
accessed.
Read() method in the DataReader is used to read the rows from DataReader and it always
moves forward to a new valid row, if any row exist .
sqlReader.Read();
Data Adapter
DataAdapter can be considered as a bridge between the actual data
source to your application.
It is commonly used together with a DataSet. Using DataAdapter
and DataSet is the disconnected way of retrieving data from the
data source.
DataAdapter allows you to fill a DataSet with values from the data
source, or execute different commands to the data source.
DataAdapter class inherits from the
System.Data.Common.DbDataAdapter base class.
Each data provider has its own version of DataAdapter.
Data Adapter Properties
The DataAdapter is the one that actually executes the commands to data
source. It has a SelectCommandproperty which accepts
a DbCommand object that specifies the SELECT statement used to
retrieved data from the data source.
The following shows you an example of assigning a SelectCommand.
SqlCommand selectCommand = new SqlCommand("SELECT * FROM
Students", connection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = selectCommand;
To execute the command specified by the SelectCommand property, we
can use the Fill() method of the DbDataAdapter class.
The Fill() method requires an instance of the DataSet or DataTable classes.
The following shows an example of filling a DataTable instance with
values retrieved from the database.
adapter.Fill( DataSet/DataTable);
Dataset
System.Data.DataSet class holds data that are retrieved from the database.
DataSet class allows you to hold disconnected data.
DataSet contains DataTableCollection and theirDataRelationCollection . It
represents a complete set of data including the tables that contain, order, and
constrain the data, as well as the relationships between the tables.
Dataset contains more than one Table at a time. We can set up Data
Relations between these tables within the DataSet. The data set may comprise
data for one or more members, corresponding to the number of rows.
DataAdapter Object allows us to populate 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.
Data Table
dv = dt.DefaultView;
Data GridView
ad.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Databind();
Repeater Control
1. <HeaderTemplate>
2. <FooterTemplate>
3. <ItemTemplate>
4. <AlternatingItemTemplate>
5. <SeperatorTemplate>