Database Access
Database Access
Database Access
Applications communicate with a database, firstly, to retrieve the data stored there and
present it in a 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 part of the .Net framework
that is used by the .Net applications for retrieving, accessing and updating data.
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.
Datasets store data in a disconnected cache and the application retrieves data
from it.
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 1/15
3/8/2018 VB.Net - Database Access
Data Provider
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.
1 Connection
2 Command
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.
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.
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 2/15
3/8/2018 VB.Net - Database Access
The EntityClient provider - enables accessing data through Entity Data Model
(EDM) applications.
DataSet
DataSet is an in-memory representation of data. It is a disconnected, cached set of
records that are retrieved from a database. When a connection is established with the
database, the data adapter creates a dataset and stores data in it. After the data is
retrieved and stored in a 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 DataSet class is present in the System.Data namespace. The following table
describes all the components of DataSet:
1 DataTableCollection
2 DataRelationCollection
3 ExtendedProperties
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 3/15
3/8/2018 VB.Net - Database Access
It contains additional information, like the SQL statement for retrieving data, time
of retrieval, etc.
4 DataTable
5 DataRelation
6 DataRowCollection
7 DataView
8 PrimaryKey
9 DataRow
It represents a row in the DataTable. The DataRow object and its properties and
methods are used to retrieve, evaluate, insert, delete, and update values in the
DataTable. The NewRow method is used to create a new row and the Add method
adds a row to the table.
10 DataColumnCollection
11 DataColumn
Connecting to a Database
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 4/15
3/8/2018 VB.Net - Database Access
Example 1
We have a table stored in Microsoft SQL Server, named Customers, in a database named
testDB. Please consult 'SQL Server' tutorial for creating databases and database tables in
SQL Server.
Select a server name and the database name in the Add Connection dialog box.
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 5/15
3/8/2018 VB.Net - Database Access
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 6/15
3/8/2018 VB.Net - Database Access
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 7/15
3/8/2018 VB.Net - Database Access
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 8/15
3/8/2018 VB.Net - Database Access
Choose the database object, Customers table in our example, and click the Finish
button.
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 9/15
3/8/2018 VB.Net - Database Access
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:
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 10/15
3/8/2018 VB.Net - Database Access
Example 2
In this example, let us access data in a DataGridView control using code. Take the
following steps:
Double click the button control to add the required code for the Click event of the
button, as shown below:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
'TODO: This line of code loads data into the 'TestDBDataSet.CUSTOMERS' table. You can m
Me.CUSTOMERSTableAdapter.Fill(Me.TestDBDataSet.CUSTOMERS)
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As SqlConnection = New sqlconnection()
connection.ConnectionString = "Data Source=KABIR-DESKTOP; _
Initial Catalog=testDB;Integrated Security=True"
connection.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter _
("select * from Customers", connection)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
When the above code is executed and run using Start button available at the Microsoft
Visual Studio tool bar, it will show the following window:
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 11/15
3/8/2018 VB.Net - Database Access
Clicking the Fill button displays the table on the data grid view control:
Example 3
So far, we have used tables and databases already existing in our computer. In this
example, we will create a table, add columns, rows and data into it and display the table
using a DataGridView object.
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 12/15
3/8/2018 VB.Net - Database Access
When the above code is executed and run using Start button available at the Microsoft
Visual Studio tool bar, it will show the following window:
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 13/15
3/8/2018 VB.Net - Database Access
Clicking the Fill button displays the table on the data grid view control:
YouTube 56K
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 14/15
3/8/2018 VB.Net - Database Access
https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm 15/15