1. Technology

Using Files as a Database

Need a "quick and dirty" database? Try this!

By

The previous article in this two part series, Coding an XML Configuration File, explained how to create a simple configuration file using XML and the System.Xml namespace. There are a lot of ways to get things done with .NET and choosing the one that's right for your system is one of the problems. The technique demonstrated is about as basic as you can get, but it's certainly not the only way to go.

The application used to illustrate the article was a simple desktop "Stock Calculation" utility that made it easy to calculate share cost and quantity information when a stock market transaction is being considered. The utility got initial configuration data from an XML file and had a single button that could be used to change the configuration data using values from the form components. That works "OK" but a lot of applications, including this one, need the ability to select from different sets of configuration data to populate the form. In this case, it would be very handy to be able to select from a list of companies using their ticker symbol codes and save cost and quantity information for each company.

Typically, that's a database application. But a database isn't the only way to get the job done. You might not have a database installed on your computer. Or you might not want to take the trouble of setting one up for the relatively simple requirements of this program.

Another way to do it would be to use an XML file that includes more information and multiple nodes with the information. For example, this XML would work:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  <StockCalcValues>
    <StockCalcValue stockSymbol="ABCD">
      <companyName>ABCD Industries</companyName>
      <buyPrice>10.64</buyPrice>
      <sellPrice>10.99</sellPrice>
      <numShares incByThousand="True">2000</numShares>
      <profitAmount>1000</profitAmount>
    </StockCalcValue>
    <StockCalcValue stockSymbol="PQRS">
      <companyName>PQRS Incorporated</companyName>
      <buyPrice>20.33</buyPrice>
      <sellPrice>21.55</sellPrice>
      <numShares incByThousand="True">2500</numShares>
      <profitAmount>3050.00</profitAmount>
    </StockCalcValue>
  </StockCalcValues>

If you're looking for faster processing time or perhaps you need all of the data to be in one file, this might be the best design for you. You would probably want to use XML DOM (Document Object Model) methods to process this file.

I didn't use this design, however. In our case, we're starting with something that already works using the System.Xml XmlTextReader, and XmlTextWriter objects. These objects, although fast and efficient, just give you forward-only access to XML data. Updating just one node in an XML file means rewriting the entire file. You also have to navigate to the right node using XML methods to accomplish the update which requires a fairly good knowledge of the XML methods.

Another possibility is to simply add to the code from the previous program and instead of having multiple XML nodes in one file, use multiple XML files in one directory. This allows exactly the same XML reader and writer methods to be reused. It has the additional benefit of giving you files that you can also maintain by simply adding or deleting in Windows if you decide to. The processing isn't as fast as it would be with a single file or database, but if you're just maintaining a dozen or so files on a single computer, this isn't a problem. This is the design I used.

If you want to follow along with the changes to our example StockCalc program, you can download the starting application here. The completed application download is at the end of the article.

Visual Basic was the original RAD (Rapid Application Design) system. The RAD concept was that a lot of applications don't have complex architectural requirements so you can simply design through the development GUI by drawing the new features you want on a form and adding the code to make them work.

Here's a "before" and "after" snapshot of the changes that will be made to our form. The main change is the addition of a ListBox component that will hold the stock symbols for the companies along with buttons to let us manage them and a text box for the one that is currently displayed. The old "Update Defaults" button has been deleted and a few other minor changes that I just decided would work better have been added.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The new and revised code that will have to be added to the program is explained on the next page.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Using Files as a Database

©2014 About.com. All rights reserved.