Setup Environment:
I'm developing an Excel 2010 Application Level Add-in using vb.net with .NET framework 4.
My goal:
- Have user type in multiple names to search for
- Use the list of names to execute a SQL query on a LARGE spreadsheet (30,000+ rows)
- Return the recordset and paste into new worksheet
Performance is my priority. I'd like to know the fastest way to do this by leveraging the .NET framework.
Using an ADO Connection Object in my code works, but the process takes too long (5 - 8 seconds).
This is the SQL query I'm using on the table named wells:
SELECT *
FROM wells
WHERE padgroup in
(SELECT padgroup
FROM wells
WHERE name LIKE 'TOMCHUCK 21-30'
OR name LIKE 'FEDERAL 41-25PH')
Here's a portion of what the table looks like:
I'm using this code right now to create an ADO Connection Object to retrieve my results:
'Create Recordset Object
rsCon = CreateObject("ADODB.Connection")
rsData = CreateObject("ADODB.Recordset")
rsCon.Open(szConnect)
rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
'Check to make sure data is received, then copy the data
If Not rsData.EOF Then
TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
Else
MsgBox("No records returned from : " & SourceFile, vbCritical)
End If
'Clean up the Recordset object
rsData.Close()
rsData = Nothing
rsCon.Close()
rsCon = Nothing
Based on what I know, Excel spreadsheets are stored in the Open XML format and the .NET framework includes native support for parsing XML.
After researching it, I came across a few different options:
Could somebody provide a pointer on what would be the best method to use? I'd really appreciate it.
Additional Notes:
- All queries need to be able to perform without connectivity to an online database
- I only need access to the spreadsheet once to extract the raw data from rows
Right now I just embed the spreadsheet as a project resource.
Then, at run-time I create the file, run the query, store the results in-memory, and delete the file.
'Create temp file path in the commonapplicationdata folder
Dim excelsheetpath As StringBuilder
excelsheetpath = New StringBuilder(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
excelsheetpath.Append("\MasterList.xlsm")
'Save resources into temp location in HD
System.IO.File.WriteAllBytes(excelsheetpath.ToString, My.Resources.MasterList)
'Now call the function to use ADO to get records from the MasterList.xlsm file just created
GetData(excelsheetpath.ToString, "Sheet1", "A1:S40000", True, False)
'Store the results in-memory and display by adding to a datagridview control (in a custom task pane)
'Delete the spreadsheet
System.IO.File.Delete(excelsheetpath.ToString())