By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

read txtfile into string or stringbuilder or .Net dataTable?

P: n/a
I understand that the stringbuilder object can create a string of text much
more quickly/efficiently than a string object. I am just checking - if I
have a textfile with say 1000 rows of data - say 50 columns per row uniformly
- does it make any difference if I read this data directly into a string from
a StreamReader or into a StringBuilder object? Is there any criteria for
using a StringBuilder object over a string or vice versa?

While I'm at it - this data will end up in a .Net dataTable object. If my
dataTable: dt1, exists and has 50 columns (correct datatypes) can a
StreamReader read the data directly into the dataTable?

Thanks,
Rich
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rich,

I don't see why you need that string or that stringbuilder.

What I hear from you is something as an csv file that you want to convert to
a datatable.
That can with not more than 10 rows of code.

Can you explain it something more.

Cor
Nov 21 '05 #2

P: n/a
I have a delimited text field with 1000 rows of data, 50 columns. I want to
read this data to sql server. Right now I have a stored procedure which
inserts the data to the table in sql server from a dataTable in my .net app.
I have a sqlDataAdapter which creates the dataTable, 50 parameters. I read
the data to the dataTable from the text file and then run the
sqlDataAdapter.Update(dataset, "MyTable"). this works fine. But right now I
read the textfile to a string, str1 then:

strRows = Split(str1, myMultiCharDelimeter1)
For i = 1 to strRows.Length - 1
strCols = Split(strRows(i).ToString, myMultiCharDelim2)
dr = dataset.Tables("MyTable").NewRow
For j = 0 to strCols.Length - 1
dr.Item(j) = strCols(j)
Next
dataset.Tables("MyTable").Rows.Add(dr)
Next
....
sqlDataAdapter.Update(dataset, "MyTable")
Is there an easier way to populate MyTable dataTable from the text file than
using the two for loops?

"Cor Ligthert" wrote:
Rich,

I don't see why you need that string or that stringbuilder.

What I hear from you is something as an csv file that you want to convert to
a datatable.
That can with not more than 10 rows of code.

Can you explain it something more.

Cor

Nov 21 '05 #3

P: n/a
Rich,

You know this code?

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim file As String = "Test2.txt"
Dim path As String = "C:\Test1\"
Dim ds As New DataSet
Try
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
///

I hope this helps a little bit?

Cor
Nov 21 '05 #4

P: n/a
Yes. Thank you. This is exactly what I was looking for.

"Cor Ligthert" wrote:
Rich,

You know this code?

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim file As String = "Test2.txt"
Dim path As String = "C:\Test1\"
Dim ds As New DataSet
Try
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
///

I hope this helps a little bit?

Cor

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.