none
Flat File Database in C# RRS feed

  • Question

  • Hello,

    I would like to use flat files with my C# application. I couldn't find any useful information regarding connectivity with flat files from C#. Is this flat files act like normal files ? if I am storing information to flat file, how I can read it back ? Please help.a
    Thursday, August 30, 2007 6:55 AM

Answers

  •  

    Flat files in my terms :-

     

    One row in a file = 1 datarow

     

    Columns are separated by a delimiter and every row should have the same number of columns and the first row may or may not be column headings.

     

    You can certainly read a file line by line and load into a datatable for instance.

     

    There are classes to assist. File.Open can open a file for reading. A TextReader can read the lines and you can split the line into a string array.

     

    There's alot to learn there but it's well worth it you'll use it again.

     

    I generally use a StreamReader to read files and would need to probably look at a quick example for a refresher.

     

    Certainly what you need can be achieved and it's actually not that difficult.

     

    You could google for some good examples i'm sure of that. It's where I found the info originally.

     

    You would probably want to create a class that has the methods for reading and writing to the file and a method for getting the file read into a datatable for you to use. If you make it comma separated values with double quotes around each column value you can also open it in Excel with no changes.

     

    If you hae lots of columns of different types you may need to do alot of casting yourself which is not very pretty but again not too hard either.

    Thursday, August 30, 2007 8:54 AM
  • This is the nature of a flat file system. It's essentially a passive sytem and there is no built-in mechanism to manage multi-user access.

     

    If multi-user access is a requirement I think you really need to be looking at a server or file based system that can manage this for you. For a flat file system you would pretty much need to write your own database manager.

    Friday, August 31, 2007 12:26 PM

All replies

  •  

    Flat files in my terms :-

     

    One row in a file = 1 datarow

     

    Columns are separated by a delimiter and every row should have the same number of columns and the first row may or may not be column headings.

     

    You can certainly read a file line by line and load into a datatable for instance.

     

    There are classes to assist. File.Open can open a file for reading. A TextReader can read the lines and you can split the line into a string array.

     

    There's alot to learn there but it's well worth it you'll use it again.

     

    I generally use a StreamReader to read files and would need to probably look at a quick example for a refresher.

     

    Certainly what you need can be achieved and it's actually not that difficult.

     

    You could google for some good examples i'm sure of that. It's where I found the info originally.

     

    You would probably want to create a class that has the methods for reading and writing to the file and a method for getting the file read into a datatable for you to use. If you make it comma separated values with double quotes around each column value you can also open it in Excel with no changes.

     

    If you hae lots of columns of different types you may need to do alot of casting yourself which is not very pretty but again not too hard either.

    Thursday, August 30, 2007 8:54 AM
  • You may find this useful. Even if you don't want to convert it into Xml, I'm sure the code sample will provide the code you need to read the text file and split it up into fields.

     

    However I think this would be a better approach. It's ready-made and allows you to connect to the flat file in the same way you'd connect to a normal database.

     

    This only helps with reading the data, not saving any changes back to the file. Do you need to do this?

     

    Another option is to store the data as Xml. You can still store it in a single file, but you don't have the limitations of a flat file. You can have a proper data structure if you want, and .Net has plenty of built-in ways for you to load, manipulate and save this data.

     

    Sean

    Thursday, August 30, 2007 12:18 PM
  • Hello,

    Thanks for reply. I tried implementing this and succeded. My application works on network, and my flat file database resides on the server. It's centralized. So multiple users will be accessing this file. I am getting another problem here. When multiple users are trying to update something, it's throwing exception, because file may be in use by other user. So how can I use this in such a way like how table insertion in database's (multiple insertion can work at same time)  works.

    Please give your ideas
    Thursday, August 30, 2007 5:24 PM
  • This is the nature of a flat file system. It's essentially a passive sytem and there is no built-in mechanism to manage multi-user access.

     

    If multi-user access is a requirement I think you really need to be looking at a server or file based system that can manage this for you. For a flat file system you would pretty much need to write your own database manager.

    Friday, August 31, 2007 12:26 PM
  • You can use a named system mutex to ensure that only one process is able to write to the file at a time. However I agree with Paul, you don't want to be writing a system for this yourself.

     

    Sean

    Tuesday, September 4, 2007 9:28 PM
  • Hi Navaneeth,

     

    Please look into this Code for flat file Writing & Reading using C#

     

    ======================Writing a Flat File (.TXT) File ===============================

    using System;

    using System.IO;

    namespace MyTextExample

    {

    class MyTextClass

    {

    static void main(string[] args)

    {

    TextWriter tw = new StreamWriter("date.txt");

    tw.WriteLine(DateTime.Now);

    tw.close();

    }

    }

    }

    ================== Reading from a Flat File =======================================

    using System;

    using System.IO;

     

    namespace MyTextReader

    {

    class MyTextReaderClass

    {

    static void main(string[] args)

    {

    TextReader tr = new StreamReader("date.txt");

    Console.WriteLine(tr.ReadLine());

    tr.Close();

    }

    }

    }

     

    If you need more help about flat file generation... Please reply ...

     

    Regards

     

    Wednesday, September 5, 2007 9:03 AM