locked
How does Database Transaction work in Webpages? RRS feed

  • Question

  • User-701197764 posted

    Hi,

    I am trying to use sql server database transaction in webpages site, but can not find any documents about it. Dose the Database object surport for transaction in webpages? Or I have to use ADO.NET objects in Webpages?  How can I do that?

    thanks in advance

    Raymond

    Thursday, April 9, 2015 9:20 AM

Answers

  • User-821857111 posted

    To answer your question, the Database helper does not support Transactions. You will have to use ADO.NET instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 8:09 AM

All replies

  • User1577371250 posted

    Hi,

    Check this sample

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
    
        // Start a local transaction.
        SqlTransaction sqlTran = connection.BeginTransaction();
    
        // Enlist a command in the current transaction.
        SqlCommand command = connection.CreateCommand();
        command.Transaction = sqlTran;
    
        try
        {
            // Execute two separate commands.
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')";
            command.ExecuteNonQuery();
            command.CommandText =
              "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')";
            command.ExecuteNonQuery();
    
            // Commit the transaction.
            sqlTran.Commit();
            Console.WriteLine("Both records were written to database.");
        }
        catch (Exception ex)
        {
            // Handle the exception if the transaction fails to commit.
            Console.WriteLine(ex.Message);
    
            try
            {
                // Attempt to roll back the transaction.
                sqlTran.Rollback();
            }
            catch (Exception exRollback)
            {
                // Throws an InvalidOperationException if the connection  
                // is closed or the transaction has already been rolled  
                // back on the server.
                Console.WriteLine(exRollback.Message);
            }
        }
    }

    https://msdn.microsoft.com/en-us/library/2k2hy99x(v=vs.110).aspx

    Thursday, April 9, 2015 9:33 AM
  • User-821857111 posted

    To answer your question, the Database helper does not support Transactions. You will have to use ADO.NET instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 8:09 AM