0

Below is my class code

 public class TextReaderClass
 {

    public DataTable ReadTextFile(string filepath)
    {
        DataTable dtTextFile = new DataTable();

        dtTextFile.Columns.Add("Emp_Id");
        dtTextFile.Columns.Add("Emp_FirstName");
        dtTextFile.Columns.Add("Emp_LastName");
        dtTextFile.Columns.Add("Emp_Designation");
        dtTextFile.Columns.Add("Emp_Salary");
        dtTextFile.Columns.Add("Emp_JoiningDate");

        //CODE to add here reading from text file and filling the data table
        string[] readTextfile = System.IO.File.ReadAllLines(@"D:\Rutu\datatest.txt");

        //Run a loop here for each record in the text file
        foreach (string line in readTextfile)
        {
            var cols = line.Split(';');
            DataRow dtrow = dtTextFile.NewRow();
            //Create an object of Data Row
            dtTextFile.Rows.Add(dtrow);              
            //Fill the row data in DataTable
        }
       return dtTextFile;

    }
}

I tried this CODE for reading from text file and filling the data table. Any help will be highly appreciated. Thanks

sri harsha
  • 676
  • 6
  • 16

3 Answers3

1

You can do like that by doing this you you also read and add it to datatable

public DataTable ReadTextFile(string filePath, int numberOfColumns)
{
    DataTable tbl = new DataTable();

    for(int col =0; col < numberOfColumns; col++)
        tbl.Columns.Add(new DataColumn("Column" + (col+1).ToString()));


    string[] lines = System.IO.File.ReadAllLines(filePath);

    foreach(string line in lines)
    {
        var cols = line.Split(':');

        DataRow dr = tbl.NewRow();
        for(int cIndex=0; cIndex < 3; cIndex++)
        {
           dr[cIndex] = cols[cIndex];
        }

        tbl.Rows.Add(dr);
    }

    return tbl;
}
Tech Sourav
  • 124
  • 1
  • 7
0

If you have a formatted text file and MS SQL Server consider using the bulk insert facility:

https://learn.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-ver15

An example:

BULK INSERT Sales.Orders FROM 'C:\Rutu\datatest.txt' WITH ( FORMAT='CSV');
Mark McWhirter
  • 1,168
  • 3
  • 11
  • 28
0

try this code.

public DataTable ReadTextFile(string filepath,int numberOfColumns)
{
    DataTable dtTextFile = new DataTable();

    for(int col =0; col < numberOfColumns; col++)
        dtTextFile.Columns.Add(new DataColumn("Column" + (col+1).ToString()));


    string[] readTextfile = System.IO.File.ReadAllLines(filepath);


    foreach (string line in readTextfile)
    {
        var cols = line.Split(';');
        DataRow dtrow = dtTextFile.NewRow();

        for(int cIndex=0; cIndex < 3; cIndex++)
        {
            dtrow [cIndex] = cols[cIndex];
        }

        dtTextFile.Rows.Add(dtrow);              

    }

    return dtTextFile;

}

And also get datatable to gridview

dataGridview.DataSource=ReadTextFile(filepath,6);
Hammad Sajid
  • 312
  • 1
  • 3
  • 14
Vishal Parmar
  • 524
  • 7
  • 27