read txtfile into string or stringbuilder or .Net dataTable?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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?
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top