I have a small job that takes a text file of email/zip codes and inserts them into a sql server 2005 table. It reads each line of the source file, checks to make sure that it parses into a valid email address/zip code, creates a sql insert command, adds it to a string builder and eventually executes it. I want to do a single execute instead of individual sql calls as there will possibly be several thousand inserts.
This works, but I'd like to know if this is a bad approach. What's the best practice here?
Dim SqlString As String = "insert into [CollectedEmail] values('{0}','{1}');"
Do Until sourceFile.Peek = -1
line = sourceFile.ReadLine
If line.Length > 0 Then
Dim emailAddress As String = Trim(line.Substring(0, EmailLength))
Dim zipcode As String = Trim(line.Substring(EmailLength + 2, ZipCodeLength))
If CVal.validEmail(emailAddress) AndAlso CVal.validZip(zipcode) Then
SQL.AppendLine(String.Format(SqlString, emailAddress, zipcode))
ElseIf CVal.validEmail(emailAddress) Then
SQL.AppendLine(String.Format(SqlString, emailAddress, ""))
Else
badAddresses.WriteLine(emailAddress)
End If
End If
Loop
InsertToDatabase(SQL.ToString)
Thank you