Read table from .txt file into DataTable object.

  • Thread starter Thread starter markov_alex
  • Start date Start date
M

markov_alex

Hi. In my application i need to read tables that saved as .txt files
with values delimited by ',' into DataTable object. Please tell me
what is good way to do it?

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
markov_alexwrote:
Hi. In my application i need to read tables that saved as .txt files
with values delimited by ',' into DataTable object. Please tell me
what is good way to do it?

Both those links are broken. Please send me code or another link.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Uzytkownik "markov_alex said:
Hi. In my application i need to read tables that saved as .txt files
with values delimited by ',' into DataTable object. Please tell me
what is good way to do it?

Both those links are broken. Please send me code or another link.

Ok, below is my working example:

a) Data file (E:\myTextData.txt):
Id,Name,Value
1,John,23
2,Bill,34
3,Mary,50
4,Alice,80

b) Test code (OleDb):

using System;
using System.Data;
using System.Data.OleDb;

namespace LoadTextCSharp
{
class Class1
{
static void Main(string[] args)
{
Test t = new Test();
t.LoadData();
t.WriteData();

System.Console.ReadLine();
}
}

class Test
{
OleDbConnection con = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\myTextData.txt;" +
@"Extended Properties=""text;HDR=YES;FMT=Delimited;"";");

OleDbCommand myCom;
DataTable myDt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();

public Test()
{
myCom = new OleDbCommand("Select * From myTextData.txt", con);
da.SelectCommand = myCom;
}

public void LoadData()
{
con.Open();
try
{
da.Fill(myDt);
}
catch(OleDbException ex)
{
System.Console.WriteLine(ex.Message);
}

con.Close();
}

public void WriteData()
{
//headers
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dc.ColumnName + "\t");

//data
foreach(DataRow dr in myDt.Rows)
{
System.Console.WriteLine();
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dr[dc].ToString() + "\t");
}
}
}
}

Regards,
Grzegorz
 
Uzytkownik "markov_alex said:
Hi. In my application i need to read tables that saved as .txt files
with values delimited by ',' into DataTable object. Please tell me
what is good way to do it?

Both those links are broken. Please send me code or another link.

Ok, below is my working example:

a) Data file (E:\myTextData.txt):


b) Test code (OleDb):

using System;
using System.Data;
using System.Data.OleDb;

namespace LoadTextCSharp
{

class Class1
{
static void Main(string[] args)
{
Test t = new Test();
t.LoadData();
t.WriteData();


System.Console.ReadLine();
}
}

class Test
{
//OleDbConnection con = new OleDbConnection("File name=myUdl.udl");
OleDbConnection con = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\;" +
@"Extended Properties=""text;HDR=YES;FMT=Delimited;"";");

OleDbCommand myCom;
DataTable myDt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();

public Test()
{
myCom = new OleDbCommand("Select * From myTextData.txt", con);
da.SelectCommand = myCom;
}

public void LoadData()
{
con.Open();
try
{
da.Fill(myDt);
}
catch(OleDbException ex)
{
System.Console.WriteLine(ex.Message);
}

con.Close();
}

public void WriteData()
{
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dc.ColumnName + "\t");

foreach(DataRow dr in myDt.Rows)
{
System.Console.WriteLine();
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dr[dc].ToString() + "\t");
}
}
}
}
using System;
using System.Data;
using System.Data.OleDb;

namespace LoadTextCSharp
{
class Class1
{
static void Main(string[] args)
{
Test t = new Test();
//load data from text file
t.LoadData();

//test loaded data
t.WriteData();

System.Console.ReadLine();
}
}

class Test
{
//OleDbConnection con = new OleDbConnection("File name=myUdl.udl");
OleDbConnection con = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\;" +
@"Extended Properties=""text;HDR=YES;FMT=Delimited;"";");

OleDbCommand myCom;
DataTable myDt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();

public Test()
{
myCom = new OleDbCommand("Select * From myTextData.txt", con);
da.SelectCommand = myCom;
}

public void LoadData()
{
con.Open();
try
{
da.Fill(myDt);
}
catch(OleDbException ex)
{
System.Console.WriteLine(ex.Message);
}

con.Close();
}

public void WriteData()
{
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dc.ColumnName + "\t");

foreach(DataRow dr in myDt.Rows)
{
System.Console.WriteLine();
foreach(DataColumn dc in myDt.Columns)
System.Console.Write(dr[dc].ToString() + "\t");
}
}
}
}

Regards,
Grzegorz
 
Uzytkownik "Grzegorz Danowski" <[email protected]> napisal w
wiadomosci (...)
Ok, below is my working example:

a) Data file (E:\myTextData.txt):

I'm forgot to paste the test text file:

Id,Name,Value
1,John,23
2,Bill,34
3,Mary,50
4,Alice,80

In case of more complicated formats you must use ini.file (as Val Mazur
wrote).
I hope it helps.
Grzegorz
 
Uzytkownik "Grzegorz Danowski" <[email protected]> napisal w
wiadomosci (...)
Ok, below is my working example:

a) Data file (E:\myTextData.txt):

I have forgot paste the test text file:

Id,Name,Value
1,John,23
2,Bill,34
3,Mary,50
4,Alice,80

In case of more complicated formats you must use ini.file (as Val Mazur
has written).
I hope it helps.
Grzegorz
 
On 8 Nov 2004 11:56:37 -0600, (e-mail address removed)-dot-il.no-spam.invalid (markov_alex) wrote:

¤ Hi. In my application i need to read tables that saved as .txt files
¤ with values delimited by ',' into DataTable object. Please tell me
¤ what is good way to do it?
¤

You can use Jet OLEDB with the Text driver:

Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My Documents\TextFiles" & ";" & _
"Extended Properties=""Text;HDR=NO;"""

Dim TextConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
TextConnection.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM CSV#txt", TextConnection)

Dim ds As New DataSet("TextFiles")
da.Fill(ds, "CSV")

Dim dt As DataTable

dt = ds.Tables("CSV")

DataGrid1.SetDataBinding(ds, "CSV")

Dim drCurrentCol As DataColumn
For Each drCurrentCol In dt.Columns
Console.WriteLine(drCurrentCol.ColumnName)
Next

Dim drCurrent As DataRow
For Each drCurrent In dt.Rows
Console.WriteLine(drCurrent(0).ToString)
'Console.WriteLine(drCurrent(1).ToString)
'Console.WriteLine(drCurrent(2).ToString)
'Console.WriteLine(drCurrent(3).ToString)
'Console.WriteLine(drCurrent(4).ToString)
'Console.WriteLine(drCurrent(5).ToString)
'Console.WriteLine(drCurrent(6).ToString)
Next

TextConnection.Close()


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Back
Top