Re: Urgent problem: Any help greatly appreciated

From: David Browne (meat_at_hotmail.com)
Date: 06/10/04


Date: Thu, 10 Jun 2004 13:06:12 -0500


"Simon Harvey" <simon.harvey@the-web-works.co.uk> wrote in message
news:%23bue9rvTEHA.3404@TK2MSFTNGP10.phx.gbl...
> Hi everyone,
>
> I'm having a problem that I don't know how to sort.
>
> I am trying to execute a number of SQL stored procedures in a single
> transaction. However it always throughs an exception saying that the
"Thread
> was being aborted"
> I really need to be able to execute these procedures in a single
transaction
> so that the data doesnt become corrupt.
>
> If anyone could help me I would be very greatful.
>
> Sincerest thanks and kindest regards
>
OK, where to start.

First, don't return bool. Just use a void function. If anything goes wrong
throw an exception.

Second don't do a ASP.NET redirect in this function. It belongs in the
catch block of the code which invokes this function. Not only is is "tier
mixing", the redirect is implemented with a ThreadAbortException, and it can
be confusing to follow the thread of execution.

Third, you need to guaratee that the connection gets closed.

Forth, simplify the program flow.

Try this instead

  public static SqlConnection connect()
  {
     SqlConnection con = new SqlConnection(connectionString);
     con.Open();
     return con;
  }
  public static void executeBatchTransaction(ArrayList cmds)
  {
    using (SqlConnection con = connect())
    {
      SqlTransaction trans = con.BeginTransaction();;
      for (int i = 0; i < cmds.Count; i++)
      {
        SqlCommand cmd = (SqlCommand)cmds[i];
        cmd.Transaction = trans;
        try
        {
          cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
          trans.Rollback();
          throw;
        }
      }
      trans.Commit();
    }
  }

David



Relevant Pages

  • Re: Urgent problem: Any help greatly appreciated
    ... > I am trying to execute a number of SQL stored procedures in a single ... Second don't do a ASP.NET redirect in this function. ... public static SqlConnection connect ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Urgent problem: Any help greatly appreciated
    ... > I am trying to execute a number of SQL stored procedures in a single ... Second don't do a ASP.NET redirect in this function. ... public static SqlConnection connect ...
    (microsoft.public.dotnet.framework.adonet)
  • Urgent problem: Any help greatly appreciated
    ... I am trying to execute a number of SQL stored procedures in a single ... I really need to be able to execute these procedures in a single transaction ... SqlTransaction trans; ...
    (microsoft.public.dotnet.framework.adonet)
  • Urgent problem: Any help greatly appreciated
    ... I am trying to execute a number of SQL stored procedures in a single ... I really need to be able to execute these procedures in a single transaction ... SqlTransaction trans; ...
    (microsoft.public.dotnet.general)
  • Urgent problem: Any help greatly appreciated
    ... I am trying to execute a number of SQL stored procedures in a single ... I really need to be able to execute these procedures in a single transaction ... SqlTransaction trans; ...
    (microsoft.public.dotnet.languages.csharp)