I screen scrape records from out legacy system(Reflections). Typically I write to Excel or Access and this is lighting fast. Now I am trying to write them directly to our SQL Server. My code works but is very slow. Since this is my first time with this type of connection, I am wondering if my parameters are some how set up wrong and is slowing the process down. I typically scrape in excess of 100,000 records at a time. My sessions are timing out after 40 minutes.
This code is for a batch update. The actual reading in of the records takes about 30 seconds but the batch update is the slow process.
I have also tried to update after each record and the records scroll very slow as it updates after each record.
Any suggestions would be much appreciated.
You don't know what you don't know...
This code is for a batch update. The actual reading in of the records takes about 30 seconds but the batch update is the slow process.
SQL:
Sub FilemanToSS3()
Dim conn As Object
Dim rst As Object
Set conn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
With conn
'Create a connection to SQL Server Connection
.ConnectionString = "Provider=SQLOLEDB.1; Integrated Security=SSPI;Persist Security Info=True;Data Source=R02LITDWH62;Initial catalog=MyDatabase"
'Open connection
.Open
End With
With Session ' Starts Session
With rst ' Opens destination table in Batch mode
Set .ActiveConnection = conn
.Source = dbo.tblTest
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
.CursorLocation = adUseClient
.Open
i = 1
Do Until (Trim(strReadline) Like "" And i > 10) ' read one record at a time until end
'...code to read in 1 record from screen and clean it
arrData = Split(strReadline, "|") 'Creates array from record which are pipe delimited
.AddNew
For y = LBound(arrData) To UBound(arrData)
If Len(arrData(y)) <> 0 Then 'Check if array element is empty
rst.Fields(y).value = Trim(arrData(y))
End If
Next y
i = i + 1
Loop
rst.UpdateBatch
End With
'Close connection to SQL Server and set objects to Nothing
conn.Close
Set rst = Nothing
Set conn = Nothing
End With ' End Session
Exit Sub ' Exit Sub
I have also tried to update after each record and the records scroll very slow as it updates after each record.
SQL:
With rst ' Opens destination table in Batch mode
Set .ActiveConnection = conn
.Source = dbo.tblTest
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.CursorLocation = adUseClient
.Open
i = 1
Do Until (Trim(strReadline) Like "" And i > 10) ' read one record at a time until end
...code to read in 1 record from screen and clean it
arrData = Split(strReadline, "|") 'Creates array from record
.AddNew
For y = LBound(arrData) To UBound(arrData)
If Len(arrData(y)) <> 0 Then 'Check if array element is empty
rst.Fields(y).value = Trim(arrData(y))
End If
Next y
.Update
i = i + 1
Loop
End With
Any suggestions would be much appreciated.
You don't know what you don't know...