Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / C#

Lightweight DB Framework

Rate me:
Please Sign up or sign in to vote.
3.63/5 (6 votes)
23 May 2006CPOL3 min read 40.9K   159   28  
Manage small database tables with lightweight and simple C# code. Best suited for PocketPC and Windows Mobile devices
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=windows-1255">
	<TITLE>The Code Project</TITLE>
	<META NAME="GENERATOR" CONTENT="OpenOffice.org 2.0  (Win32)">
	<META NAME="CREATED" CONTENT="20060522;21134096">
	<META NAME="CHANGEDBY" CONTENT="Arie Schwartzman">
	<META NAME="CHANGED" CONTENT="20060524;12223720">
	<!--------------------------------------------------------------------------->
	<!-- INTRODUCTION                                

 The Code Project article submission template (HTML version)

Using this template will help us post your article sooner. To use, just 
follow the 3 easy steps below:
 
     1. Fill in the article description details
     2. Add links to your images and downloads
     3. Include the main article text

That's all there is to it! All formatting will be done by our submission
scripts and style sheets. 

-->
	<!--------------------------------------------------------------------------->
	<!-- IGNORE THIS SECTION -->
	<!--------------------------------------------------------------------------->
	<!------------------------------- STEP 1      --------------------------->
	<!-- Fill in the details (CodeProject will reformat this section for you) -->
	<STYLE>
	<!--
		P { color: #000000; font-family: "Verdana", "Helvetica", "Arial", sans-serif; font-size: 10pt }
		H2 { color: #ff9900; font-family: "Verdana", "Helvetica", "Arial", sans-serif; font-size: 13pt }
		PRE { background: #fbedbb; border: none; padding: 0cm; color: #000000; font-family: "Courier New", "Courier", "mono"; font-size: 9pt; font-style: normal; font-weight: medium }
		DT { color: #000000; font-family: "Verdana", "Helvetica", "Arial", sans-serif; font-size: 10pt }
		A:link { text-decoration: none }
	-->
	</STYLE>
</HEAD>
<BODY LANG="" TEXT="#000000" BGCOLOR="#ffffff" DIR="LTR">
<PRE><SPAN LANG="he-IL">Title:       Ligthweight DB framework</SPAN>
<SPAN LANG="he-IL">Author:      Arie Schwartzman </SPAN>
<SPAN LANG="he-IL">Email:       arie@reflective.co.il</SPAN>
<SPAN LANG="he-IL">Environment: c#</SPAN>
<SPAN LANG="he-IL">Keywords:    Pocket PC, Windows Mobile, DB</SPAN>
<SPAN LANG="he-IL">Level:       Intermediate&quot;</SPAN>
<SPAN LANG="he-IL">Description: An article on something-or-other</SPAN>
<SPAN LANG="he-IL">Section      Miscellaneous</SPAN>
<SPAN LANG="he-IL">SubSection   General</SPAN></PRE>
<UL>
	<LI><P STYLE="margin-bottom: 0cm"><!------------------------------- STEP 2      ---------------------------><!-- Include download and sample image information. --><SPAN LANG="he-IL"><A HREF="Article_demo.zip">Download
	demo project - XXX Kb </A></SPAN>
	</P>
	<LI><P><SPAN LANG="he-IL"><A HREF="Article_src.zip">Download source
	- XXX Kb</A> </SPAN>
	</P>
</UL>
<P>&nbsp;</P>
<H2><!------------------------------- STEP 3      ---------------------------><!-- Add the article text. Please use simple formatting (<h2>, <p> etc) --><SPAN LANG="he-IL">Introduction</SPAN></H2>
<P><SPAN LANG="he-IL">When I had decied to write a small Shopping
list program (<A HREF="http://www.fridayshopping.com/">www.fridayshopping.com</A>)
for my own personal use, I palanned to use Microsoft's Compact
Framework for storing the data using XML serialization APIs.
Unfortunatly, I have descovered that there is no such API available
for the Compact Framework at the time. So since the amount of data I
was planning to store was small I have decided to develop my own very
simple database framework, instead of using some profesional DB
package that would be expensive and memory consuming. </SPAN>
</P>
<H2><SPAN LANG="he-IL">Storage Files</SPAN></H2>
<P><SPAN LANG="he-IL">Databases are stored as plain XML files. As
mentioned, Compact Framework does not provide support for XML
Serialization/Deserialization, so I had to implement a simple version
myself, using XML readers and writers. Each database table is stored
in a separate XML file. When database table is loaded, the file is
being parsed and read entirely into the memory. This makes is
suitible for relatively small databases. When the database is saved,
the entire memory content is being written back into a XML file.</SPAN></P>
<H2><SPAN LANG="he-IL">File Structure</SPAN></H2>
<DL>
	<DT STYLE="margin-bottom: 0.5cm"><SPAN LANG="he-IL">In order to be
	able to access a unique record, each record in a database is
	identified by a unique seqential key. The <FONT COLOR="#0033ff">NextKey</FONT>
	sequential key is stored at the top of the database file, so that
	each new record that is added will use it and increment it for the
	usage of the next record added. All other fileds are stored
	subsequently as regular XML elements. In the following example,
	there are two books in the database.</SPAN></DT></DL>
<PRE><SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">Books</FONT><FONT COLOR="#0000ff">&gt;</FONT>  </SPAN>
<SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">NextKey</FONT><FONT COLOR="#0000ff">&gt;</FONT>                       </SPAN>
  <SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;<STRONG>2</STRONG>&lt;/</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;</FONT>                     </SPAN>
<SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;/</FONT><FONT COLOR="#990000">NextKey</FONT><FONT COLOR="#0000ff">&gt;</FONT>                                   </SPAN>
<FONT COLOR="#0000ff">  <SPAN LANG="he-IL">&lt;</FONT><FONT COLOR="#990000">Book</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
    <SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">Key</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
  <STRONG><FONT COLOR="#ff0000">    </FONT></STRONG><SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;<STRONG>0</STRONG>&lt;/</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">    <SPAN LANG="he-IL">&lt;/</FONT><FONT COLOR="#990000">Key</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
   <SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">name</FONT><FONT COLOR="#0000ff">&gt;<STRONG>The Bible</STRONG>&lt;/</FONT><FONT COLOR="#990000">name</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#990000">   <SPAN LANG="he-IL">&lt;author</FONT><FONT COLOR="#0000ff">&gt;<STRONG>God</STRONG>&lt;/</FONT><FONT COLOR="#990000">author</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">   <SPAN LANG="he-IL">&lt;</FONT><FONT COLOR="#990000">isbn</FONT><FONT COLOR="#0000ff">&gt;<STRONG>0000000000</STRONG>&lt;/</FONT><FONT COLOR="#990000">isbn</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">  <SPAN LANG="he-IL">&lt;/</FONT><FONT COLOR="#990000">Book</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">  <SPAN LANG="he-IL">&lt;<FONT COLOR="#990000">Book</FONT>&gt;</SPAN></FONT>
    <SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">Key</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
  <STRONG><FONT COLOR="#ff0000">    </FONT></STRONG><SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;<STRONG>1</STRONG>&lt;/</FONT><FONT COLOR="#990000">value</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">    <SPAN LANG="he-IL">&lt;/</FONT><FONT COLOR="#990000">Key</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
   <SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;</FONT><FONT COLOR="#990000">name</FONT><FONT COLOR="#0000ff">&gt;<STRONG>The Da Vinci Code</STRONG>&lt;/</FONT><FONT COLOR="#990000">name</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#990000">   <SPAN LANG="he-IL">&lt;author</FONT><FONT COLOR="#0000ff">&gt;<STRONG>Dan Brown</STRONG>&lt;/</FONT><FONT COLOR="#990000">author</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">   <SPAN LANG="he-IL">&lt;</FONT><FONT COLOR="#990000">isbn</FONT><FONT COLOR="#0000ff">&gt;<B>0385504209</B>&lt;/</FONT><FONT COLOR="#990000">isbn</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN>
<FONT COLOR="#0000ff">  <SPAN LANG="he-IL">&lt;/<FONT COLOR="#990000">Book</FONT>&gt;</SPAN></FONT>

<SPAN LANG="he-IL"><FONT COLOR="#0000ff">&lt;/</FONT><FONT COLOR="#990000">Books</FONT><FONT COLOR="#0000ff">&gt;</FONT></SPAN></PRE><H2>
<SPAN LANG="he-IL">Define your database classes</SPAN></H2>
<P><SPAN LANG="he-IL">Each database is comprised from two classes. </SPAN>
</P>
<OL>
	<LI><P><SPAN LANG="he-IL">The class that represents each data
	record. It extends the <I>ItemBase</I> class. The main role of this
	class is to define record members and handle serialization /
	deserialization of data from and into the members. </SPAN>
	</P>
	<LI><P><SPAN LANG="he-IL">The class that represents the container of
	the database records. We use the <I>ListItemTypeAttribute</I> to
	define the class that is contained in this container. It extends the
	<I><FONT COLOR="#008080">ItemListBase</FONT></I> class. In most
	cases the class will not have any implementation. Here is a sample
	code that defines the &quot;Books&quot; database</SPAN></P>
</OL>
<PRE><FONT SIZE=2><SPAN LANG="he-IL"><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">class</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#008080">Book</FONT><FONT COLOR="#000000"> : </FONT><FONT COLOR="#008080">ItemBase</FONT></SPAN></FONT>
<SPAN LANG="he-IL">{</SPAN>
<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">string</FONT><FONT COLOR="#000000"> name;</FONT></FONT></SPAN>
<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">string</FONT><FONT COLOR="#000000"> author;</FONT></FONT></SPAN>
<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">string</FONT><FONT COLOR="#000000"> isbn;</FONT></FONT></SPAN>

<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> Book()</FONT></FONT></SPAN>
        <SPAN LANG="he-IL">{</SPAN>
        <SPAN LANG="he-IL">}</SPAN>
      
<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> Book(</FONT><FONT COLOR="#0000ff">string</FONT><FONT COLOR="#000000"> name)</FONT></FONT></SPAN>
        <SPAN LANG="he-IL">{</SPAN>
<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">this</FONT><FONT COLOR="#000000">.name = name;</FONT></FONT></SPAN>
        <SPAN LANG="he-IL">}</SPAN>

<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">protected</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">override</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">void</FONT><FONT COLOR="#000000"> SerializeMembers(System.Xml.</FONT><FONT COLOR="#008080">XmlTextWriter</FONT><FONT COLOR="#000000"> xmlWriter)</FONT></FONT></SPAN>
        <SPAN LANG="he-IL">{</SPAN>
<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">base</FONT><FONT COLOR="#000000">.SerializeMembers(xmlWriter);</FONT></FONT></SPAN>
            <SPAN LANG="he-IL"><FONT SIZE=2>xmlWriter.WriteElementString(</FONT><FONT COLOR="#800000">&quot;name&quot;</FONT>, name);</SPAN>
            <SPAN LANG="he-IL"><FONT SIZE=2>xmlWriter.WriteElementString(</FONT><FONT COLOR="#800000">&quot;author&quot;</FONT>, author);</SPAN>
            <SPAN LANG="he-IL"><FONT SIZE=2>xmlWriter.WriteElementString(</FONT><FONT COLOR="#800000">&quot;isbn&quot;</FONT>, isbn);</SPAN>
        <SPAN LANG="he-IL">}</SPAN>

<FONT COLOR="#000000">        </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">override</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">void</FONT><FONT COLOR="#000000"> DeserializeMembers(System.Xml.</FONT><FONT COLOR="#008080">XmlNodeReader</FONT><FONT COLOR="#000000"> xmlReader)</FONT></FONT></SPAN>
        <SPAN LANG="he-IL">{</SPAN>
<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">base</FONT><FONT COLOR="#000000">.DeserializeMembers(xmlReader);</FONT></FONT></SPAN>

<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">if</FONT><FONT COLOR="#000000"> (xmlReader.IsStartElement(</FONT><FONT COLOR="#800000">&quot;name&quot;</FONT><FONT COLOR="#000000">))</FONT></FONT></SPAN>
            <SPAN LANG="he-IL">{</SPAN>
                 <SPAN LANG="he-IL">name = xmlReader.ReadElementString();</SPAN>
            <SPAN LANG="he-IL">}</SPAN>
<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">if</FONT><FONT COLOR="#000000"> (xmlReader.IsStartElement(</FONT><FONT COLOR="#800000">&quot;author&quot;</FONT><FONT COLOR="#000000">))</FONT></FONT></SPAN>
            <SPAN LANG="he-IL">{</SPAN>
                <SPAN LANG="he-IL">author = xmlReader.ReadElementString();</SPAN>
            <SPAN LANG="he-IL">} </SPAN>
<FONT COLOR="#000000">            </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">if</FONT><FONT COLOR="#000000"> (xmlReader.IsStartElement(</FONT><FONT COLOR="#800000">&quot;isbn&quot;</FONT><FONT COLOR="#000000">))</FONT></FONT></SPAN>
            <SPAN LANG="he-IL">{</SPAN>
                <SPAN LANG="he-IL">isbn = xmlReader.ReadElementString();</SPAN>
            <SPAN LANG="he-IL">}</SPAN>
        <SPAN LANG="he-IL">}</SPAN>
<FONT SIZE=2><SPAN LANG="he-IL">}</SPAN></FONT>

<SPAN LANG="he-IL">[<FONT COLOR="#008080">ListItemTypeAttribute</FONT>(<FONT COLOR="#0000ff">typeof</FONT>(<FONT COLOR="#008080">Book</FONT>))]</SPAN>
<FONT SIZE=2><SPAN LANG="he-IL"><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#0000ff">class</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#008080">Books</FONT><FONT COLOR="#000000"> : </FONT><FONT COLOR="#008080">ItemListBase</FONT></SPAN></FONT>
<SPAN LANG="he-IL">{</SPAN>
<FONT COLOR="#000000">       </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#0000ff">public</FONT><FONT COLOR="#000000"> Books()</FONT></FONT></SPAN>
       <SPAN LANG="he-IL">{</SPAN>
       <SPAN LANG="he-IL">}</SPAN>
<SPAN LANG="he-IL">}</SPAN></PRE><P>
<BR><BR>
</P>
<H2><SPAN LANG="he-IL">Create database and adding items</SPAN></H2>
<P><SPAN LANG="he-IL">Now that we have the Books database defined, we
are ready to use it. First we create an empty database.</SPAN></P>
<PRE><FONT COLOR="#008080">    <SPAN LANG="he-IL"><FONT SIZE=2>Books</FONT><FONT COLOR="#000000"> books = </FONT><FONT COLOR="#0000ff">new</FONT><FONT COLOR="#000000"> </FONT>Books<FONT COLOR="#000000">();</FONT></SPAN></FONT>
</PRE><P>
<SPAN LANG="he-IL">Now we will define one Book record and add it to
the Books database</SPAN></P>
<PRE><FONT COLOR="#008080">    <SPAN LANG="he-IL"><FONT SIZE=2>Book</FONT><FONT COLOR="#000000"> book1 = </FONT><FONT COLOR="#0000ff">new</FONT><FONT COLOR="#000000"> </FONT>Book<FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">&quot;The Bible&quot;</FONT><FONT COLOR="#000000">);</FONT></SPAN></FONT>
    <SPAN LANG="he-IL"><FONT SIZE=2>book1.author = </FONT><FONT COLOR="#800000">&quot;God&quot;</FONT>;</SPAN>
    <SPAN LANG="he-IL"><FONT SIZE=2>book1.isbn = </FONT><FONT COLOR="#800000">&quot;000-000-001&quot;</FONT>;</SPAN>
<FONT COLOR="#008080">    </FONT>
<FONT COLOR="#008080">    <SPAN LANG="he-IL"><FONT SIZE=2>Key</FONT> justAddedKey = books.Add(book1);</SPAN></FONT></PRE><P>
<SPAN LANG="he-IL">Save it into an XML file &quot;books.xml&quot;</SPAN></P>
<PRE STYLE="margin-bottom: 0.5cm"><FONT SIZE=2><SPAN LANG="he-IL">books.Save(&ldquo;Books.xml&rdquo;);</SPAN></FONT></PRE><H2>
<SPAN LANG="he-IL">Loading database and retrieving items</SPAN></H2>
<P><SPAN LANG="he-IL">When we load a database from the XML file, the
file is parsed according to type of the database container that is
passed. All database items are deserialized and stored in the
database. The items are also hashed so that it can be accessed
quickly using the database's sequential key. In this example we load
the Books database and getting an item using a key.</SPAN></P>
<PRE>    <SPAN LANG="he-IL"><FONT SIZE=2>books = (<FONT COLOR="#008080">Books</FONT><FONT COLOR="#000000">)</FONT><FONT COLOR="#008080">Books</FONT><FONT COLOR="#000000">.Load(</FONT><FONT COLOR="#0000ff">typeof</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">Books</FONT><FONT COLOR="#000000">), &ldquo;books.xml&rdquo;);</FONT></FONT></SPAN>

<FONT COLOR="#000000">    </FONT><SPAN LANG="he-IL"><FONT SIZE=2><FONT COLOR="#008080">Book</FONT><FONT COLOR="#000000"> searchedBook = (</FONT><FONT COLOR="#008080">Book</FONT><FONT COLOR="#000000">)books.GetItem(justAddedKey);</FONT></FONT></SPAN></PRE><H2>
<SPAN LANG="he-IL">Summary</SPAN></H2>
<P><SPAN LANG="he-IL">If you considering to write a .NET app that
uses small personal database, especially if you plan to run it on a
memory constrained devices such as Pocket PC, you should consider
this framework. It's pretty bareboned but it has the basic
functionality to store and retieve records from a XML formatted
files.</SPAN></P>
<P><BR><BR>
</P>
<P><BR><BR>
</P>
</BODY>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions