How to Read text file as database

How to Read text file as database

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;


namespace PubishApps
{
    public class ReadTextFileAsDB
    {
/************************************************
* Topic : How to Read text file as database
* Use : Client code could implement DB-Query on text file.
* Author : kalit sikka
* For : http://eggheadcafe.com
* **********************************************/

        OleDbConnection oConnection = new OleDbConnection();

        private bool OpenConnection(string InputTextFileName)
        {
            if(!string.IsNullOrEmpty(InputTextFileName))
            {
                oConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+InputTextFileName+"'; Extended Properties=text;HDR=yes;FMT=Delimited";
                 oConnection.Open();

                 if(oConnection.State == System.Data.ConnectionState.Open)
                      return true;
                 else
                     return false;
            }

            return false;
        }

         public void BindingDataToGridView(string InputTextFileName, string Query, DataSet ds)
        {
            try
            {
                 OpenConnection(InputTextFileName);

                  // Create the data adapter to retrieve all rows from text file.
                OleDbDataAdapter da =
                     new OleDbDataAdapter(Query, oConnection);
                
                 // Create and fill the table.
                DataSet dt = new DataSet("MyData");
                da.Fill(dt);
                ds = dt.Copy();

                 // Bind the default view of the table to the grid.
             //   DBview.DataSource = dt.DefaultView;


             }catch(Exception ex)
             {
                 Console.WriteLine("Error Occured: "+ ex.Message);
             }
             finally
            {
                 if(oConnection.State == System.Data.ConnectionState.Open)
                {
                    oConnection.Close();
                }
            }


        }

        
    }
}


Format of InputFile

Field1,ClientBates,DOC_Break,Addin_Field,Path
IM,FM036021-001-000001,B,0,@FM03602101;IMAGES\00\00;FM036021-001-000001.TIF;2
IM,FM036021-001-000002, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000002.TIF;2
IM,FM036021-001-000003,D,0,@FM03602101;IMAGES\00\00;FM036021-001-000003.TIF;2
IM,FM036021-001-000004, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000004.TIF;2
IM,FM036021-001-000005,D,0,@FM03602101;IMAGES\00\00;FM036021-001-000005.TIF;2
IM,FM036021-001-000006, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000006.TIF;2
IM,FM036021-001-000007, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000007.TIF;2
IM,FM036021-001-000008, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000008.TIF;2
IM,FM036021-001-000009, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000009.TIF;2
IM,FM036021-001-000010,D,0,@FM03602101;IMAGES\00\00;FM036021-001-000010.TIF;2
IM,FM036021-001-000011, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000011.TIF;2
IM,FM036021-001-000012, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000012.TIF;2
IM,FM036021-001-000013, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000013.TIF;2
IM,FM036021-001-000014, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000014.TIF;2
IM,FM036021-001-000015, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000015.TIF;2
IM,FM036021-001-000016, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000016.TIF;2
IM,FM036021-001-000017, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000017.TIF;2
IM,FM036021-001-000018, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000018.TIF;2
IM,FM036021-001-000019, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000019.TIF;2
IM,FM036021-001-000020, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000020.TIF;2
IM,FM036021-001-000021, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000021.TIF;2
IM,FM036021-001-000022, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000022.TIF;2
IM,FM036021-001-000023, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000023.TIF;2
IM,FM036021-001-000024, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000024.TIF;2
IM,FM036021-001-000025, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000025.TIF;2
IM,FM036021-001-000026, ,0,@FM03602101;IMAGES\00\00;FM036021-001-000026.TIF;2
By Kalit Sikka   Popularity  (2551 Views)