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 to append data from a varchar column to a text column
In Access 2007 what expression would update a value by simply removing all underscores?
How to get Full XML node name using C#
CRM 4.0: issues pre-populating a Currency lookup and money field ("A currency is required if a value exists in a money field")
Extract sender,subject,priority,at<wbr />tachment from ms outlook using vba
Error Loading sshnas.dll
ADO.NET Insert Command
Error in Stored Proceedure Syntax
How do I close Adobe Reader from within VB.NET
Guides On Data Access Pages