Solved

VB.net Avoiding duplicates in Transact Rollback update

Posted on 2014-10-27
2
148 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now