Write T-SQL to SqlCommand.Text directly VS using SqlConnection.BeginTransaction

  • Thread starter Thread starter Skypea
  • Start date Start date
S

Skypea

Guest
Does writing a T-SQL to single SqlCommand.Text directly has a same effect with using a SqlConnection.BeginTransaction with multiple SqlCommand? Is it safe if I use the example below style? Because if I use the SqlConnection.BeginTransaction, I have to write a lot of SqlCommand in some case.

Example:

Dim sb As New StringBuilder
sb.AppendLine("BEGIN TRAN tr1")
sb.AppendLine("BEGIN TRY")
sb.AppendLine("-- INSERT, UPDATE, DELETE COMMANDS")
sb.AppendLine("COMMIT;")
sb.AppendLine("END TRY")
sb.AppendLine("BEGIN CATCH")
sb.AppendLine("ROLLBACK;")
sb.AppendLine("END CATCH")

using conn As SqlConnection = GetConnection()
Using cmd As New SqlCommand(sb.ToString(), conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using

Continue reading...
 
Back
Top