473,416 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

reading text file to dataset

I need to read text file having data either comma seperated or tab seperated
or any custom seperator and convert into a DataSet in C# .

I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file
but could not get the data in correct format.

All columns are not coming in dataset and rows are messing up.

Suggestions please ???


Mar 22 '06 #1
4 12757
You can use IO.StreamReader class to open the file then read it into a
datatable. After done, add this datatable to the dataset.
Here is the working function in VB.Net (can be easily converted to C#) that
read a text file (can be CSV, tab delitmiting, or anything... You just need
to specify what the seperator character is)and return a dataTable.

'Reading file to datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal
seperator As Char) As DataTable

Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields
there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try

Return myTable
End Function

Hope this helps.
VHD50.

"Amit Maheshwari" wrote:
I need to read text file having data either comma seperated or tab seperated
or any custom seperator and convert into a DataSet in C# .

I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file
but could not get the data in correct format.

All columns are not coming in dataset and rows are messing up.

Suggestions please ???


Mar 22 '06 #2


In some specail casees it wouldn't work, like--

FaxNumber,Company,"First Name , Last Name"

123456789,"asd,fgjh",xxx yyy

In this example there are three coulmns. In first row third coulmn having
comma within coulmn name enclosed by ", and same in second column of second
row. Split(seperator) will give incorrect coulmns values. Microsoft Text
Driver working fine but its showing first column of first row as null which
should be 'FaxNumber'. No idea where is the prob with this. And if save this
file as CSV the same thing occur but if I use any other CSV file which is
generated by code, it works fine for that.

What should I do to resolve this..???

"VHD50" <VH***@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...
You can use IO.StreamReader class to open the file then read it into a
datatable. After done, add this datatable to the dataset.
Here is the working function in VB.Net (can be easily converted to C#) that read a text file (can be CSV, tab delitmiting, or anything... You just need to specify what the seperator character is)and return a dataTable.

'Reading file to datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal
seperator As Char) As DataTable

Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields
there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try

Return myTable
End Function

Hope this helps.
VHD50.

"Amit Maheshwari" wrote:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# .

I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format.

All columns are not coming in dataset and rows are messing up.

Suggestions please ???


Mar 23 '06 #3

In some specail casees it wouldn't work, like--

FaxNumber,Company,"First Name , Last Name"

123456789,"asd,fgjh",xxx yyy

In this example there are three coulmns. In first row third coulmn having
comma within coulmn name enclosed by ", and same in second column of second
row. Split(seperator) will give incorrect coulmns values. Microsoft Text
Driver working fine but its showing first column of first row as null which
should be 'FaxNumber'. No idea where is the prob with this. And if save this
file as CSV the same thing occur but if I use any other CSV file which is
generated by code, it works fine for that.

What should I do to resolve this..???

"VHD50" <VH***@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...
You can use IO.StreamReader class to open the file then read it into a
datatable. After done, add this datatable to the dataset.
Here is the working function in VB.Net (can be easily converted to C#) that read a text file (can be CSV, tab delitmiting, or anything... You just need to specify what the seperator character is)and return a dataTable.

'Reading file to datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal
seperator As Char) As DataTable

Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields
there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try

Return myTable
End Function

Hope this helps.
VHD50.

"Amit Maheshwari" wrote:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# .

I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format.

All columns are not coming in dataset and rows are messing up.

Suggestions please ???


Mar 23 '06 #4
Hey, I got the solution, it was datatype problem when reading using
Microsoft.Jet.OLEDB.4.0, it was treating first column as a Double because
all the contents except first row are double type. To resolve this i created
a schema.ini file in which i wrote ScanRows=1 which means scan only first
row to decide the datatype.
Now there is another thing to think is if first row is empty and contents
start from second row then it understand that file has only one column and
in dataset will only have one column.But for now i am not worrying abt it.
"Amit Maheshwari" <am*********@dev.com> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...

In some specail casees it wouldn't work, like--

FaxNumber,Company,"First Name , Last Name"

123456789,"asd,fgjh",xxx yyy

In this example there are three coulmns. In first row third coulmn having
comma within coulmn name enclosed by ", and same in second column of second row. Split(seperator) will give incorrect coulmns values. Microsoft Text
Driver working fine but its showing first column of first row as null which should be 'FaxNumber'. No idea where is the prob with this. And if save this file as CSV the same thing occur but if I use any other CSV file which is
generated by code, it works fine for that.

What should I do to resolve this..???

"VHD50" <VH***@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...
You can use IO.StreamReader class to open the file then read it into a
datatable. After done, add this datatable to the dataset.
Here is the working function in VB.Net (can be easily converted to C#)

that
read a text file (can be CSV, tab delitmiting, or anything... You just

need
to specify what the seperator character is)and return a dataTable.

'Reading file to datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal
seperator As Char) As DataTable

Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields
there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try

Return myTable
End Function

Hope this helps.
VHD50.

"Amit Maheshwari" wrote:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# .

I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format.

All columns are not coming in dataset and rows are messing up.

Suggestions please ???



Mar 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
4
by: Brian Parker | last post by:
Here's a snippet of code I have: ============================================== DataSet ds = new DataSet(); string strXMLFileName = Path.GetTempFileName(); StreamWriter sw = File.AppendText(...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
2
by: rwiegel | last post by:
I'm trying to read rows from an Excel file and display them in an ASP.NET DataGridview. I am using C# for the code file. I am using OleDb to read from the Excel file. The columns that contain...
0
by: ahtan | last post by:
Hi, I have a little problem reading a csv file. The following is the code that I have and it works, however, it doesn't read the first value in the csv file. In the csv file, I have: 1, test10,...
5
by: Lucvdv | last post by:
This would better be described by 'serialization' than 'interop', but I didn't find a newsgroup that seems closer on topic. The problem in a few words: I save data with DataSet.WriteXML, but I...
2
by: tshad | last post by:
I have a csv file that I am reading into my dataset. But I want to ignore any rows that have nothing in the 3rd column - something like: da = new OleDbDataAdapter("SELECT * FROM " +...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.