Click here to Skip to main content
Licence CPOL
First Posted 17 Oct 2007
Views 31,091
Downloads 742
Bookmarked 31 times

C# Class: Text file as DataBase

By | 17 Oct 2007 | Article
This article tries to explain how we could use a TextFile as a simple DataBase.

Object

Introduction

I made this class to use on a Mobile Project (.net compact framework). I tried to use SQL Compact but it gets a lot of resources.
This article will try to show you some great features of Classes in C# and to get them you should have some basic knowledge of Csharp.
We are going to create a class that will provide us some pre-made functions to use a simple text file as a Database.
A simple explanation of classes:

"In object-oriented programming, a class is a programming language construct used to group related instance variables and methods.
A method (called a function in some languages) is a set of instructions specific to a class.
Depending on the language, classes may support multiple inheritance or may require interfaces to extend other classes.
A class may indicate either specifically or abstractly what methods exist when the program executes. The latter is known as an 'abstract class'.
A class must be defined with a constructor if used as an object that will be instantiated.
However, some classes, especially those containing factory methods, are defined as static classes with no constructor.
Instead, they merely organize data hierarchically (e.g. the Math class may contain the static constant PI and the static method abs)."
(Wikipedia)

Conclusion:
If we are objects, the world would have a class called Persons with many functions and properties that describe us and we would be an object of this class =)

Creating the DataBase

The columns will be separated by ';' and we don't need to set ID reference because we are going to use line number as it.
So, one database with the fields Name, LastName, Phone, Mail and Website would be like:

Renan;Duarte;323123131;renan@rodriguesduarte.net;software.botecodorenan.net
João;Carlos;14324;joao.carlos@test.com;www.joaocarlos.com
 ...

Functions

  • Entries: Returns the number of lines.
  • CreateConfigFile: Private function that verifies if the database exists or not and if is needed to create it or not.
  • DeleteEntrie: Deletes an specific record.
  • UpdateEntrie: Updates the details of an specific record.
  • InsertEntrie: Insert a new record.
  • Select: Search for something in a specified column. Returns IDs separated by ";", otherwise "ERROR".
  • ReadEntrie: Reads one record.

Adapting the code to the custom columns

In my class, I prefer to set the columns through the code instead of doing this at the first line of the Database and in my opinion
this is better to avoid future errors. Then the headers of the functions would be:

 public int ReadEntrie(int id, ref string name, ref string lastname, ref string phone, ref string mail, ref string website)

 public int InsertEntrie(string name, string lastname, string phone, string mail, string website)

 public int UpdateEntrie(int id,string name, string lastname, string phone, string mail, string website)

PS: If you change the columns, you will also need to change the Array selection.

Setting Properties

I created this field to get and set the path of the text file. If it exists, great, otherwise we will create this file and use it as we have setted.

  private string data_path = "";
  public string Path
  {
       get { return data_path; }
       set { data_path = value; }
  }
The code above will give us this result:

Many choices

The description that we see at the second image can be added inserting the summary before the header of any function or field:
 /// <summary>
 /// Get or Set the path of the text file!
 /// </summary>

Creating the object

 private DataText conDB;
 private void Form1_Load(object sender, EventArgs e)
 {
 conDB = new DataText();
 conDB.Path = "C:\test.txt";
 fillComboList();
 }


Notice that We have to set the path and after that we should create a function that gets all the entries and fill another object like a ComboBox, ListView, etc.
P.s.:This should be easily done getting the number of entries DataText.Entries and after using While.

The Engine

All the records can be easily read using FileStream and StreamReader. After reading the line, you only need to split it (using ';' separator).
Then you have an Array with all the values of the line =)
The new entries will be added at the end of the file and to update one line, we will re-write all the database.
That's ok for me because I don't have many records. Otherwise we can save the text in another object which can treat better this problem.

Additional functions

We can set functions with the same name but with different parameters. Like:

 public int ReadEntrie(int id, ref string name)

And we are going to get this result:

Many choices

Points of Interest

Setting functions with the same name should be great to get a specific variable variable like, only the name or only the mail.
This code gives you some experience with classes and this would be a start of making dll's and other components =)
I haven't made any bench with this class until now but it should be perfect for small databases.

License

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

About the Author

RenanDuarte

Software Developer
ControliD
Brazil Brazil

Member

Brazilian, studying engineering at Escola Politécnica de São Paulo, USP.
I lived in India for one year, developing software for mobiles and now i'm working at Relogio de Ponto - Control iD developing access control with RFID and fingerprint recognition.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmemberomid reza ghajar18:05 30 May '11  
Questionpls help me run this program PinmemberJaedah5:12 27 Feb '10  
GeneralMy vote of 1 Pinmemberkerkyraios11:36 26 Nov '08  
GeneralAnother approach Pinmemberjw1232:47 17 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Oct 2007
Article Copyright 2007 by RenanDuarte
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid