ADO.Net
ADO.Net
ADO.Net
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 –
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.
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 −