|
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.DirectoryEntry("LDAP://" & gLDAPPath) Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry) mySearcher.Filter = ("(&(objectCategory=User))") Dim result As System.DirectoryServices.SearchResult Dim strDN, strDepartment As String Dim objRecordSet, objDC For Each result In mySearcher.FindAll strDN = result.Path strDepartment = result.GetDirectoryEntry().Properties("department").Value If strDepartment <> "" Then arrDepartment.Add(strDepartment) Console.WriteLine("Found AD Department: " & strDepartment) End If Next arrDepartment.Sort() Return arrDepartment
Catch ex As Exception Console.WriteLine(ex.Message) 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.IEnumerator Dim mySqlDataAdapter As SqlDataAdapter
connectstring = "Data Source=" & gServer & ";Integrated Security=SSPI;" & "Initial Catalog=" & gDB dbConn = New SqlConnection(connectstring)
Try dbConn.Open()
Dim SQLCMD1 As New SqlCommand("DELETE FROM WD_AD_DEPT_SYNC", dbConn) SQLCMD1.ExecuteNonQuery()
myEnumerator1 = arrDepartment.GetEnumerator()
While myEnumerator1.MoveNext() Dim SQLCMD As New SqlCommand("INSERT INTO WD_AD_DEPT_SYNC (DEPARTMENTNAME) VALUES ('" & myEnumerator1.Current & "')", dbConn) Console.WriteLine("Inserting " & myEnumerator1.Current & " INTO WD_AD_DEPT_SYNC") SQLCMD.ExecuteNonQuery() End While
dbConn.Close() dbConn.Dispose() Catch ex As Exception Console.WriteLine(Err.Description) 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.IEnumerator = arrDepartment.GetEnumerator() dbConn = New SqlConnection(connectstring)
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("Inserting " & myEnumerator1.Current & " INTO WD_AD_DEPT_SYNC") SQLCMD.ExecuteNonQuery() End While
dbConn.Close() dbConn.Dispose() Catch ex As Exception Console.WriteLine(Err.Description) 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
|
|
|
|