ADO.Net

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ADO.

Net Object Model


ADO.Net object model is nothing but the structured process flow through various
components. The object model can be pictorially described as −

The data residing in a data store or database is retrieved through the data provider.
Various components of the data provider retrieve data for the application and update
data.
An application accesses data either through a dataset or a data reader.
 Datasets store data in a disconnected cache and the application retrieves data
from it.
 Data readers provide data to the application in a read-only and forward-only
mode
 A data provider is used for connecting to a database, executing commands and
retrieving data, storing it in a dataset, reading the retrieved data and updating
the database.
 The data provider in ADO.Net consists of the following four objects –

Sr.No. Objects & Description


1 Connection
This component is used to set up a connection with a data source.

2 Command
A command is a SQL statement or a stored procedure used to retrieve, insert,
delete or modify data in a data source.

3 DataReader
Data reader is used to retrieve data from a data source in a read-only and
forward-only mode.

4 DataAdapter
This is integral to the working of ADO.Net since data is transferred to and from a
database through a data adapter. It retrieves data from a database into a dataset
and updates the database. When changes are made to the dataset, the changes
in the database are actually done by the data adapter.
Connecting to a Database
The .Net Framework provides two types of Connection classes −
 SqlConnection − designed for connecting to Microsoft SQL Server.
 OleDbConnection − designed for connecting to a wide range of databases, like
Microsoft Access and Oracle.

Example
We have a table stored in Microsoft SQL Server, named Customers, in a database
named testDB. Please consult 'SQL Server' for creating databases and database
tables in SQL Server.
Let us connect to this database. Take the following steps −
 Select TOOLS → Connect to Database

 Select a server name and the database name in the Add Connection dialog box.

M
 Click on the Test Connection button to check if the connection succeeded.
 Add a DataGridView on the form.

 Click on the Choose Data Source combo box.


 Click on the Add Project Data Source link.

 This opens the Data Source Configuration Wizard.


 Select Database as the data source type
 Choose DataSet as the database model.

 Choose the connection already set up.


 Save the connection string.
 Choose the database object, Customers table in our example, and click the
Finish button.

 Select the Preview Data link to see the data in the Results grid −

When the application is run using Start button available at the Microsoft Visual Studio
tool bar, it will show the following window −

You might also like