‘VB.NET - DATABASE ACCESS
Applications communicate witha database, firstly, to retrieve the data stored there and presentit ina user-
friendly way, and secondly, to update the database by inserting, modifying and deleting data.
Microsoft ActiveX Data Objects. Net (ADO.Net) is a model, a partof the .Net framework that is used by the Net
applications for retrieving, accessing and updating data.
ADO.Net Object Model
ADO.Net object model is nothing but the structured process flow through various components. The object
model canbe pictorialy deseribed as:
Datsbrowider Dataset
ou | |* Se, Saateoetoleaion
mae 7
‘ i I
Date Reader [*—7 Datastore
‘The data residing ina data store or database is retrieved through the data provider. Various components of
the data provider retrieve data for the application and update data.
Anapplication accesses data either througha dataset or a data reader.
+ Datasets store data ina disconnected cache and the application retrieves data from it.
+ Data readers provide data to the application ina read-only and forward-only mode
Data Provider
Adata provider is used for connecting to a database, executing commands and retrieving data, storing itina
dataset, reading the retrieved data and updating the database.
‘The data provider in ADO.Net consists of the following four objects:
S.N Objects & Description
1 Connection
‘This components used to setup a connection witha data source.
2 Command
Acommand is a SQL statement or a stored procedure used to retrieve, insert, delete or modify data in
adata source.Data reader is used to retrieve data froma data source ina read-only and forward-only mode.
4 DataAdapter
‘This is integral to the working of ADO.Net since data is transferred to and froma database througha
data adapter. Itretrieves data froma database into a dataset and updates the database. When
changes are made to the dataset, the changes inthe database are actually done by the data adapter.
‘There are following different types of data providers included in ADO.Net
+ The .Net Framework data provider for SQL Server - provides access to Microsoft SQL Server.
+ The .Net Framework data provider for OLE DB - provides access to data sources exposed by using
OLE DB.
+ The .Net Framework data provider for ODBC - provides access to data sources exposed by ODBC.
+ The Net Framework data provider for Oracle - provides access to Oracle data source.
+ The EntityClient provider - enables accessing data through Entity Data Model (EDM) applications.
DataSet
DataSetis anin-memory representation of data. Itis a disconnected, cached setof records thatare retrieved
froma database. Whena connectionis established with the database, the data adapter creates a dataset and
stores data init, After the data is retrieved and stored ina dataset, the connection with the database is closed.
‘This is called the ‘disconnected architecture’. The dataset works as a virtual database containing tables, rows,
and columns.
‘The following diagram shows the dataset object model
=]
DataRelationCallection DataTableCollection Extended roperties
| DataRelation DataTable
WA N\
| DataRowCollection DataView PrimaryKey DataColumnCollection
DataRow Datatolumn
‘The DataSet class is present inthe System.Data namespace. The following table describes all the components
of Data
S.N_ Components & Description
1 DataTableCollectionun
DataRelationCollection
Itcontains relationships and the links between tables ina data set
ExtendedProperties
Itcontains additional information, lie the SQL statement for retrieving data, time of retrieval, etc.
DataTable
Itrepresents a table inthe DataTableCollection of a dataset. It consists of the DataRow and
DataColumn objects. The Dataable objects are case-sensitive.
DataRelation
Itrepresents a relationship inthe DataRelationshipCollection of the dataset. Its used to relate two
DataTable objects to each other through the DataColumn objects.
DataRowCollection
Itcontains allthe rows ina DataTable.
DataView
Itrepresents a fixed customized view ofa DataTable for sorting, filtering, searching, editing and
navigation.
PrimaryKey
Itrepresents the column that uniquely identifies a row ina DataTable.
DataRow
Itrepresents a row inthe DataTable. The DataRow object and its properties and methods are used to
retrieve, evaluate, insert, delete, and update values inthe DataTable. The NewRow method is used to
create anew row and the Add method adds a row to the table.
DataColumnCollection
Itrepresents all the columns ina DataTable.
DataColumn
Itconsists of the number of columns that comprise a DataTable.
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 1
We
have a table stored in Microsoft SQL Server, named Customers, ina database named testDB. Please consult
"SQL Server' tutorial for creating databases and database tables in SQL Server.
Let}
‘us connectto this database. Take the following steps:
1. Select TOOLS -> Connect to DatabaseTOOLS TEST WINDOW HELP
Attach to Process. Cirle Alte
Be Code Snippets Manage Cirle K, Ctrt+B
Choose Toolbex tems.
Library Package Manager »
Pee eee Foss
reste GUD
Error Lookup
Eternal Tools.
aes
Customize.
Options.
2. Selecta server name and the database name inthe Add Connection dialog box.‘Add Connection ea)
Enter information to connect to the selected data source or click ‘Change” to
choose a different data source and/or provider.
Data source:
Microsoft SQL Server (SqiCient)
Servername:
KABIR-DESKTOP ~ [Refresh
Log on tothe server
Use Windows Authentication
©) Use SQL Server Authentication
‘Save my password
Connect to 0 database
(© Selector enter a database name:
=
Attach » datebase file:
3. Click onthe Test Connection button to check ifthe connection succeeded.
jrosoft Visual Stusio Express 2012 for Windows Des. aaa)
4. Add a DataGridView on the form.DataGridView Tas
cect Sone =)
Ft Clones.
‘dé Column.
Enable Adding
trl Eing
1 tnabiedeting
El kre Coun Reordering
ockin Pit Container
5. Click on the Choose Data Source combo box.
6. Click onthe Add Project Data Source link.
(SNe
|"id_ Add Project Data source.
Click the ‘Add Project Data Source..' lik to
connect to data,
7. This opens the Data Source Configuration Wizard.
8. Select Database as the data source type10. Choose the connectionalready set up.© Connesion sg thutyou wil ve inthe spain apandto se deta)
‘Dita Source: TABIR-DESKTOPnial Caalog-testOB tegrated Securty=True
11, Save the connection string.
G, meena
‘tong ceamection stings your platen confine nes mirtenanc and plore. To sve the