VB6 - Optimize multiple SQL executes.

Hi

I have the code below which i know isnt that pretty, it uses simple strings to send commands to my SQLConnection using the execute method.

However the code below can be quite slow, is there anyone with an idea to optimize this code ?
For Each cVare In getArticleContainer
        If (cVare.isSelected Or Not onlySelected) And (Not cVare.adlibitum Or afregnAdlibitum) Then
            If cVare.isBetaling Then
                sqlStr = "insert into salg_betal (salg_text,salg_value,salg_id) "
                sqlStr = sqlStr & " values('" & FQuo(cVare.vare_navn) & "'," & getSQLDecimalVal(cVare.vare_price) & "," & sellThis & ")"
                frmFront.registerCnxn.execute (sqlStr)
            Else
                frmFront.fValues = "nuværende vare: " & cVare.getCopyStr
                sqlStr = "insert into salg_linie (salg_vare,salg_antal,salg_id,salg_navn) values(" & cVare.vare_id & "," & getSQLDecimalVal(Round(cVare.vare_antal, 2)) & "," & sellThis & ",'" & FQuo(cVare.vare_navn) & "')"
                found = frmFront.registerCnxn.execute("SET NOCOUNT ON " & sqlStr & " select SCOPE_IDENTITY() as mID SET NOCOUNT OFF").Fields("mID").value
                '' Tjek om der er en besked på denne vare når den sælges.
                Call checkForMessage(cVare.vare_id)
                '' RABAT
                If cVare.rabat_procent > 0 Then
                    frmFront.registerCnxn.execute "insert into rabat_salg (salg_linieID,rabat_id,rabat_procent,person_id) values (" & found & "," & cVare.rabat_id & "," & getSQLDecimalVal(cVare.rabat_procent) & "," & pers_id & ")"
                End If
                Call checkAddArticle(frmFront.monthID, cVare.vare_id)
                If cVare.isSammensat Then
                    For Each nVare In cVare.getArticleCollection
                        Call checkAddArticle(frmFront.monthID, nVare.vare_id)
                        sqlStr = "insert into salg_sammensat (salg_linieID,salg_vare,salg_pris,salg_momsID,salg_momsnavn,salg_moms,vare_type) "
                        sqlStr = sqlStr & " select " & found & "," & nVare.vare_id & "," & getSQLDecimalVal(nVare.vare_price) & ",moms_id,moms_navn,moms_sats from moms_db inner join t_vare on moms_id=vare_moms where vare_id=" & nVare.vare_id
 
                        frmFront.fValues = frmFront.fValues & vbNewLine & sqlStr
                        frmFront.registerCnxn.execute (sqlStr)
                    Next
                Else
                    sqlStr = "insert into salg_sammensat (salg_linieID,salg_vare,salg_pris,salg_momsID,salg_momsnavn,salg_moms,vare_type) "
                    sqlStr = sqlStr & " select " & found & "," & cVare.vare_id & "," & getSQLDecimalVal(cVare.vare_price) & ",moms_id,moms_navn,moms_sats from moms_db inner join t_vare on vare_moms=moms_id where vare_id=" & cVare.vare_id
                    frmFront.fValues = frmFront.fValues & vbNewLine & sqlStr
                    frmFront.registerCnxn.execute (sqlStr)
                End If
            End If
        End If
    Next

Open in new window

mSchmidtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thiyagukTech Lead Commented:
1.You can completely Port this code to SQL Stored procedure.
2.try to Form multiple insert statements like
"insert into tablename values (blah,blah,blah);insert into tablename"

Construct this type of sql statement and try to run only once. Definitely this will solve the problem
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mSchmidtAuthor Commented:
How would you go about doing this a a Stored procedure.
I have n number of items in my ArticleContainer and each article may consist of multiple sub articles.
Sending this data as parameters seems impossible.
However it would ofcause be the best situation because it would limit data being sent to the server to the absolute minimum.


Additionally doing this as multiple insert statements joined would be possible for the inner loop however i use a value returned from a former execute and append this within

sqlStr = "insert into salg_linie (salg_vare,salg_antal,salg_id,salg_navn) values(" & cVare.vare_id & "," & getSQLDecimalVal(Round(cVare.vare_antal, 2)) & "," & sellThis & ",'" & FQuo(cVare.vare_navn) & "')"
found = frmFront.registerCnxn.execute("SET NOCOUNT ON " & sqlStr & " select SCOPE_IDENTITY() as mID SET NOCOUNT OFF").Fields("mID").value

I dont see how this would be possible ?
               
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.