ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
MODULE 3-1
Database Programming
LEARNING OBJECTIVES
1. Define a database and the different database management systems,
2. Use ADO.NET in creating and manipulating databases, and
3. Use database programming in developing an application.
LEARNING CONTENTS
⃝ Database Fundamentals
• What is a Database?
o A database is an organized data for access and
manipulation under pragmatic or application control.
o A database is also defined as a collection of data or
information that is organized so that it can easily be
accessed, managed, and updated.
o A database is presented as tables and each piece of
data in a database is called a data entry.
▪ Each data entry is categorized under a field.
▪ Hence, the data entry under a field is of the same
datatype.
▪ A row of adjacent data entries under different
fields is called a record.
Page 1 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
• Types of Database
1. Flat File Database
Consists of a single large table which contain all data entries.
Example
2. Relational Database
A system designed by Edgar Codd of IBM in 1970 and consists of several logically related
tables.
To make a table relational, you choose one or more fields to function as primary and foreign
keys.
▪ A primary key (PK) are fields containing values that uniquely identify each record in a
table (w/solid-line box).
▪ A foreign key (FK) are fields which establishes and enforces a link between the data in
two tables to control the data that can be stored in the foreign key table (w/dashed-line
box).
SQL (Structured Query Language)
➢ The standard language (by ANSI and ISO) for relational database management systems
➢ Used to communicate with a database, i.e. creates and manages relational databases
➢ Some SSIS (SQL Server Integration Services) Connection Managers
Open Database Connectivity (ODBC) – an “adaptor” Microsoft and Oracle applications used
to communicate to SQL-based databases.
Object-link and Embedding for Databases (OLE-DB) – “adaptor” Microsoft used to
communicate its own Component Object Model (COM)-based data sources (not SQL-based,
like MS Access) with standardized databases using SQL.
Page 2 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
➢ Some notes on SQL
SQL programming statements are used to perform tasks such as "Select", "Insert", "Update",
"Delete", "Create", and "Drop" data from a database.
Common relational database management systems (RDBMS) that use SQL are the following:
Oracle, Sybase, Microsoft SQL Server, Access, and Ingres.
Although most database systems use SQL, most DBMS also have their own additional
proprietary extensions that are usually only used on their system.
⃝ Managing Databases
o Databases in VB
o VB uses a process known as data binding to provide a simple and consistent way for applications,
thru its user interface, to present and interact with data.
o Before .NET, VB uses three different interfaces to facilitate data binding among database
protocols and these interfaces are:
Data Access Objects (DAO). For MS Access Databases (*.mdb) which uses the Joint Engine
Technology (JET) database engine.
Remote Data Objects (RDO). For ODBC (Non-MS databases such as Oracle)
ActiveX Data Objects (ADO). For OLE-DB
o Each type of interface uses data controls to connect and move within databases. In .NET versions
of VB (2008 and up), data controls were replaced by Binding Navigator Control.
o Specific data controls which display data entries of a database are called Data Bound Controls.
Some of these controls include: picture boxes, labels, text boxes, lists, combo-boxes, and grids.
o ActiveX Data Objects (ADO)
o ADO is composed of a library of objects facilitate modular approach in connecting with
databases.
Page 3 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o Since DAO and RDO were limited to relational databases, Microsoft developed ADO to extend
the database programming features of VB among web pages, Excel spreadsheets, and other
documents using the OLE-DB protocol.
Four choices to access SQL before ADO
1. DAO -Used Jet database engine to access Microsoft and other ISAM data sources
2. RDO - Easy-to-use interface and has ability to access almost any SQL data source
3. ODBC - Can be accessed directly
4. OLE DB - offered access to ISAM, SQL, non-SQL, and legacy data; Most difficult interface
With the release of Microsoft .Net Framework, in 2002, ADO.NET replaced ADO as the new interface
for database programming in Visual Studio.
o ADO.Net
o an object-oriented set of libraries that allows
you to interact with data sources. Commonly,
the data source is a database, but it could also
be a text file, an Excel spreadsheet, or an XML*
file.
`*XML stands for Extensible Markup
Language, which is another way of
transforming documents into output for
screen, similar to HTML.
o All classes in ADO.NET are in the System.Data
namespace of the Framework.
o Since different data sources expose different
protocols, a way to communicate with the right
data source using the right protocol is needed
➢ comes in different sets of libraries for each
way you can talk to a data source →
DataProvider
Page 4 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o The two main components of ADO.NET for accessing and manipulating data are:
1. Data Providers
▪ “fetches” data from the database and delivers it to the user interface of the program
▪ To connect with an external database, ADO.NET may use multiple data providers for
Microsoft SQL Server and other database platforms
▪ Data Provider Component is composed of
i. Connection Object. Handles all communication from external data source
ii. Command Object. Takes the SQL statement and prepares it for transport through the
Connection object.
iii. DataAdapter Object. Modifies data manipulation statements generated by a data set
into a format that can be used by the related data source
iv. DataReader Object. provides fast, read-only access to the results of a query
2. DataSet
▪ an exact and complete copy of the database as presented to the user interface. It is not
the database itself
▪ DataTable and DataRelations make up the DataSet component of ADO.NET and together
are used for working with internal data (stand-alone).
▪ DataTable is an actual table representation of in-memory data.
Each table has DataColumn items which define the datatype of the individual data
values that appear in the table’s records.
A DataRow is an entry for each record of data stored within a table under a specified
datatype in a DataColumn.
▪ DataRelation represents a parent/child relationship between two DataTable objects.
Page 5 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
Using ADO.Net
o Creating and Connecting a Data Source
Using MS SQL Server Compact Edition (MS SQL Server CE), a database can be created and added
in a VB Project
1. Create a new Windows Forms Application Project.
2. Go to the menu bar and choose Project|Add New Item and choose “Local Database”.
3. Add a valid DataSet name (no space) and click Finish.
4. In the Database Explorer Window, right-click “Tables” and click “Create Table”
5. In the New Table Window, indicate the Table Name, add the necessary columns and field
properties, and click Ok.
Page 6 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
6. In the Database Explorer Window, right-click the newly created table and click “Show
Table Data.”
7. Enter the necessary values for the table.
8. In the Data Sources window, right-click the dataset and choose “Configure Data Source
with Wizard”.
9. Check the Tables in the Data Source Configuration Wizard Window and click “Finish”. You
have successfully connected a DataTable in your project.
Page 7 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o Adding Data Bound Controls
▪ From the Data Sources Window, data bound control based from a DataTable or
DataColumn may be added to a form thru drag and drop.
▪ Choosing a DataTable can give the following options:
1. DataGridView. Default logical representation of a DataTable thru an actual table
2. Details. Creates textboxes and the equivalent labels of all DataColumns of the
DataTable
▪ Choosing a DataColumn can give the following options:
1. TextBox, Label, or Linklabel. When dragged and dropped to a form, creates a
TextBox, Label or Linklabel which can only be controlled thru a binding navigator
control.
2. ComboBox or ListBox. When dragged and dropped to a form, creates either a
ComboBox or ListBox.
Page 8 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o Working with DataGrids
DataGrid
The DataGrid
control
provides the
easiest way to
manage a
binded data
thru a form.
It enables
display and
edit data from
SQL database,
LINQ query, or
any other
bindable data
source.
When used
with other data
bound control,
it can also act
as a navigation
control.
Page 9 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o Searching for a Record
A record can be selected from a DataGrid in two ways:
1. Selecting Manually. Selected records in a DataGrid are displayed on other data
bound controls and may be used thru the following:
Example
2. Search thru Code. Thru coding, a value-lookup can also be done among all the cell
entries of a DataGrid control thru the following code:
Example
Note that the rows and columns of a DataGrid control starts with 0 and not 1 as
shown
o Adding Entries to the Database
New records can be added to existing database using SQL commands which can be
accessed by typing the following code at the top-most line of the form (line 1):
Thru MS SQL Server, records can be added using the following steps:
1. Create a connection:
2. Create the Command String. The Command String defines the data that will be added
to the database. (Enclose names with spaces with brackets)
3. Execute the Command.
Page 10 of 11
ENSC 26 COMPUTER APPLICATIONS IN ENGINEERING LECTURE MODULES
o Deleting Entries from the Database
1. Create a connection. (See adding entries on how to create a connection.)
2. Create the Command String. The Command String defines the data that will be added
to the database. (Enclose names with spaces with brackets)
3. Execute the Command.
Prepared by: Edited by:
Maricris C. Cunanan Aidrean P. Opaco
AY 2020-2021 AY 2023-2024
Page 11 of 11