SQL transaction

  • how to use sql transaction object?

    what possible errors will cause?

  • You need to instatiate an instance of the Transaction Scope class. The following is an example of how you would do this:

    using (TransactionScope ts = new TransactionScope())

    {

    using (SqlConnection conn = new SqlConnection("context connection=true")

    {

    try

    {

    conn.Open;

    SqlCommand cmd = new SqlCommand("Insert into SomeTable Values (SomeValues");

    cmd.ExecuteNonQuery();

    }

    catch (SqlException ex)

    {

    SqlContext.Pipe.Send(ex.Message.ToString());

    }

    finally

    {

    ts.Complete(); //commit or rollback the transaction here

    conn.Close();

    }

    }

    }

    Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
    My Blog | Twitter | MVP Profile
    Training | Consulting | Become a SQLskills Insider
    Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply