Working With Databases: Retrieve and Display Data
Working With Databases: Retrieve and Display Data
NET -
Overview of ADO.NET objects – Connections- Dataadapters - Data set - Accessing data with
server explorer -Understanding data source controls - Data binding using data bound controls -
Working with data grids - Customizing data grids - Working with grid view controls - Data
binding with grid view control- Creating a new data connection - Using access data source
class.
UNIT V
Introduction
Overview of ADO.Net
ADO.NET provides a bridge between the front end controls and the back end
database. The ADO.NET objects encapsulate all the data access operations and the
controls interact with these objects to display data, thus hiding the details of
movement of data.
The two key components of ADO.NET are Data Providers and DataSet .
DATA PROVIDER
The .Net Framework includes mainly three Data Providers for ADO.NET. They are
o Microsoft SQL Server Data Provider,
o OLEDB Data Provider
o ODBC Data Provider.
SQL Server uses the SqlConnection object,
OLEDB uses the OleDbConnection Object
ODBC uses OdbcConnection Object respectively.
The four Objects from the .Net Framework provide the functionality of Data Providers in the
ADO.NET. They are
Connection Object
Command Object
DataReader Object
DataAdapter Object.
-The Connection Object provides physical connection to the Data Source.
-The Command Object uses to perform SQL statement or stored procedure to be executed
at the Data Source.
-The DataReader Object is a stream-based , forward-only, read-only retrieval of query
results from the Data Source, which do not update the data.
-Finally the DataAdapter Object , which populate a Dataset Object with results from a
Data Source .
THE DATASET
The dataset represents a subset of the database. It does not have a continuous connection
to the database. To update the database a reconnection is required. The DataSet contains
DataTable objects and DataRelation objects. The DataRelation objects represent the
relationship between two tables.
CONNECTION OBJECT
The Connection Object is a part of ADO.NET Data Provider and it is a unique session with
the Data Source. In .Net Framework the Connection Object is Handling the part of physical
communication between the application and the Data Source. Depends on the parameter
specified in the Connection String , ADO.NET Connection Object connect to the specified
Database and open a connection between the application and the Database . When the
connection is established , SQL Commands may be executed, with the help of the
Connection Object, to retrieve or manipulate data in the Database. Once the Database
activity is over , Connection should be closed and release the resources .
In ADO.NET the type of the Connection is depend on what Database system you are
working with. The following are the commonly using the connections in the ADO.NET
SqlConnection
OleDbConnection
OdbcConnection
DATA ADAPTERS
DataAdapter is a part of the ADO.NET Data Provider. DataAdapter provides the
communication between the Dataset and the Datasource. We can use the DataAdapter in
combination with the DataSet Object. That is these two objects combine to enable both data
access and data manipulation capabilities.
The DataAdapter can perform Select , Insert , Update and Delete SQL operations in the Data
Source. The Insert , Update and Delete SQL operations , we are using the continuation of the
Select command perform by the DataAdapter. That is the DataAdapter uses the Select
statements to fill a DataSet and use the other three SQL commands (Insert, Update, delete)
to transmit changes back to the Database. From the following links describe how to use
SqlDataAdapter and OleDbDataAdapter in detail.
SqlDataAdapter
OleDbDataAdapter
DATA SET
The dataset represents a subset of the database. It does not have a continuous connection
to the database. To update the database a reconnection is required. The DataSet contains
DataTable objects and DataRelation objects. The DataRelation objects represent the
relationship between two tables.
ASP.NET provides a wide variety of rich controls that can be bound to data. Under the Data
tab of the Visual Studio Toolbox, you can get several controls under the Data tab that could
be used to display data from a data source, like a database or XML file.
DataGridView is very powerful and flexible control for displaying records in a tabular (row-
column) form. Here I am describing a different way of databinding with a DataGridView
control.
Step 1 : Select DataGridView control and click at smart property. Look at the following
figure.
Step 5 : Choose Database (By default it is selected) and click the next button. A new
window will be open to Database Model.
Step 6 : Select DataSet (By default it is selected) and click the next button. A new window
will be open.
The DataGridView control provides several properties that you can use to adjust the
appearance and basic behavior (look and feel) of its cells, rows, and columns. If you have
special needs that go beyond the capabilities of the DataGridViewCellStyle class, however,
you can also implement owner drawing for the control or extend its capabilities by creating
custom cells, columns, and rows.
To paint cells and rows yourself, you can handle various DataGridView painting events. To
modify existing functionality or provide new functionality, you can create your own types
derived from the existing DataGridViewCell , DataGridViewColumn ,
and DataGridViewRow types. You can also provide new editing capabilities by creating
derived types that display a control of your choosing when a cell is in edit mode.
GridView control renders data items in an HTML table. Each data item is rendered in a
distinct HTML table row. For example, given code will demonstrates how we use the
GridView to display the contents of the database table.