Solved

VB.net Avoiding duplicates in Transact Rollback update

Posted on 2014-10-27
2
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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