Results 1 to 5 of 5

Thread: [RESOLVED] Could this be more efficient?

  1. #1

    Thread Starter
    Member red_bull_n_vodka's Avatar
    Join Date
    Jun 2005
    Posts
    32

    [RESOLVED] Could this be more efficient?

    I tried bringing the open and close statements out, but then it doesnt get myOdbcCommand. Is there a more efficient way to write this? This ran for 45 minutes before thowing an error. It got though most of the tables.

    VB Code:
    1. For iTable = 1 To 12
    2.         For iCounter = 0 To (ds.Tables(0).Rows.Count - 1)
    3.             If (ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV") <> 0) Then
    4.                 iGasServ = ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV")
    5.                 Dim myDeleteStatement As String = "DELETE * FROM GSV00" & iTable & "T WHERE GSV001T.U_GAS_SERV = " & iGasServ
    6.                 Dim myOdbcCommand As New OdbcCommand(myDeleteStatement, gsvConnection)
    7.                 'Opens the db connection
    8.                 gsvConnection.Open()
    9.                 'Excutes the delete statement
    10.                 myOdbcCommand.ExecuteNonQuery()
    11.                 'Closes connection
    12.                 myOdbcCommand.Connection.Close()
    13.             End If
    14.         Next
    15.         MessageBox.Show("Done with " & iTable)
    16.         Next
    Last edited by red_bull_n_vodka; Jul 15th, 2005 at 02:40 PM.
    I am a rookie @ this...

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Could this be more efficient?

    you get a TimeOut error after 45min! that long ...
    How many record your tables have ??
    I there some relation between your tables ?

    What is your db engine; SQL Server, Oracle, Access ?

    In your code you open the conenction on gsvConnection like this
    gsvConnection.Open()

    Why don't you close it the same way ?
    gsvConnection.Close()

  3. #3

    Thread Starter
    Member red_bull_n_vodka's Avatar
    Join Date
    Jun 2005
    Posts
    32

    Re: Could this be more efficient?

    Quote Originally Posted by Zakary
    you get a TimeOut error after 45min! that long ...
    How many record your tables have ??
    I there some relation between your tables ?

    What is your db engine; SQL Server, Oracle, Access ?

    In your code you open the conenction on gsvConnection like this
    gsvConnection.Open()

    Why don't you close it the same way ?
    gsvConnection.Close()
    VB.NET --> Access

    I swear I tried that connection like you said, and it wouldnt work. Now it does though. I also moved them outside the loop. Its currently running so hopefully it works

    Thanks.
    I am a rookie @ this...

  4. #4
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: Could this be more efficient?

    Have you tried something like this to get the connection out?

    VB Code:
    1. Dim myDeleteStatement As String
    2.         Dim myOdbcCommand As New OdbcCommand
    3.  
    4.         'Opens the db connection
    5.         myOdbcCommand.Connection = gsvConnection
    6.         gsvConnection.Open()
    7.  
    8.         For iTable = 1 To 12
    9.         For iCounter = 0 To (ds.Tables(0).Rows.Count - 1)
    10.             If (ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV") <> 0) Then
    11.                 iGasServ = ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV")
    12.                 myDeleteStatement = "DELETE * FROM GSV00" & iTable & "T WHERE GSV001T.U_GAS_SERV = " & iGasServ
    13.                 myOdbcCommand.CommandText = myDeleteStatement                                'Excutes the delete statement
    14.                 myOdbcCommand.ExecuteNonQuery()
    15.             End If
    16.         Next
    17.         MessageBox.Show("Done with " & iTable)
    18.         Next
    19.  
    20.         myOdbcCommand.Connection.Close()
    This may be totally wrong as I usually work with Sql Server and this is from memory, but hopefully it will help.

    EDIT:: Looks like I was late posting, hope you got it

  5. #5

    Thread Starter
    Member red_bull_n_vodka's Avatar
    Join Date
    Jun 2005
    Posts
    32

    Re: Could this be more efficient?

    This is what finally worked. It only took about an hour or to run...

    VB Code:
    1. Dim iCounter As Integer = 0
    2.         Dim iTable As Integer = 1
    3.         Dim sQuery As String
    4.  
    5.         DataGrid1.DataSource = ds
    6.  
    7.         gsvConnection.Open()
    8.  
    9.         For iCounter = 0 To (ds.Tables(0).Rows.Count - 1)
    10.             If iCounter = (ds.Tables(0).Rows.Count - 1) Then
    11.                 sQuery = sQuery & ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV")
    12.             Else
    13.                 sQuery = sQuery & ds.Tables(0).Rows(iCounter).Item("U_GAS_SERV") & ","
    14.             End If
    15.         Next
    16.  
    17.         For iTable = 1 To 12
    18.             If iTable >= 10 Then
    19.                 Dim myDeleteStatement As String = "DELETE FROM GSV0" & iTable & "T WHERE GSV0" & iTable & "T.U_GAS_SERV IN(" & sQuery & ")"
    20.                 Dim myOdbcCommand As New OdbcCommand(myDeleteStatement, gsvConnection)
    21.                 'Excutes the delete statement
    22.                 myOdbcCommand.ExecuteNonQuery()
    23.             Else
    24.                 Dim myDeleteStatement As String = "DELETE FROM GSV00" & iTable & "T WHERE GSV00" & iTable & "T.U_GAS_SERV IN(" & sQuery & ")"
    25.                 Dim myOdbcCommand As New OdbcCommand(myDeleteStatement, gsvConnection)
    26.                 'Excutes the delete statement
    27.                 myOdbcCommand.ExecuteNonQuery()
    28.             End If
    29.         Next
    30.  
    31.         gsvConnection.Close()

    I am a rookie @ this...

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