Click here to Skip to main content
15,918,967 members
Articles / Programming Languages / C#

C# Class: Text file as DataBase

Rate me:
Please Sign up or sign in to vote.
3.30/5 (24 votes)
17 Oct 2007CPOL4 min read 91.9K   4.6K   39   7
This article tries to explain how we could use a TextFile as a simple DataBase.



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)."

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:



  • 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";
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.


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

Written By
Software Developer
Brazil Brazil
Check my Resumé at

Comments and Discussions

QuestionRead text file from web server? Pin
mynametaken24-Sep-17 6:03
mynametaken24-Sep-17 6:03 
QuestionExcellent Pin
Retro6173-Jul-14 9:18
Retro6173-Jul-14 9:18 
General5+ Pin
LTakahata21-Jun-12 16:49
LTakahata21-Jun-12 16:49 
GeneralMy vote of 3 Pin
omid reza ghajar30-May-11 18:05
omid reza ghajar30-May-11 18:05 
Questionpls help me run this program Pin
Jaedah27-Feb-10 5:12
Jaedah27-Feb-10 5:12 
GeneralMy vote of 1 Pin
kerkyraios26-Nov-08 11:36
kerkyraios26-Nov-08 11:36 
Does not work as described.
GeneralAnother approach Pin
jw12317-Oct-07 2:47
jw12317-Oct-07 2:47 

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

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