Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net Avoiding duplicates in Transact Rollback update

Posted on 2014-10-27
2
Medium Priority
?
159 Views
Last Modified: 2014-10-28
Hi
I use the following code to execute a number of SQL statements. There are often many duplicates.
Is there a way to check whether the SQL statement has already been executed and not execute it again?

       Dim oConnectionString As String = Globals.ThisAddIn.oRIGHT.lblConnectionString.Text

        Dim cmd As SqlCommand
        Dim cn As New SqlConnection(oConnectionString)
        Dim trans As SqlTransaction '+++++++ Transaction and rollback ++++++++
        Try

            cn.Open() 'open connection
            trans = cn.BeginTransaction(IsolationLevel.ReadCommitted)   '+++++++ Transaction and rollback ++++++++

            For I = 0 to UBound(arrSQL)

                          sSQL = arrSQL(I)

                            cmd = New SqlCommand(sSQL, cn)
                            cmd.Transaction = trans '+++++++ Transaction and rollback ++++++++
                            cmd.ExecuteNonQuery()

         
            Next

            trans.Commit()  '+++++++ Transaction and rollback ++++++++
            cmd = Nothing

            MsgBox("Update Successful!")
        Catch ex As Exception
            trans.Rollback() '+++++++ Transaction and rollback ++++++++
            Dim oError As String = "Upload not successful! " & ex.Message
            oAddComment(oRunRow, oRunColumn, oError)
        Finally
            If Not IsNothing(cmd) Then
                cmd.Dispose()
            End If

            If Not IsNothing(cn) Then
                cn.Dispose()
            End If
        End Try

    End Sub
0
Comment
Question by:Murray Brown
2 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 total points
ID: 40407804
The Distinct method returns only distinct entries:
For Each sSQL In arrSQL.Distinct()
        cmd = New SqlCommand(sSQL, cn)
        cmd.Transaction = trans '+++++++ Transaction and rollback ++++++++
        cmd.ExecuteNonQuery()
Next

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 40409950
Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question