Microsoft
Software
Hardware
Network
Question : Insert ArrayList into SQL Database
Good Day all,
I'm extracting some data from Active Directory and storing it in an ArrayList, as follows:
Function GetADDepartments()
Try
Dim arrDepartment As New ArrayList
Dim entry As New DirectoryServices.Director
yEntry("LD
AP://" & gLDAPPath)
Dim mySearcher As New System.DirectoryServices.D
irectorySe
archer(ent
ry)
mySearcher.Filter = ("(&(objectCategory=User))
")
Dim result As System.DirectoryServices.S
earchResul
t
Dim strDN, strDepartment As String
Dim objRecordSet, objDC
For Each result In mySearcher.FindAll
strDN = result.Path
strDepartment = result.GetDirectoryEntry()
.Propertie
s("departm
ent").Valu
e
If strDepartment <> "" Then
arrDepartment.Add(strDepar
tment)
Console.WriteLine("Found AD Department: " & strDepartment)
End If
Next
arrDepartment.Sort()
Return arrDepartment
Catch ex As Exception
Console.WriteLine(ex.Messa
ge)
End Try
End Function
Now that I have this ArrayList, I would like to insert it into a SQL table... Currnetly I'm using:
Private Sub InsertDepartment(ByVal arrDepartment As ArrayList)
Dim dbConn As SqlConnection
Dim connectstring
Dim mycmd As SqlCommand
Dim myEnumerator1 As System.Collections.IEnumer
ator
Dim mySqlDataAdapter As SqlDataAdapter
connectstring = "Data Source=" & gServer & ";Integrated Security=SSPI;" & "Initial Catalog=" & gDB
dbConn = New SqlConnection(connectstrin
g)
Try
dbConn.Open()
Dim SQLCMD1 As New SqlCommand("DELETE FROM WD_AD_DEPT_SYNC", dbConn)
SQLCMD1.ExecuteNonQuery()
myEnumerator1 = arrDepartment.GetEnumerato
r()
While myEnumerator1.MoveNext()
Dim SQLCMD As New SqlCommand("INSERT INTO WD_AD_DEPT_SYNC (DEPARTMENTNAME) VALUES ('" & myEnumerator1.Current & "')", dbConn)
Console.WriteLine("Inserti
ng " & myEnumerator1.Current & " INTO WD_AD_DEPT_SYNC")
SQLCMD.ExecuteNonQuery()
End While
dbConn.Close()
dbConn.Dispose()
Catch ex As Exception
Console.WriteLine(Err.Desc
ription)
End Try
dbConn = Nothing
End Sub
I feel I'm using too many resources and connections to the database to accomplish this task. Can anyone offer a way to bulk insert an arraylist or a better way to perform this operation?
Thanks much!
John
Answer : Insert ArrayList into SQL Database
the only this I would point out is that your Insert.... procedure declares an SQLDataAdapter object that is NOT being used.
Also, you can re-use the SQLCMD command object from the INNER loop, without the need to declare and instantiate a separtate SQLCMD1 object. But that is a relatively minor issue.
code would then look like:
Private Sub InsertDepartment(ByVal arrDepartment As ArrayList)
Dim dbConn As SqlConnection
Dim connectstring as String = "Data Source=" & gServer & ";Integrated Security=SSPI;" & "Initial Catalog=" & gDB
Dim myEnumerator As System.Collections.IEnumer
ator = arrDepartment.GetEnumerato
r()
dbConn = New SqlConnection(connectstrin
g)
Try
dbConn.Open()
Dim SQLCMD As New SqlCommand("DELETE FROM WD_AD_DEPT_SYNC", dbConn)
SQLCMD.ExecuteNonQuery()
While myEnumerator.MoveNext()
Dim SQLCMD As New SqlCommand("INSERT INTO WD_AD_DEPT_SYNC (DEPARTMENTNAME) VALUES ('" & myEnumerator1.Current & "')", dbConn)
Console.WriteLine("Inserti
ng " & myEnumerator1.Current & " INTO WD_AD_DEPT_SYNC")
SQLCMD.ExecuteNonQuery()
End While
dbConn.Close()
dbConn.Dispose()
Catch ex As Exception
Console.WriteLine(Err.Desc
ription)
End Try
dbConn = Nothing
End Sub
Just did a little house-cleaning, to tighten up the code. Should not have any impact on performance.
AW
Random Solutions
How do I change Notepad to print landscape using Access VBA
Changing Report Recordsource Property in VBA
Copied all vb files (..vb file, project, solution, user options) for a project but getting errors.
How to overline in Word2003?
Report on Form
How to upload a new version of the PDF file - Sharepoint Services 3.0?
ODBC Connection to Back End Access MDB with Front End MDE
can I resell Windows Home Server OEM after 1 months use
Silverlight: open excel in Browser
Crop Images