Solved

VB.net Avoiding duplicates in Transact Rollback update

Posted on 2014-10-27
2
155 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
[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:Murray Brown
ID: 40409950
Thanks very much
0

Featured Post

Industry Leaders: 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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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