ist 2020 ch 12
TRANSCRIPT
-
7/27/2019 IST 2020 CH 12
1/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 1
12.1 Introduction
Database - Organized collection of data
Database management system (DBMS) Software that provides
mechanisms for storing, organizing, retrieving and modifying data
Visual Basic programs communicate with databases and manipulate
their data through ADO.NET (Active Data Objects - technology usedto interact with a database)
-
7/27/2019 IST 2020 CH 12
2/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 2
12.2 ADO.NET Object Model
ADO.NET Provides an API for accessing database systems
programmatically. For those familiar with previous technologies ADO.NETreplaces Microsoft's ActiveX Data Objects (ADO) technology.
DataSets - Represents cached data from the data source. It can beused when the application is disconnected from the database. As adataset does not need a persistent connection to work with data onecan connect to the data source to populate the DataSet, and
disconnects immediately afterwards then continue working. Later on ifnecessary then one can reconnect to perform an update and thendisconnect
Connection - Represents a connection of dataset to a database
DataAdapter It populates and update the DataSet with data from thedata base and vice versa by executing commands/Query statementsgiven (in SQL)
CommandsUsed by the data adapter to know what to retrieve fromoradd to the database
Data BaseData SetApplication Data Adapter
For Connection
-
7/27/2019 IST 2020 CH 12
3/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 3
12.3 Integrated Development Environment (IDE)
The IDE provides visual programming tools and wizards that simplifyaccessing data in your projects. These tools establish databaseconnections and create the ADO.NET to view and manipulate thedata through GUI controls
Connection string - Specifies the path to a database file. It
determine how the database is accessed Data Set - Adding a data source causes the IDE to generate a
dataset to store data from that data source
DataGridView component - Displays a data source in a GUIorganized in rows and columns
BindingNavigatorcomponent. Provides several ways for users tobrowse and manipulate data displayed by another GUI control on aForm
-
7/27/2019 IST 2020 CH 12
4/32
CH 12 WORKING WITH DATABASES
Example on IDE
Here we use a Wizard to create a programme that reads the
database and allows us to scroll through it.
Start by designing the database below:
9/30/2013 Prepared By L. mutanu 4
-
7/27/2019 IST 2020 CH 12
5/32
CH 12 WORKING WITH DATABASES
Open a new windows application and
click on and click on the data
sources tab at the bottom of thesolution explorer
Click on add new database
Select the database option and clickon next
9/30/2013 Prepared By L. Mutanu 5
\
-
7/27/2019 IST 2020 CH 12
6/32
CH 12 WORKING WITH DATABASES
We need to select the data
connection object. Click on new
connection.
The Microsoft Access Database
File (OLE DB) data source
appears by default, this is for Ms
Access 2003. Click on change.
The object for Ms Access 2007
will be under the options shown
below
9/30/2013 Prepared By L. mutanu 6
-
7/27/2019 IST 2020 CH 12
7/32
CH 12 WORKING WITH DATABASES
Select the following OLE DB
provider. This is the engine for
MS Access 2007.
Write the full path of the file
and the file name.
Test connection If you get the
message below then you can
proceed, otherwise fix the
problem first.
9/30/2013 Prepared By L. mutanu 7
-
7/27/2019 IST 2020 CH 12
8/32
CH 12 WORKING WITH DATABASES
The connection has been
created. We now need to save
the connection. Give your
connection string any nameyou like
Select the objects you would
like added to your dataset. In
our example we only need the
two fields ENO & ENAME
under table employee.
Give the dataset a name
9/30/2013 Prepared By L. mutanu 8
-
7/27/2019 IST 2020 CH 12
9/32
CH 12 WORKING WITH DATABASES
When you are returned to your form, you should notice the Solution
Explorer has added your a Data Set.
Your data set will be your source of data for the application.
The dataset in turn gets data from the database
To add a Field to your Form, click on one in the list. Hold down your leftmouse button, and drag it over to your form.
9/30/2013 Prepared By L. mutanu 9
-
7/27/2019 IST 2020 CH 12
10/32
CH 12 WORKING WITH DATABASES
Notice the IDE automatically creates: Labels & Textboxes To display he data
A Binding Source links a textbox to the
correct field
A navigation bar To help you scroll
through the data
A data adapter To populate the datasetwith data from the database
9/30/2013 Prepared By L. mutanu 10
Run your program!
-
7/27/2019 IST 2020 CH 12
11/32
CH 12 WORKING WITH DATABASES
While the IDE is easy, you are limited to making use of what it gives
you.
Often you want to customize your application to do a little more.
Writing your own database code then becomes a better option.
We now look at how to write our own code
9/30/2013 Prepared By L. mutanu 11
-
7/27/2019 IST 2020 CH 12
12/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 12
NB: Design a database before attempting to connect to it.
12.5 Write your own Database code
-
7/27/2019 IST 2020 CH 12
13/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 13
Instead of using the IDE you can write your own DB code. In thisUnit we look at how to write your own code.
The aim is to gain a deeper understanding of programmingconcepts in order to understand what happens behind the scenes.
You will have to write code for the following tasks that were
discussed earlier:
1. Connection
2. Data Set
3. Data Adapter
4. Commands
-
7/27/2019 IST 2020 CH 12
14/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 14
12.6 Design the Interface
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
15/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 15
12.7 Declare Required Components
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
16/32
CH 12 WORKING WITH DATABASES
7. Write code to populate the text boxes with data from the data set
12.8 Display Data from your Data base
Data BaseData SetApplication Data Adapter
Commands
1. Specify the database application and the database
2. Open the connection
Connection
3. Write commands (SQL Statements) specifying what to read
4.Tell the adapterthe database connection and the commands required
5. Tell the adapterto populate the data set
6. Close the connection
Data BaseData SetApplication Data Adapter
For Connection
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
17/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 17
Write the following code in the click event for the display button
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
18/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 18
12.9 Navigating the Data Set
We need to write code for the Back and Next buttons. Since we are
manipulating the data set we do not need the data adapter this time. We need to know the number of records in the data set so that we
can inform the user when they reach the end. A variable can becreated for this (We can call it maxrow).
We also need a variable to help us know the record we are currentlyon (We can call it inc).
The following steps apply:1. Declare the two variables
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
19/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 19
2. Initialize the variables.
We need to have connected to the database before attempting to
navigate.
Since the display event did this for us we can avoid duplicating the
connection code by forcing the user to click on display before
attempting to navigate. This can be achieved by disabling all otherbuttons except the display button when the form loads. Thus the
following code should be written for the form load event.
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
20/32
CH 12 WORKING WITH DATABASES
Initializing of our variables should then be done within the display
event as follows
9/30/2013 Prepared By L. mutanu 20
Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Employee.accdb
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
21/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 21
3. Write Code for the Buttons Click Events
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
22/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 22
12.10 Editing a record
When a record is edited the database needs to be updated and as a
result we need the data adapterto help us update it. Additionally we need a command builderevery time we need to
write to the database. Command Builder will build a SQL string foryou.
Steps:
1. Create a command builder
2. Write the changes on the controls to the data set3. Write the data in the data set to the database
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
23/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 23
12.11 Writing data to a Database
To Do this:
1. Clear the Controls
In our example the button Add will be used to clear the controls
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
24/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 24
2. Create a command builder
3. Create a new row
4. Add data from controls to new row
5. Add new row to data set
6. Write the data to a database
In our example we will use the save button to do the above
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
25/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 25
Write the following code for the save button
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
26/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 26
12.12 Deleting a record from the data base
Again we need to make changes to the data base hence we will
require the data adapter and the command builder Steps:
1. Create the command builder
2. Delete the row
3. Change to focus to another row e.g. the first row
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
27/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 27
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
28/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 28
12.13 Sequencing Procedures
Notice if you attempt to edit an empty record you will get an error!
Some task need to be done in a specific order. You can disable controls to ensure that the user does not do
something that is not expected.
For example the following code can be inserted in the form load
event to ensure that the user can only click on display command first
before doing anything else.
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
29/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 29
You must however remember to enable the buttons within the
display buttons click event, otherwise the user will not be able to
click anything else within the program.
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
30/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 30
Exercise I
Design the database below:
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
31/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L. mutanu 31
Design the interface below and write code to implement theapplication
CH 12 WORKING WITH DATABASES
-
7/27/2019 IST 2020 CH 12
32/32
CH 12 WORKING WITH DATABASES
9/30/2013 Prepared By L mutanu 32
Exercise II
Design the system below