Developing Matt

My Technical Journal

Committing transactions in code

leave a comment »

I’ve always committedd/rolled back my transactions in my sql statements.  This morning mitch introduced me to the SqlTransaction object.  Thanks mitch!

Public Sub Test()

        Dim cn As New SqlConnection

        Dim cmd As SqlCommand

        Dim Tran As SqlTransaction

        Try

            Tran = cn.BeginTransaction

            For Each s As String In Now.ToString

                cmd = New SqlCommand(“insert”, cn)

                cmd.Transaction = Tran

                cmd.ExecuteNonQuery()

            Next

            Tran.Commit()

        Catch ex As Exception

            Tran.Rollback()

        Finally

            cn.Close()

        End Try

End Sub

 

Advertisements

Written by matt

June 3, 2008 at 8:36 am

Posted in Asp.net, VB.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: