I am writing a small application that traverses a website log file (already imported in SQL server 2000) and populates a field called "country_" based on the ip and another lookup table that matches ip long numbers with countries.
What I want to do is run a sql query giving me a set of records where country_ is not populated, then load the the first record, run an update statement for the ip address for this and all other records with the same ip address.
It works and updates, except that it goes through the records even if they have been updated simply because the datareader is not refreshed, even if I close it and reload it again.
I previously used a data control and updated record by record which worked, but was slow (I update about 300-600K records per run), so I am trying to see if I run the sql command directly if it would make a difference in performance.
Anyone know what I need to do to ensure I refresh the set after each loop?
Help is appreciated.
The code looks as follows (I have removed the regular declarations and parts that don't apply to the database)
------------------------------------
Dim myConnection As SqlConnection
Dim Countries_conn As SqlConnection
Dim myCommand As SqlCommand
Dim Countries As SqlCommand
Dim dr As SqlDataReader
goahead = True
myConnection = New SqlConnection(ConnectionStrText.Text)
myConnection.Open()
myCommand = New SqlCommand("Select * from staging where country_ IS Null or Country_ = 'Not Found'", myConnection)
Countries_conn = New SqlConnection(ConnectionStrText.Text)
Countries_conn.Open()
Do While goahead
dr = myCommand.ExecuteReader
If dr.HasRows Then
dr.Read()
ipindr = dr("IP")
numip = Dot2LongIP(Trim(ipindr))
Countries = New SqlCommand("UPDATE staging SET Country_ = (SELECT countryLong FROM IPCountry where " & numip & " BETWEEN FROM_Long AND To_Long) WHERE staging.IP = '" & ipindr & "'", Countries_conn)
ra = Countries.ExecuteNonQuery()
Countries_conn.Close()
Else
goahead = False
End If
myConnection.Close()
Loop
-------------------------------------------
Dot2LongIP is a function that converts ip address from Dot format to Long format so I can compare it to my ip country database.
Staging is the table where I want to update country_
IPcountry is the lookup table
What I want to do is run a sql query giving me a set of records where country_ is not populated, then load the the first record, run an update statement for the ip address for this and all other records with the same ip address.
It works and updates, except that it goes through the records even if they have been updated simply because the datareader is not refreshed, even if I close it and reload it again.
I previously used a data control and updated record by record which worked, but was slow (I update about 300-600K records per run), so I am trying to see if I run the sql command directly if it would make a difference in performance.
Anyone know what I need to do to ensure I refresh the set after each loop?
Help is appreciated.
The code looks as follows (I have removed the regular declarations and parts that don't apply to the database)
------------------------------------
Dim myConnection As SqlConnection
Dim Countries_conn As SqlConnection
Dim myCommand As SqlCommand
Dim Countries As SqlCommand
Dim dr As SqlDataReader
goahead = True
myConnection = New SqlConnection(ConnectionStrText.Text)
myConnection.Open()
myCommand = New SqlCommand("Select * from staging where country_ IS Null or Country_ = 'Not Found'", myConnection)
Countries_conn = New SqlConnection(ConnectionStrText.Text)
Countries_conn.Open()
Do While goahead
dr = myCommand.ExecuteReader
If dr.HasRows Then
dr.Read()
ipindr = dr("IP")
numip = Dot2LongIP(Trim(ipindr))
Countries = New SqlCommand("UPDATE staging SET Country_ = (SELECT countryLong FROM IPCountry where " & numip & " BETWEEN FROM_Long AND To_Long) WHERE staging.IP = '" & ipindr & "'", Countries_conn)
ra = Countries.ExecuteNonQuery()
Countries_conn.Close()
Else
goahead = False
End If
myConnection.Close()
Loop
-------------------------------------------
Dot2LongIP is a function that converts ip address from Dot format to Long format so I can compare it to my ip country database.
Staging is the table where I want to update country_
IPcountry is the lookup table