Reputation: 15
I'm trying to import records from a memory Stream into a custom entity called new_transactions. In this instance I have 282 records in the memory stream (file size and format varies) I have come up with this code, but it takes about 4 1/2 minutes to import the 282 records. I am looking for suggestions on how this could be speeded up if possible.
Dim tsortcode As String = String.Empty
Dim taccountnumber As String = String.Empty
Dim taccountname As String = String.Empty
Dim tbankref As String = String.Empty
Dim tvalue As Decimal = 0.00
Dim ImportLine As String = String.Empty
Dim LineArray As String()
Dim tcontext As New DynamicsCRMContext
Dim FileToRead = (From f In tcontext.Annotation Where f.Id = RecordGuid).FirstOrDefault
Dim memStream As New MemoryStream
memStream = ExportFile(FileToRead.FileName, FileToRead.DocumentBody,
FileToRead.MimeType)
Dim stream As New StreamReader(memStream)
Dim IgnoreLines As Integer = 0
For IgnoreLines = 0 To _selectedtranslator.new_skiplines - 1
stream.ReadLine()
Next
Do While Not stream.EndOfStream
ImportLine = FlushFieldCommas(Replace(stream.ReadLine, """", ""))
LineArray = Split(ImportLine, ",")
' Sort Code -
tsortcode = Right("000000" & Replace(LineArray(0), "-", ""), 6)
' Account Number
taccountnumber = Right("00000000" & Replace(LineArray(1), "-", ""), 8)
' Account Name
taccountname = Right(Space(18) & LineArray(2), 18)
' Bank Ref
tbankref = Right(Space(18) & LineArray(7), 18)
'Value
tvalue = CDec(LineArray(3))
Try
Dim _addtransaction As New DynamicsCRMContext
Dim _addtr = _addtransaction.Transactions.Create
_addtr.Id = Guid.NewGuid.ToString
_addtr.new_transactionsetid_id = TsetRecordGuid
_addtr.new_sortcode = tsortcode
_addtr.new_accountnumber = taccountnumber
_addtr.new_accountname = UCase(taccountname)
_addtr.new_reference = UCase(tbankref)
_addtr.new_value = Double.Parse(tvalue)
_addtr.new_transactioncode = "99"
_addtr.new_createdby_id = _userid
_addtr.new_createdmethod = "IMPORTED"
_addtransaction.Transactions.Add(_addtr)
_addtransaction.SaveChanges()
Catch extr As Exception
WriteToErrorLog("Add Transaction " & extr.Message.ToString)
WriteToErrorLog(extr.InnerException.ToString)
End Try
Loop
Upvotes: 0
Views: 16