Home

File-Based Databases

 

A File-Based Application

 

Introduction to Databases

A computer database can be created using almost any application that can be used to store text. Because databases have become a valuable means of holding company information, specialized software products have been developed to make it possible to store large and complex pieces of information.

A file-based database (also called a flat file database) is an application that stores one or various lists of information in regular, traditional text-based files.

 

Creating a File-Based Application

To create a file-based application, you can use a simple or complex text editor such as Notepad. The main action from you is to create the list(s) and save it(them) as text files using either the txt or any extension of your choice. After creating a list, when necessary, you can open it to access and optionally change the values. After making the changes, you can save the file. In the same way, you can print the values and do any type of file-related operation that you judge necessary.

One of the disadvantages of using a text editor to create a database is that, if you decide to distribute it, either your users should know as much as necessary about the file (its location and its content, just to name a few) you must provide a mechanical means of accessing the file(s). To make your product more professional and user friendly, you can create a graphical application that the user would use to access the values in the database. With this approach, you can create an attractive graphical user interface (GUI) object that display a functionality the user is familiar with. Using such an aesthetic interface, you can provide the means of adding, editing, deleting, or changing the values of the database.

To create a file-based application, you can use the C# language or the Microsoft Visual C# programming environment that, in combination with the .NET Framework, provides all the tools you would need.

Using Windows Controls and Accessories

To create an aesthetically user-friendly application, you can use the various Windows controls that are implemented in the .NET Framework. The controls are varied and are meant to accomplish different roles:

  • The form is the most fundamental control. As a container, in fact the main container, it is the primary object that holds the other controls of an application
  • Based on its fundamental role in an application, a form can be equipped with functional accessories placed on its frame and intended to assist with file or print operations. These accessories are the menu(s), the toolbar(s), and the status bar
  • A dialog box is a special type of form you create to perform the same actions as a form but it does not use the accessories and it may show a different type of border
  • On top of the form or the dialog box, there are controls, referred to as containers, that can be used to carry or hold other controls. These are the property sheet, the group box, or the panel
  • Text-based controls are used to receive or display text. These are the label, the text box, the masked text box, and the link label
  • Some text-based controls can display enhanced or formatted text beyond the functionality of the traditional text box. Besides the formatting and the characteristics of their paragraph, they may also display pictures. These controls are the rich text box and the web browser
  • Button-based controls allow the user to perform an action when clicked. These controls are the command button, the radio button, and the check box
  • List-based controls display a list of items to the user. This type includes the list box, the combo box, the tree view, the list view, and the domain up/down
  • Date and time-based controls are specialized to handle date and time values. The controls in this category are the calendar and the date/time picker
  • There are other controls used to show a value (the numeric up-down control) or a progress (the progress bar), etc

The Directory of a Database

 

Introduction

One of the biggest differences between a database and a regular application is that, traditionally, although not always, all of the files of a database are located in the same directory. The directory can be local and accessed only by one computer. The directory can be located in one computer and accessed by various users on different computers or a workgroup. The directory can be located on a server that no user directly uses but that directory's files can be accessed from one or more computers.

Another particularity of a database is that usually you, the database developer, create and manage the directory or directories used by the application.

Another difference of a database as compared to a regular application is that, while using the database, users do not create files. This means that there is no actual file processing on the part of the users. For example, the user does not even open the database in the traditional sense. You, the database developer, provides a means of accessing the database. Then, the user adds, edits, or deletes values.

Creating a Directory

Based on the above discussion of directories, when creating a file-based application, one the first actions you should perform consists of setting up the directory where the file(s) of your application would be located. If you already know (and you should know) where and how the application would be accessed, you can manually create a folder using Windows Explorer, My Computer, or any appropriate utility. Otherwise, you can still programmatically create the directory.

The .NET Framework supports the creation and management of directories through various classes. The main class used to deal with directories is called Directory. Besides the Directory class, the .NET Framework provides support for folders through a class named DirectoryInfo. To use it, declare a variable of type DirectoryInfo using its constructor to initialize it.

To actually create a directory using the static Directory class, you can call its CreateDirectory() method that is overloaded with two versions. Here is an example:

Imports System.IO

Public Class Exercise

    Private Sub btnDirectory_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnDirectory.Click
        Directory.CreateDirectory("E:\Bethesda Car Rental")
    End Sub
End Class

To create a folder using the DirectoryInfo class, call its Create() method that comes in two versions. Here is an example:

Private Sub btnDirectory_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnDirectory.Click
    Dim DirInfo As DirectoryInfo = New DirectoryInfo("E:\Bethesda Car Rental")
    DirInfo.Create()
End Sub

When you call either the Directory.CreateDirectory() method or the DirectoryInfo.Create() method, if the directory does not exist, it would be created. If the directory exists already, nothing would happen. This implies that the compiler would not attempt to create a directory if there is already one in the indicated location and you can safely call any of these methods without the risk of deleting its existing files, if any.

Before performing any operation on a directory, you should first make sure it exists. To get this information, you can call the Directory.Exists() method that returns a Boolean value. This method takes as argument the path to the directory. Here is an example:

Private Sub btnDirectory_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnDirectory.Click
        If Directory.Exists("E:\Bethesda Car Rental") Then
            MsgBox("The directory exists already")
        Else
            MsgBox("That directory was not yet created")
        End If
End Sub

During the lifetime of your database, at one time, you may want to change its location, for any reason you judge necessary. Although this operation is primarily easily supported, it could become complex in some scenarios. Still, to move a directory and its contents, you can call the Directory.Move() method. This method takes two arguments: the source and the destination. After the method has been called, the directory held by the first argument would be moved, along with its sub-folders and files, to the path specified by the second argument. To move a directory using the DirectoryInfo class, you can call its MoveTo() method.

As opposed to creating a directory, if you don't need it anymore, you can remove it. To support this, the Directory class is equipped with the Delete() method that is overloaded with two versions. One of the versions is used to delete a directory that is currently empty while the other version is used to delete the directory and its content.

The Files of a File-Based Application

 

Introduction

As its name indicates, a file-base application uses one or more files to hold its information. If you decide to create the application using the C# language, you can take advantage of the .NET Framework rich library and its support for file processing.

In the .NET Framework, file processing is primarily supported through the System.IO namespace that is filled with various classes to deal with files and directories (folders). The most fundamental class of the System.IO namespace and used to perform file processing is called File. The abstract and sealed File class contains all necessary methods used to create a file, check the existence of a file, write information to a file, read information from a file, or manipulate the system attributes of a file.

Another one of the fundamental file processing classes is called Stream. This is mainly an abstract class that lays a foundation for other stream-oriented classes. One of the classes that derives from Stream is called FileStream.

Creating a File

To create a new file, you can use the File class, call one of the versions of its Create() method that takes an argument as the name of, or the path to, the file and returns a FileStream object.

Besides File, you can use the StreamWriter class to create a file. To do this, declare a variable of type StreamWriter and initialize it using one of its constructors.

Writing to a File

One of the most routine operations performed on a class consists of writing information to it. And one of the most useful classes in this domain is called StreamWriter. The StreamWriter class is derived from the TextWriter class. To create a file using the StreamWriter class, you can declare a StreamWriter variable and initialize it using one of its constructors. After creating the file, you can write information to it by calling the Write() or the WriteLine() method. Always make sure you close the stream after using it. Also make sure you use exception handling in your code.

Here is an example:

File Processing

Imports System.IO

Public Class Exercise

    
    Private Sub txtLastName_Leave(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles txtLastName.Leave
        Dim strInitials As String = txtFirstName.Text.Substring(0, 1) & _
                                    txtLastName.Text.Substring(0, 1)
        txtSave.Text = strInitials
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnSave.Click
        Dim stmWrite As StreamWriter = New StreamWriter(txtSave.Text)

        stmWrite.WriteLine(txtFirstName.Text)
        stmWrite.WriteLine(txtLastName.Text)
        stmWrite.WriteLine(dtpDateHired.Value)
        stmWrite.WriteLine(cbxGenders.Text)
        stmWrite.WriteLine(txtHourlySalary.Text)

        stmWrite.Close()

        txtFirstName.Text = ""
        txtLastName.Text = ""
        dtpDateHired.Value = DateTime.Today
        cbxGenders.Text = "Unknown"
        txtHourlySalary.Text = "0.00"
        txtSave.Text = ""
        txtOpen.Text = ""
    End Sub
End Class

Besides StreamWriter, to create a file and write information to it, you can use the BinaryWriter class. You start by declaring a BinaryWriter variable and initialize it using one of its constructors, passing a Stream-based object.

Reading From a File

Before exploring the contents of a file, you must first open. To open a file using the File class, you can call its Open method that is overloaded with three versions. If the information in the file is raw text, you can call the OpenText() method. After opening a file, you can read its content.

To support the ability to read from a file, you can use the StreamReader class. The StreamReader class is derived from the TextReader class. When using it, you can start by opening the file. To do this, declare a variable of type StreamReader and use one of its constructor to specify the name of, or the path to, the file. To read information from the file, you can call its Read() or its ReadLine() method. Here is an example:

Private Sub btnOpen_Click(ByVal sender As Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnOpen.Click
        Dim stmReader As StreamReader = New StreamReader(txtOpen.Text)

        txtFirstName.Text = stmReader.ReadLine()
        txtLastName.Text = stmReader.ReadLine()
        dtpDateHired.Value = DateTime.Parse(stmReader.ReadLine())
        cbxGenders.Text = stmReader.ReadLine()
        txtHourlySalary.Text = stmReader.ReadLine()

        stmReader.Close()
End Sub

Instead of StreamReader, you can use the BinaryReader class to read information from a file.

Practical Learning: Reading From a File

  1. In the Class Name combo box, select mnuFileOpen
  2. In the Method Name combo box, select Click and implement the event as follows:
     
  3. Execute the application
  4. Type 1001 for the receipt number and, on the main menu of the form, click File -> Open
  5. Close the form and return to your programming environment

Saving and Opening Data

We have mentioned that, on a typical database, the user is not aware of opening or saving files. In the same way, the user can be spared with deciding when to save and when not to save data. Whenever possible, most operations should be performed behind-the-scenes with little to no intervention from the user. To make this possible, you are in charge of creating the file(s), receiving data from the user, and then adding that data to the file.

Data Input and Output

 

Introduction

Data input, also referred to as data entry, consists of entering the values into the application. The user does it mainly using the keyboard and the mouse. As we reviewed the Windows controls, there are various types of objects you can use to assist the user. One of the suggestions you should follow is that you should make the user's job as easy as you can. Because users of a database are not expected to do any heavy word processing. This means that typing speed is not among their strongest points. Instead, when choosing the Windows controls for your application, you should select the most appropriate one for a particular piece of information.

Printing

Data output consists of retrieving information from a database. Besides opening the objects, such as the forms, that hold the information of a database, users also regular want to print. In fact, in some businesses, the customers require to have a printed copy of their transaction. Therefore, if you are in the habit of neglecting to configuring printing in your Windows applications, for a database, you should (strongly) loose the habit and provide your users with the ability to print the data of your application.

If you are creating a file-based application, you can use the various printing classes of the .NET Framework. Unfortunately, there is no environment inside the Microsoft Visual Studio 2005 that can assist you to visually design a report. You must manually draw everything.

 

Home Copyright © 2008-2016, FunctionX, Inc.