Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, there , i want to creat a basic database with .txt file. i build an application with c# smart device visual studio 2008.
i have a form that contain 3 fields of textbox.
every textbox represent a value to set.
for example textbox1.text= "ID_14" textbox2.txt="AER",textbox3.txt="men"
me i want when i hit the button. it will generate me a txt file that contain data that was in textbox. in this format.

ID_14: ;AER ;men.

and if i add other information for example
textbox1.text= "ID_15" textbox2.txt="AEl",textbox3.txt="women"
i add those information of my last txt file and the output will be like these

ID_14: ;AER ;men.
ID_15: ;AEl ;women.

this is the first step.

and the second step
i want to open this database read it and to edit last information, for example if i want to change the sex or the id after i save it.

i have no idea ho to do that. please help

What I have tried:

Hello, there , i want to creat a basic database with .txt file. i build an application with c# smart device visual studio 2008.
i have a form that contain 3 fields of textbox.
every textbox represent a value to set.
for example textbox1.text= "ID_14" textbox2.txt="AER",textbox3.txt="men"
me i want when i hit the button. it will generate me a txt file that contain data that was in textbox. in this format.

ID_14: ;AER ;men.

and if i add other information for example
textbox1.text= "ID_15" textbox2.txt="AEl",textbox3.txt="women"
i add those information of my last txt file and the output will be like these

ID_14: ;AER ;men.
ID_15: ;AEl ;women.

this is the first step.

and the seconde step
i want to open this database read it and to edit last information, for example if i want to change the sex or the id after a save it.

i have no idea ho to do that. please help
Posted
Updated 25-Jun-16 5:23am
v4

The whole problem is so awkward and not easy to implement, so it's hard to give a comprehensive advice. However, the idea of implementation of the database storage might be a useful exercises. (Perhaps one goal would be to develop the appreciation of existing database systems. :-))

The problem is: it looks like you try to use a plain text file where a line represents some record. Generally, it's a very bad media for the database storage, because the lines can be of different lengths. Imagine you need to modify one record close to the beginning of the file. If the modified record makes different length of the line, you have to rewrite the rest of the file, because each and every character will be shifted. There is nothing you can do about it: in general case, you have to read the whole file, and write it all back, or a big part of it, 1/2 of the file size on average. You cannot even skip any part of the file on reading, because you never know the length of each line.

One solution is to create another file. You can call it "index file". In its simplest form, it can be used to store the position of each line in the main file, as well as the length of each record. Moreover, you may need to have several indices. For example, one index would be sorted by one field, another by another field. Then the main reading will be done via peeking the line at given position. However, the problem with modification will remain the same: you will need to rewrite the rest of the file; also, you should keep integrity between index files and main file.

Note that if this is a kind of relational database, we are only discussing a single table represented by the file. In relational model, you would need to have multiple table, keys, foreign keys, all that stuff.

Anyway, another approach for a single table would be fixed-size records. Let's say, in a table metadata you setup fixed sizes of all data types. It is especially painful for strings: for each attribute, you will need to have length limit. With fixed-size lines, you don't even need the end-of-line characters. You just peek a record by multiplying the record number by record size, go to this position in file and read/write it, without rewriting the rest of the file. But the limitation of string lengths is draconian…

Well, just some food for thought…

—SA
 
Share this answer
 
The best way to do this would be to create a class of objects that hold those details, bind them to the textboxes and use serialisation to save them to backing store. There are many samples of each issue that will show you how to do it.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900