Introduction to Databases |
|
In Lesson 1, to get a list of items, we created
and stored it in a variable-based list. The user had no way of storing items in
the computer. This meant that once the computer was shut down or the application was closed, the
information created while using the application was lost. In list-oriented business applications, to make sure that
the same information is not entered over and over again, any new information can
be saved and stored somewhere. Such a type of file-based application can be used
to create new items, save them, and process them the next time they are needed.
This is the basis of databases. A database is a information created as a list and stored somewhere so the list can be better
managed. This means that the person using the database should be able to add
information to the database, to retrieve information from the database, to
change the information stored in the database, to manipulate it, to manage
the database as an ensemble, and to save new or changed information.
There are various types of databases used with different
goals. The .NET Framework provides various means of creating and managing such
various types of databases. Those we will review are categorized as follows:
- File-Based Databases: A database is referred to as file-based, sometimes called a
flat file database, when it uses the regular techniques of file processing. In
other words, its information is stored in one or more regular Windows files
with a familiar or unfamiliar file extension. For example, you can create
text files (files that have the .txt extension), use them to save
information, then open those files when needed and process them as regularly
as you would a normal text file. In the same way, you can use any other file
extension of your choice to store the information of a file. With a
file-based database, you decide what extension(s) your file(s) would have
and how to use it. You would be completely in charge and you can prevent
other applications from using or accessing the files.
Advantages: File-based databases can be considered the easiest to create because
they rely on normal techniques of file processing. Another positive point is
that, because the .NET Framework provides an impressive support for file
processing, file-based databases are easy to create.
Disadvantages: File-based databases are not relation-oriented. Because the
information is stored in regular Windows files, you must constantly remember
where a particular piece of information is stored. Since information is
stored in distinguished files, a file can also be corrupted if not used or stored
safely
File-based databases will be the subject of this lesson
- XML-Based Databases: XML is a technique of describing data. By using XML, you can
create information and save it in one or more files that can be accessed by
any programming environment that can read and manipulate XML. Normally, the
files of this type of database have the .xml extension and they can be
universally used. If you use the .NET Framework to create your file(s), you
would benefit from an impressive library that is highly XML-oriented but the
files as normal XML entities can be accessed by any other application.
Advantages: XML files are the easiest files to create, easier than objects of
file-based databases. An XML file can be created from one application by a
certain person and the same file can be manipulated by another person using
a completely different application without any danger of corrupting the
file. This also means that different people and different applications from
different operation systems can access the same XML file and manipulate it
at will.
Disadvantages: Because XML has standard rules, you must first study XML before
being able to create valid XML files. Because an XML file is a regular
text-based database, an informed user can open it with any text editor and
possibly corrupt it.
We studied XML in the previous lessons. Starting here and in future lessons,
we will further use XML
- Relational Databases: This is the default concept of a database. A
relational database is an group of data-oriented objects (mostly tables)
that act as an ensemble so information can flow from one object to another
using predefined relationships.
Advantages: Relational databases provide the ideal techniques of creating, storing,
and manipulating information. The relationships among objects are created
inside of the objects (tables) that make up the database, then the database engine ensures that information flows from one object to another. This
reduces many mistakes that would be produced during data entry and that
result in duplicated information
Disadvantages: Relational databases have many rules that you, the database developer,
should learn and master. Besides studying the techniques of creating a
database using a library such as the .NET Framework, you would also need to
learn an additional language called SQL
Introduction to File-Based Databases |
|
As mentioned above, a file-based database consists of
creating one or more files that can be used as a database. To do this, you can
create an array of a list of objects as we learned in Lesson 1. The problem
with arrays is that they have a fixed size and you must know from the beginning
the number of items that can created in the list. The alternative is to use a
linked-list. As seen in Lesson 1, this is easily supported through
the ArrayList class.
Practical
Learning: Introducing File-Based Databases
|
|
- Start a new Windows Forms Application named DepartmentStore3
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- Set the Name to NewStoreItem and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
Label |
|
Item #: |
|
TextBox |
txtItemNumber |
|
|
Label |
|
Item Name: |
|
TextBox |
txtItemName |
|
|
Label |
|
Size: |
|
TextBox |
txtSize |
|
|
Label |
|
Unit Price: |
|
TextBox |
txtUnitPrice |
|
AlignText: Right |
Button |
btnAddItem |
Add Item |
|
Button |
btnClose |
Close |
|
Form |
|
|
AcceptButton: btnAddItem
MaximizeBox: False
StartPosition: CenterScreen |
|
- Arrange the Tab Order (View -> Tab Order) so the Item # text box is the
last in the sequence:
- Double-click an unoccupied area of the form and implement its Click event
as follows:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace DepartmentStore3
{
. . . No Change
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container* components;
ArrayList *lstStoreItems;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
. . . No Change
}
private: System::Void NewStoreItem_Load(System::Object * sender, _
System::EventArgs * e)
{
lstStoreItems = new ArrayList;
}
};
}
|
- Return to the New Store Item form and double-click its Close button
- Implement the event as follows:
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
- Display the first form (Form1.h [Design])
- Add a button to it with the following properties:
Text: New Store Item
Name: btnNewStoreItem
- Double-click the New Store Item button and implement its Click event as
follows:
#pragma once
#include "NewStoreItem.h"
namespace DepartmentStore3
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
. . . No Change
private: System::Void btnNewStoreItem_Click(System::Object * sender, _
System::EventArgs * e)
{
NewStoreItem *frmNewItem = new NewStoreItem();
frmNewItem->Show();
}
};
}
|
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- Set the Name to StoreInventory and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
DataGrid |
|
|
Auto Format: Colorful 3 |
Button |
bntLoad |
Load |
|
Button |
btnClose |
Close |
|
Form |
|
|
AcceptButton: bntLoad
MaximizeBox: False
StartPosition: CenterScreen |
|
- Double-click the Close button and
implement its Click event as follows:
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
- Display the first form (Form1.h [Design])
- Add a button to it with the following properties:
Text: Store Inventory
Name: btnStoreInventory
- Double-click the Store Inventory button
- In the top section of the file, under the other #include line, type #include
"StoreInventory.h"
- Implement its Click event as follows:
System::Void btnStoreInventory_Click(System::Object * sender, _
System::EventArgs * e)
{
StoreInventory *frmInventory = new StoreInventory();
frmInventory->Show();
}
|
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET) if necessary.
Set the Name to CustomerOrder and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
Label |
|
Item # |
|
Label |
|
Description |
|
Label |
|
Size |
|
Label |
|
Unit Price |
|
Label |
|
Qty |
|
Label |
|
Sub-Total |
|
TextBox |
txtItemNumber1 |
|
|
TextBox |
txtDescription1 |
|
|
TextBox |
txtSize1 |
|
|
TextBox |
txtUnitPrice1 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity1 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal1 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber2 |
|
|
TextBox |
txtDescription2 |
|
|
TextBox |
txtSize2 |
|
|
TextBox |
txtUnitPrice2 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity2 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal2 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber3 |
|
|
TextBox |
txtDescription3 |
|
|
TextBox |
txtSize3 |
|
|
TextBox |
txtUnitPrice3 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity3 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal3 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber4 |
|
|
TextBox |
txtDescription4 |
|
|
TextBox |
txtSize4 |
|
|
TextBox |
txtUnitPrice4 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity4 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal4 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber5 |
|
|
TextBox |
txtDescription5 |
|
|
TextBox |
txtSize5 |
|
|
TextBox |
txtUnitPrice5 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity5 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal5 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber6 |
|
|
TextBox |
txtDescription6 |
|
|
TextBox |
txtSize6 |
|
|
TextBox |
txtUnitPrice6 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity6 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal6 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber7 |
|
|
TextBox |
txtDescription7 |
|
|
TextBox |
txtSize7 |
|
|
TextBox |
txtUnitPrice7 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity7 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal7 |
0.00 |
TextAlign: Right |
TextBox |
txtItemNumber8 |
|
|
TextBox |
txtDescription8 |
|
|
TextBox |
txtSize8 |
|
|
TextBox |
txtUnitPrice8 |
0.00 |
TextAlign: Right |
TextBox |
txtQuantity8 |
0 |
TextAlign: Right |
TextBox |
txtSubTotal8 |
0.00 |
TextAlign: Right |
Label |
|
This order will be saved in |
|
DateTimePicker |
dtpSaleDate |
|
Format: Custom
CustomFormat: ddd dd MMM yyyy |
Label |
|
Total Order: |
|
TextBox |
txtTotalOrder |
0.00 |
TextAlign: Right |
Button |
btnSave |
Save and Start a New Order |
|
Button |
btnClose |
Close |
|
|
- Double-click the Close button and implement its Click event as follows:
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
- Display the first form (Form1.h [Design])
- Add a button to it with the following properties:
Text: New Customer Order
Name: btnNewOrder
- Double-click the New Customer Order button
- In the top section of the form, type #include "CustomerOrder.h"
- Implement its Click event as
follows:
System::Void btnNewOrder_Click(System::Object * sender, System::EventArgs * e)
{
CustomerOrder*frmOrder = new CustomerOrder();
frmOrder ->Show();
}
|
- To add a new form, on the main menu, click Project -> Add New Item...
- In the Templates list of the Add New Item dialog box, click Windows Forms
(.NET)
- Set the Name to DailySales and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
Label |
|
View Sales For |
|
DateTimePicker |
dtpSaleDate |
|
Format: Custom
CustomFormat: ddd dd MMM yyyy |
DataGrid |
|
|
Auto Format: Colorful 3 |
Button |
btnClose |
Close |
|
Form |
|
|
MaximizeBox: False
StartPosition: CenterScreen |
|
- Double-click the Close button and
implement its Click event as follows:
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
- Display the first form (Form1.h [Design])
- Add a button to it with the following properties:
Text: View Daily Sales
Name: btnDailySales
- Double-click the View Daily Sales button
- In the top section of the file, under the other #include line, type #include "DailySales.h"
- Implement its Click event as follows:
System::Void btnDailySales_Click(System::Object * sender, System::EventArgs * e)
{
DailySales *frmSales = new DailySales;
frmSales->Show();
}
|
- Return to the main form. Add a new button to it and set its properties as
follows:
Text: Close
Name: btnClose
- Double-click the Close button and implement its Click event as follows:
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}
|
- Save all
A file-based database is primarily a technique of storing values
in files so they can be retrieved when needed. Based on this, the files are
created like any other and they can have any extension of your choice. When a
file is created, its contents can be stored in a portable medium such as a hard disc, a floppy disc, a compact disc, or any valid
and supported type of storage.
File processing is support in the .NET Framework through the System::IO
namespace that contains many different classes to handle almost any type of file
operation you may need to perform.
The Application's Directory |
|
When creating a file-base application, you should know where the files are
located, even if you don't explicitly communicate this to the user. This means
that you may need to create a folder that would hold the files of your database.
In some cases you can create a new folder on the C: drive, under the the Program
Files folder or in the My Documents folder. In some other cases, you may use a
network shared folder as the repository of users files. These are decisions you
can make when planning the deployment.
Once the application has been installed, you can allow the user to directly save
the files to default or selected folder without being prompted to specify the
name or path of the file.
Before saving a file, you may first want to check its
existence. This can be done by calling the File::Exists() method. Once
you have decided to save a file, you can specify the type of operation using the
FileMode option of the Stream-based class you are using. The
options of the FileMode
are FileMode::Append, FileMode::Create, FileMode::CreateNew,
FileMode::Open, FileMode::OpenOrCreate, and FileMode::Truncate.
The FileAccess option allows you to specify the type of access the user
will need when saving the file. The options are FileAccess::Write, FileAccess::Read,
and FileAccess::ReadWrite. The FileShare allows you to decide
whether or how other users (actually, processes) can access the file while it is
being accessed. The options of the FileShare are FileShare::Inheritable,
FileShare::None, FileShare::Read, FileShare::Write, and FileShare::ReadWrite.
In traditional file processing supported by most compiled
languages such as C++, you learn to create values from primitive types (int,
double, char, etc) and save them to a medium. If you create your
own class in C++ and want to save information from its variable, you would have
to save one member variable at a time. This is not the easiest job to perform in
C++. Fortunately, many
libraries such as MFC, VCL, and the .NET Framework provide built-in classes you
can use to save a variable of your class as if the variable had been created
from a primitive data type.
Object serialization consists of saving the state of any
object as a whole. Serialization makes it
possible to easily create a file-based database since you are able to save and
restore the object the same way you would use file processing of primitive types
in C++.
The .NET Framework provides all means of serializing any
managed class through a concept referred to as binary serialization. To proceed
with object serialization, you must first specify the object you would use. As
with any other program, you can either use one of the classes built-in the .NET
Framework or you can use your own programmer-created class. If you decide to use
your own class, you can specify whether the whole class or just some parts of
the class can be saved.
Binary serialization consists of saving an object. This
object is usually a class that either you create or is provided through the .NET
Framework. For a class to be serializable, it must be marked with the Serializable
attribute. This means that, when checking the MSDN documentation, any class you
see with this attribute is already configured to be serialized. This is the case
for almost all list-oriented classes such as Array, ArrayList,
etc, including many of the classes we will use when we start studying ADO .NET
and relational databases.
If you create your own class and want to be able to save
values from its variables, you can (must) mark the class with the Serializable
attribute. To do this, type [Serializable] before starting to create the
class. Here is an example:
#pragma once
using namespace System;
[Serializable]
public __gc class CEmployee
{
public:
String *FirstName;
String *LastName;
DateTime DateHired;
Double Salary;
CEmployee()
{
this->FirstName = S"";
this->LastName = S"";
this->DateHired = DateTime::Now;
this->Salary = 0.00;
}
CEmployee(String *fn, String *ln, DateTime dte, Double sal)
{
this->FirstName = fn;
this->LastName = ln;
this->DateHired = dte;
this->Salary = sal;
}
};
The Serializable attribute informs the compiler that
values from the class can be serialized. When creating a serializable class, you
have the ability to specify whether all of the member variables of the class can
be serialized or just some of them.
|
|