11-17-2023 10:10 AM - last edited on 11-21-2023 12:14 PM by JackLacava
Hi everyone.
I have this error when trying to insert data into my SQL table 'System.OutOfMemoryException'. This occurs when I use .base to retrieve all my departments.
The following is how I enter the values of a table into SQL using a loop, so I would like to know what I can do to avoid that error when I want to add many values to that table.
For Each isRow As DataRow In dt.Rows
Dim sqlInsert As String = $"INSERT INTO AllocationResults (Step, Amount, Annotation, SourceRule, DriverRule, Type, StepDesc, TargetRule, AllocRule, Generation, Cube, Entity, Consolidation, Scenario, Time, [View], Account, Flow, Origin, Ic, UD1, UD2, UD3, UD4, UD5, UD6, UD7, UD8) VALUES " &
$"(1, {Amount}, '{Annotation}', '{SourceRule}', '{DriverRule}', '{Type}', '{StepDesc}', '{TargetRule}', '{AllocRule}', {Generation}, '{Cube}', '{Entity}', '{Consolidation}', '{Scenario}', '{Time}', '{View}', '{Account}', '{Flow}', '{Origin}', '{Ic}', '{UD1}', '{UD2}', '{UD3}', '{UD4}', '{UD5}', '{UD6}', '{UD7}', '{UD8}')"
'BRApi.ErrorLog.LogMessage(si, "SQL INTO: " & sqlInsert)
Using dbConnApp As DBConnInfo = BRAPi.Database.CreateApplicationDbConnInfo(si)
BRApi.Database.ExecuteSql(dbConnApp, sqlInsert, False)
End Using
Next
Solved! Go to Solution.
11-17-2023 01:22 PM - edited 11-20-2023 09:48 AM
Hi Marco,
Instead of running the query many times inside a loop, use the loop to curate a single query and run the query once at the end:
Dim sqlInsert As New StringBuilder()
Dim insertVals As New List(Of String)
'start the query
sqlInsert.AppendLine("INSERT INTO AllocationResults (Step, Amount, Annotation, SourceRule, DriverRule, Type, StepDesc, TargetRule, AllocRule, Generation, Cube, Entity, Consolidation, Scenario, Time, [View], Account, Flow, Origin, Ic, UD1, UD2, UD3, UD4, UD5, UD6, UD7, UD8) VALUES ")
For Each isRow As DataRow In dt.Rows
'add each insert group
insertVals.Add($"(1, {Amount}, '{Annotation}', '{SourceRule}', '{DriverRule}', '{Type}', '{StepDesc}', '{TargetRule}', '{AllocRule}', {Generation}, '{Cube}', '{Entity}', '{Consolidation}', '{Scenario}', '{Time}', '{View}', '{Account}', '{Flow}', '{Origin}', '{Ic}', '{UD1}', '{UD2}', '{UD3}', '{UD4}', '{UD5}', '{UD6}', '{UD7}', '{UD8}')")
Next
'finish the query
sqlInsert.AppendLine(String.Join($",{Environment.NewLine}", insertVals.ToArray()))
'run the query once for all inserts.
Using dbConnApp As DBConnInfo = BRAPi.Database.CreateApplicationDbConnInfo(si)
BRApi.Database.ExecuteSql(dbConnApp, sqlInsert.ToString(), True)
End Using
11-20-2023 07:40 AM
Just to append to @RobbSalzmann answer, you might want to consider using the sql function call, that includes parameters, and if you still get time-outs, use the "useCommandTimeOutLarge" feature
11-17-2023 01:22 PM - edited 11-20-2023 09:48 AM
Hi Marco,
Instead of running the query many times inside a loop, use the loop to curate a single query and run the query once at the end:
Dim sqlInsert As New StringBuilder()
Dim insertVals As New List(Of String)
'start the query
sqlInsert.AppendLine("INSERT INTO AllocationResults (Step, Amount, Annotation, SourceRule, DriverRule, Type, StepDesc, TargetRule, AllocRule, Generation, Cube, Entity, Consolidation, Scenario, Time, [View], Account, Flow, Origin, Ic, UD1, UD2, UD3, UD4, UD5, UD6, UD7, UD8) VALUES ")
For Each isRow As DataRow In dt.Rows
'add each insert group
insertVals.Add($"(1, {Amount}, '{Annotation}', '{SourceRule}', '{DriverRule}', '{Type}', '{StepDesc}', '{TargetRule}', '{AllocRule}', {Generation}, '{Cube}', '{Entity}', '{Consolidation}', '{Scenario}', '{Time}', '{View}', '{Account}', '{Flow}', '{Origin}', '{Ic}', '{UD1}', '{UD2}', '{UD3}', '{UD4}', '{UD5}', '{UD6}', '{UD7}', '{UD8}')")
Next
'finish the query
sqlInsert.AppendLine(String.Join($",{Environment.NewLine}", insertVals.ToArray()))
'run the query once for all inserts.
Using dbConnApp As DBConnInfo = BRAPi.Database.CreateApplicationDbConnInfo(si)
BRApi.Database.ExecuteSql(dbConnApp, sqlInsert.ToString(), True)
End Using
11-20-2023 07:40 AM
Just to append to @RobbSalzmann answer, you might want to consider using the sql function call, that includes parameters, and if you still get time-outs, use the "useCommandTimeOutLarge" feature
11-23-2023 12:21 PM
Hi Frank, this resolves this error, and if not, is there a way to prevent it from appearing?
User Interface Stack Trace: at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
11-24-2023 04:31 AM
I see these options:
- Increase the time-out (on server configuration level)
- Split the "insert" into groups to avoid the time-out
But how many rows do you need to insert and what's the scenario for this?