Data Control
Create a simple database application which enables one to browse customers' names. To create this
application, select the data control on the toolbox and insert it into the new form. Place the data control
somewhere at the bottom of the form. Name the data control as data_navigator. To be able to use the data
control, we need to connect it to any database. We can create a database file using any database application but
use the database files that come with visual basic. Let's select NWIND.MDB as our database file.
Connecting Data Control to Database
To connect the data control to this database, double-click the DatabaseName property in the Properties
window and then click on the button with three dots on the right to open a file selection dialog. From the
dialog, search the folders of your hard drive to locate the database file NWIND.MDB. It is usually placed
under Microsoft Visual Studio\VB98\ folder, Select the aforementioned file and now your data control is
connected to this database file.
The next step is to double-click on the RecordSource property to select the customers table from the database
file NWIND.MDB. You can also change the caption of the data control to anything, we use Click to browse
Customers. After that, we will place a label and change its caption to Customer Name. In addition, insert
another label and name it as cus_name and leave the label empty as customers' names will appear here when
we click the arrows on the data control. We need to bind this label to the data control for the application to
work. To do this, open the label's DataSource and select data_navigator that will appear automatically. One
more thing that we need to do is to bind the label to the correct field so that data in this field will appear on
this label. To do this, open the DataField property and select ContactName.
Now, press F5 and run the program. You should be able to browse all the customers' names by clicking the
arrows on the data control.
The Design Interface
The Runtime Interface
You can also add other fields using exactly the same method. For example, you can add title, company,
address, City, postcode ,telephone number and more to the database browser. Besides, you can design a more
professional interface.
ADO.Net Object Model
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. ADO.Net object model is nothing but the
structured process flow through various components.
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.
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.
The data provider in ADO.Net consists of the following four objects −
Objects & Description
Connection: This component is used to set up a connection with a data source.
Command: A command is a SQL statement or a stored procedure used to retrieve,
insert, delete or modify data in a data source.
DataReader: Data reader is used to retrieve data from a data source in a read-only and
forward-only mode.
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.
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
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 −
Components & Description
DataTableCollection: It contains all the tables retrieved from the data source.
DataRelationCollection: It contains relationships and the links between tables in a data
set.
ExtendedProperties: It contains additional information, like the SQL statement for
retrieving data, time of retrieval, etc.
DataTable: It represents a table in the DataTableCollection of a dataset. It consists of the
DataRow and DataColumn objects. The DataTable objects are case-sensitive.
DataRelation: It represents a relationship in the DataRelationshipCollection of the dataset.
It is used to relate two DataTable objects to each other through the DataColumn objects.
DataRowCollection: It contains all the rows in a DataTable.
DataView: It represents a fixed customized view of a DataTable for sorting, filtering,
searching, editing and navigation.
PrimaryKey: It represents the column that uniquely identifies a row in a DataTable.
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.
DataColumnCollection: It represents all the columns in a DataTable.
DataColumn: It consists 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, in a database named testDB. 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.
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 −