August 01, 1997
The VSData Database EngineA single ActiveX control can increase database performanceMichiel de Bruijn
VideoSoft's VSData database engine is truly lightweight -- the entire engine is implemented as a single ActiveX control that doesn't rely on any proprietary support DLLs. Michiel uses the engine and Visual Basic to build a catalog application for his collection of MIDI files.
Michiel, a programmer who lives in Rotterdam, The Netherlands, can be contacted at mdbruijn@inter.nl.net.
If you're using a Microsoft programming language such as Visual Basic, you've no doubt considered using the Jet database engine to implement your data store. Although Jet is a great tool and supports a number of advanced features (like data replication), it also has a number of distinct disadvantages that can cripple your application. In this article, I'll show you an alternative to Jet for some projects.
So, what's the problem with Jet? After all, if it's good enough for three major releases of Microsoft Access, it should be just fine for you as well, right? Not necessarily. Adding the Jet engine to your application also means adding megabytes of DLLs. This will not only impact the size of your distribution, but also increase the amount of memory your program requires. This can reduce performance to a level that's no longer acceptable to users.
Apart from the performance issue, Jet is also complex. This can be an advantage at times, because there's not much you can't do with it. On the other hand, you probably will not need all of this advanced functionality all of the time. In cases like this, tasks that might seem easy at first, such as storing multimedia data, are not really that straightforward due to Jet's complexity. And because of the way Jet handles record locking, it's sometimes difficult to determine if the code you're writing is really multiuser safe.
This is not to suggest that you should always avoid Jet, only that it's important to consider your options. As always, use the right tools for the job. If your data-storage requirements are modest, or if you only want to store some configuration data, it's obviously a waste of resources to use a multimegabyte database package. Instead, keep your program's configuration options in the Registry, and use a lightweight tool to store your data records. The advantages of this approach include a clean programming interface with (hopefully) few surprises, as well as optimal performance due to a minimal impact on the resources of your target system.
Implementing a Lightweight Data Store
Using a data store that is fast and efficient may sound like a great idea, but how easy is it to implement such a solution? After all, if you have to spend several weeks writing your own database engine, this alternative may be far more troublesome than using Jet. However, if you don't expect to store too many records and don't require any fancy features, you might be surprised by how efficient a do-it-yourself database engine can be.
For more demanding situations, or when you just don't want to spend extra time coding, you might consider a third-party database control such as VideoSoft's VSData. Its most appealing feature is that it is truly lightweight -- the entire database engine is implemented as a single ActiveX control that doesn't rely on any proprietary support DLLs. The control adds only 160 KB to your distribution, but it's by no means lacking in functionality.
Adding an index, for example, is possible at any moment using any field with a fixed-size data type. Performance is more than acceptable; in fact, record sorting is fast even if you're storing large chunks of data in your database. VSData includes special capabilities for file storage, allowing you to embed multiple files in records without a speed penalty. This enables easy storage and retrieval of multimedia data, like .AVI and .WAV files. Also, unlike some xBase-oriented add-on tools, VSData supports the familiar FindFirst/FindNext methods, which makes searching for data as easy as it is with Jet.
Support for automatic file locking on networks is also implemented in VSData, assuring data integrity when more than one user is trying to edit the same record. Basic database security is provided, allowing you to prevent users from adding records to a database, replacing data that's already present, or writing to the database at all. Also, if you embed Internet URLs in your records, VSData will optionally load the user's web browser to display the data at that location.
A VSData Application
In my examination of VSData, I built a catalog application in Visual Basic for my collection of MIDI files. (A demo version of the engine is available at VideoSoft's web site at http://www.videosoft.com/. The complete project is available electronically, see "Programmer's services," page 3. This file also includes the shareware/demo version of VSData.) The first step is, of course, to design the layout of the database. You can do this either through the VSData Manager utility (VideoSoft supplies its source code with the control), or from within your program. Creating the sample database in Figure 1 is programmatically straightforward, requiring only the single statement in Example 1. Note that you only have to specify the layout of the database file -- in this case, three string fields, an integer field, file, web address, date fields, and another string -- and that you don't have to specify any field names. VSData doesn't care about field names and lets you refer to fields through the Field() array. If you want to, you can assign specific names to fields by using the FieldNames() array. Databases that you create using the Manager utility will have an associated description file.
Once the database has been created, data entry works comparably to data entry with VB's native data control. The only difference is that you can't bind VB controls to a VSData control, and you'll have to write some additional code to handle these bindings yourself. As the update code in Example 2 shows, this doesn't add much complexity.
In this example, each field in the VSData database corresponds to an element in a Visual Basic control array of text boxes. You might expect the real code (see Listings One and Two) to contain some special cases for saving the file data, but the only actual exceptions made here are to handle the date field, which VSData expects to be a Variant of type 7 (vbDate), and the combo box; see Figure 2. The combo box needs special treatment because Visual Basic won't allow you to have a different control type in the same control array; otherwise, a simple check for the control class would have been sufficient. Storing the MIDI file used in the example is as simple as assigning its full path name to the appropriate field array variable. The VSData control takes care of the rest. If you're worried about the resulting update and retrieval performance, don't. Even if you're not using any indexing (as in this example) and not storing large data files, record access is almost instantaneous.
When you retrieve a record containing a field of type File from the database, VSData will, at first, return only the string 'FILE' to you. However, you can use the ExtractFile method to retrieve the original file specified when creating or updating the record. This poses one small problem, though: If you read a record from the database and want to write it back, VSData will complain because it can't locate the file 'FILE' (which is the value of MIDIfile field). To prevent the original filename from getting lost in this way, you'll have to store it in an additional database field and put it back at an appropriate moment -- when reading the record data, for example. This is the purpose of the MIDIfileName field in our example database: It keeps a "backup copy" of the filename that was entered in the MIDIfile field originally.
Once all the data is entered in the database, you need to be able to get it out again. Adding navigation buttons to the form is trivial: Invoking the MovePrevious or MoveNext method in the corresponding event routine does the trick. To prevent users from trying to move past the beginning or end of the database, it's also a good idea to disable/enable the navigation buttons depending on the Beginning Of File (BOF) and End Of File (EOF) properties. This is only a cosmetic measure, though, as VSData will simply ignore record navigation commands that can't be executed.
Adding a find option is also easy. In the example presented here, it's only possible to search for certain song titles, so after asking the user for the desired search string (using an input box), you simply execute Example 3. By using the IN operator, it's also possible to search on partial titles. Implementing searches on multiple fields and using different operators, as well as allowing the user to navigate within result sets, is left as an exercise to the reader, but VSData supports all of this.
The only thing that's left to do to wrap up the sample application is to use the special data (the MIDI file and the URL) stored in the database. VSData provides all the code needed to launch the user's web browser and go to the site stored in the WebSite field: A simple VSData1.MediaPlay "WebSite" is sufficient. Sound and video clips (.WAV and .AVI files, respectively) can be played back with equal ease. You also have some control over the appearance of the resulting Media Player window using the MediaLeft/Top, MediaParent, MediaVisible, and MediaZoom properties. If you want to provide a friendly way of browsing for files at data-entry time, simply use the SetMediaField method.
Conclusion
VSData is a versatile control. In addition, with its small size, impressive speed, and multiuser capabilities, it might be a good replacement for your current database engine, especially if you store files and multimedia data in your databases. Keep in mind, however, that a lightweight data control will not have all the capabilities of more-advanced tools. So, if you heavily depend on SQL-like queries, transactions, and the like, a Jet solution might very well be an optimal one: A smaller distribution size and a possible speed increase aren't worth the major rewrites and loss of functionality in such cases.
VideoSoft offers a demo version of VSData at http://www.videosoft.com/. If you want to discuss database design in general, you might want to visit the VBPJFORUM and the MSBASIC forums on CompuServe, or one of the Microsoft news groups at http://www.msnews.microsoft.com/.
DDJ
For More Information
|
|||||||||||||||||||||||||||||
|
RELATED ARTICLES
|
TOP 5 ARTICLES
No Top Articles.
|
|
DEPARTMENTS
|
||||||||||
|
|
|
|