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. |
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.
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:
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.
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.
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.
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.
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: 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.
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.
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, 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.
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. | |
|