Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from textfile

Status
Not open for further replies.

edderic

Programmer
May 8, 1999
628
I am a textfile of 8 Mb with line records,if The line = "#CHLI" then the next 40 lines are = 40 database fields and insert to the database (Access).

In total 20.000 records with 40 fields to insert,the code is place to backgroundworker buth de insert is 1 hour to do complete !!!

The code :

If txtBestand.Text = Nothing Then Exit Sub
Dim fiFileHolder As FileInfo = New FileInfo(txtBestand.Text)
Dim srReadFile As StreamReader = fiFileHolder.OpenText()
Dim strInput As String
Dim LineCnt As Integer = 0
da.Fill(ds, "Artikelen")
Dim ipos As Integer = 0
Try

Do
strInput = srReadFile.ReadLine
If strInput <> "#CHLI" Then GoTo verder
Select Case strInput
Case "#FIN"
Exit Do
Case "#CHLI"
DatRij = ds.Tables("Artikelen").NewRow
For LineCnt = 0 To 46
If LineCnt = 0 Then
LineCnt = 1
End If
strInput = srReadFile.ReadLine
DatRij(LineCnt) = strInput
Next
ds.Tables("Artikelen").Rows.Add(DatRij)
da.Update(ds, "Artikelen")
End Select
verder:
Loop Until strInput Is Nothing
srReadFile.Close()
cmdLees.Enabled = True
cmdImport.Enabled = False
Catch ex As Exception
If oledbCon.State = ConnectionState.Open Then oledbCon.Close()
srReadFile.Close()
End Try


Eric De Decker

 
Some issues in there. Goto is bad, m'kay. Also, you explicitly check for the value "#CHLI" but then you check for "#FIN". If strInput = "#CHLI" it can not equal "#FIN"

Also, not sure why you're looping from 0 to 46 then changing the 0th iteration to 1...

Code:
Do
  strInput = srReadFile.ReadLine
  If strInput = "#CHLI" 
    DatRij = ds.Tables("Artikelen").NewRow
    For LineCnt = 1 To 46
      strInput = srReadFile.ReadLine
      DatRij(LineCnt) = strInput
    Next
      
    ds.Tables("Artikelen").Rows.Add(DatRij)
    da.Update(ds, "Artikelen")
  End If
Loop Until strInput Is Nothing

srReadFile.Close()
cmdLees.Enabled = True
cmdImport.Enabled = False

Those changes might speed it up a little bit.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thats not the problem :
For LineCnt = 1 To 46

0 is the primary key

Eric

Eric De Decker

 
It's going to take an hour to complete because you are hitting I/O waits on the hard drive and loading 20,000 records (with 40 values each) into memory. Depending on the size of that data you could be hitting the swap file. The If LineCnt = 0 Then LineCnt = 1 code block is ensuring that DatRij(0) is empty. Changing your for loop to 1 to 46 will achieve the same thing.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top