I have the following code:
'connection string for AD sql database
Dim connString As String = "Initial Catalog=WCI_Global;Data Source=SAC_DB5_SRV;Integrated Security=SSPI;"
'connection object for AD sql database
Dim myConnection As New SqlConnection(connString)
Dim ExcelObj As Excel.Application
Dim strsql As String
ExcelObj = New Excel.Application
If (ExcelObj Is Nothing) Then
MsgBox("ERROR: EXCEL couldn't be started!")
System.Windows.Forms.Application.Exit()
End If
ExcelObj.Visible = True
myConnection.Open()
'Dim ExcelReader As OleDbDataReader
Dim ADReader As SqlDataReader
'Dim objCmd As New OleDbCommand(strExcelSql, objConn)
Dim myCommand As New SqlCommand
'myConnection.Open()
myCommand.Connection = myConnection
Dim theWorkbook As Excel.Workbook
Dim sheets As Excel.Sheets
Dim worksheet As Excel.Worksheet
Dim range As Excel.Range
Dim i As Int32
Dim myvalues As System.Array
Dim strArray As System.Array
theWorkbook = ExcelObj.Workbooks.Open("\\Sacinf01\isdept$\Development\Programs\Security Audit Tool\All_active_and_leave_employees_9-2-05.xls", 0, True)
sheets = theWorkbook.Worksheets
worksheet = sheets.Item(1)
'Clear Listview
lvCompareResults.Items.Clear()
lvCompareResults.ListViewItemSorter = Nothing
lvCompareResults.BeginUpdate()
lvCompareResults.ListViewItemSorter = Nothing
Dim lvi As ListViewItem
Dim compare As Boolean
Dim FirstName As String
Dim LastName As String
Dim yoyo As String
Dim NumFound As Int32
NumFound = 0
Dim found As Boolean
Dim count As Int32
Dim ADFirstName As String
Dim ADLastName As String
Dim spacepos As Int32
Dim j As Int32
j = 0
Dim rowfoundat As Int32
rowfoundat = 0
strsql = "Select cn,sn,whenCreated,whenChanged,displayName,memberOf,givenName from ActiveDirectoryUsers"
myCommand.CommandText = strsql
Try
ADReader = myCommand.ExecuteReader()
Catch ex As SqlException
MsgBox(ex.ToString())
End Try
While ADReader.Read()
i = 2
found = False
'count = lvi.SubItems.Count
'ADPName = myvalues(1, 1) & " " & myvalues(1, 2)
'memberof = '%" & cmbGroup.SelectedText & "%'"
ADFirstName = ADReader.GetString(6)
ADLastName = ADReader.GetString(1)
'If InStr(ADFirstName, " ") = 0 Then
'Else
' ADFirstName = Microsoft.VisualBasic.Left(ADFirstName, InStr(ADFirstName, " "))
'End If
While (i < 4081)
range = worksheet.Range("A" + i.ToString(), "B" + i.ToString())
myvalues = range.Cells.Value
FirstName = myvalues(1, 1)
LastName = myvalues(1, 2)
spacepos = InStr(FirstName, " ")
If spacepos = 0 Then
Else
FirstName = Microsoft.VisualBasic.Left(FirstName, spacepos - 1)
End If
If FirstName = ADFirstName And LastName = ADLastName Then
rowfoundat = i
i = 4080 ' job done get out of loop
found = True
ElseIf i = 4080 Then
NumFound = NumFound + 1
lvi = New ListViewItem
lvCompareResults.Items.Add(lvi)
lvi.SubItems.Add(NumFound)
lvi.SubItems.Add(myvalues(1, 1))
lvi.SubItems.Add(myvalues(1, 2))
count = lvi.SubItems.Count
'i = 4080 ' job done get out of loop
End If
i = i + 1
End While
j = j + 1
End While
lvCompareResults.EndUpdate()
'Update LastScanDate
myCommand.CommandText = "Update ActiveDirectoryUsers Set LastScanDate = GETDATE()"
myCommand.ExecuteNonQuery()
so i've ran a query against a table producing 1300 records. Then, I'm running down the resultset and for each row, I read the FirstName and LastName columns, then do a search for the FirstName and LastName in the Excel spreadsheet containing 4080 rows. (Please note how I'm doing the search). Basically, iterating down each column on the spreadsheet. This is not an efficient way. It's taking the process ages to complete. Can someone tell me a more efficient way to search for the values in the Excel spreadsheet.
Would really appreciate it.
Thanks