Click here to Skip to main content
15,896,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I use transaction to run the code below, I want the data to rowback if any of the queries is not executed successfully.
please save my day.
VB
Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ConnectionString)
                    con.Open()
                    Using copy As New SqlBulkCopy(con)
                        copy.DestinationTableName = "trycsv"
                        copy.WriteToServer(dt)
                    End Using
                    con.Close()
                    con.Open()
                    Dim query As String = "UPDATE trycsv  SET level = @level  where level =@updlevel"
                    Using cmd As New SqlCommand(query, con)
                        cmd.Parameters.AddWithValue("@level", "1")
                        cmd.Parameters.AddWithValue("@updlevel", "O")
                        cmd.ExecuteNonQuery()
                    End Using
                    con.Close()
                End Using
Posted

I would suggest to create a stored procedure and inside the stored procedure

you can easily you the transaction block, something like below

SQL
Create Procedure ProcName(@level int, @updlevel int)
As
BEGIN

BEGIN TRAN
 UPDATE trycsv  SET level = @level  where level =@updlevel

If @@Error=0
 Commit Tran
Else
 Rollback Tran



END
 
Share this answer
 
v2
Comments
Sinisa Hajnal 11-Dec-14 5:52am    
Procedure would also make the call much clearer. And transaction can be either in code (single call) or in the procedure :)
Hi here is your Complete solution using SqlTransaction .feel free to copy .happy coding


VB
     Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ConnectionString)
               con.Open()
               Dim cmd As SqlCommand = con.CreateCommand()
           Using transaction As SqlTransaction = _
             con.BeginTransaction()

               Using bulkCopy As SqlBulkCopy = New _

SqlBulkCopy(con, _
                    SqlBulkCopyOptions.KeepIdentity, transaction)
                   bulkCopy.BatchSize = 10
                   bulkCopy.DestinationTableName = _
                    "trycsv"
                   cmd.Connection = connection
                   cmd.Transaction = transaction

                   Try

                       bulkCopy.WriteToServer(reader)
                           cmd.CommandText = _"UPDATE trycsv  SET level = @level  where level =@updlevel"
                          cmd.Parameters.AddWithValue("@level", "1")
                         cmd.Parameters.AddWithValue("@updlevel", "O")
                         cmd.ExecuteNonQuery()

                       transaction.Commit()

                   Catch ex As Exception

                       transaction.Rollback()

                   Finally
                       con.Close()
                   End Try
               End Using
           End Using
       End Using
 
Share this answer
 
v2
Comments
Sinisa Hajnal 11-Dec-14 5:51am    
5 vote. You should also dispose of connection, command and transaction in finally.
Anisuzzaman Sumon 11-Dec-14 7:09am    
Surely! Thanks Sinisa
I think the link below will solve help unless I misunderstand.

http://msdn.microsoft.com/en-us/library/ms181299.aspx[^]
 
Share this answer
 
Hi,

kindly check the below link for SQL transactions.

http://msdn.microsoft.com/en-us/library/ms188929(v=sql.110).aspx[^]
 
Share this answer
 
Comments
Member 10316149 11-Dec-14 7:42am    
thanks to you guys

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900