Results 1 to 3 of 3

Thread: How can I speed this up?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    South Coast UK
    Posts
    18

    How can I speed this up?

    Very new to VB and would like some advice on how to improve performance of this utility.

    Basically it reads a text file, which has 120k lines, and then separates out fields within the text line into an Access database for processing later.

    I have tried two approaches, one using a StreamReader and the other using ADO.net to create a data set. The format of the file doesn't lead itself to ADO.Net but it still loads.

    In both cases it takes about 20minutes to create the database file.

    Here is an extract from one section of the code....

    VB Code:
    1. Dim strVTVreport As String = "Z:\xxx\xxx.txt"
    2.         Dim sr As IO.StreamReader = New IO.StreamReader(strVTVreport)
    3.         Dim Var1-Var10As String
    4.  
    5.         Dim dbConnection As OleDbConnection
    6.         Dim dbCommand As OleDbCommand = New OleDbCommand
    7.  
    8.         dbConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=Z:\xxx\DataBases\xxx.mdb")
    9.         dbCommand.Connection = dbConnection
    10.         dbConnection.Open()
    11.  
    12.         dbCommand.CommandText = "Delete * From [xxx Table]"
    13.         dbCommand.ExecuteNonQuery()
    14.  
    15.         strLineIn = sr.ReadLine
    16.  
    17.         Debug.WriteLine(DateTime.Now)
    18.  
    19.         While Not strLineIn = Nothing
    20.  
    21.             ' Debug.WriteLine(strLineIn.Length & "    " & strLineIn)
    22.  
    23.             If strLineIn.Length = 118 Then
    24.  
    25.                 If strLineIn.Substring(51, 1).ToString = ":" Then
    26.  
    27.                     strLineLen = strLineIn.Length
    28.                     strVar1 = strLineIn.Substring(1, 6).ToString
    29.                     .
    30.                     .
    31.                     .
    32.                     .
    33.                     strVar10= strLineIn.Substring(113, (strLineIn.Length - 113)).ToString
    34.  
    35.                     '
    36.                     '   Build a SQL statement
    37.                     '
    38.  
    39.                     Dim strSQL As String = "INSERT INTO [xxx]( var1....var10)" & _
    40.                                         " VALUES(@Var1....@var10)
    41.  
    42.                     '
    43.                     '   Now that we have defined the SQL statement and some positional parms assign some values to them
    44.                     '
    45.  
    46.                     dbCommand.Parameters.Add("@Var1", OleDb.OleDbType.VarChar, 50).Value = strVar1
    47.                     .... more lines
    48.                    
    49.                     dbCommand.Parameters.Add("@Var10, OleDb.OleDbType.VarChar, 50).Value = strVar10
    50.  
    51.                     '
    52.                     ' Define and SQL statement and execute it. Once were done clear all the Parameter.Add values for the next iterations
    53.                     '
    54.                     Try
    55.  
    56.                         dbCommand.CommandText = strSQL
    57.                         dbCommand.ExecuteNonQuery()
    58.                         dbCommand.Parameters.Clear()

    Thanks in advance

    RibTime

  2. #2
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    297

    Re: How can I speed this up?

    i should try to add datarows in stead of the insert command.
    Try to fill a dataset Like in this example 'DS' in combination with a .
    Someting like:

    VB Code:
    1. dbConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=Z:\xxx\DataBases\xxx.mdb")
    2.         dbDataAdapter = New OleDBDataAdapter("SELECT * FROM [xxx Table], dbConnection)
    3.         ds = New DataSet
    4.         dbCommandBuilder = New OleDBCommandBuilder(dbDataAdapter)
    5.         dbDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    6.         dbDataAdapter.Fill(ds, [xxx Table])
    7. ds.clear
    8.  
    9. (in the Loop: )
    10.  
    11. Dim Drow as Datarow
    12. Drow = Ds.Tables(0).NewRow()
    13. Drow!anything = blablabla
    14. Drow!AlsoAnyThing = blabla
    15. Ds.Tables(0).Rows.Add(Drow)
    16.  
    17. (After the loop: )
    18.  
    19. dbDataAdapter.Update(Ds, [xxx Table])

    This will speed thinks up, I think.

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How can I speed this up?

    Remember that when debugging, any kind of output will affect the speed of processing. The Debug.Writeline is slowing the process down just because it is having to output that to the debug window each iteration. If you run the EXE outside visual studio.net, it will be faster because it shouldnt output the debug messages...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width