Solved

VB.net Avoiding duplicates in Transact Rollback update

Posted on 2014-10-27
2
152 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:murbro
2 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 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:murbro
ID: 40409950
Thanks very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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