Wednesday, 11 July 2012

Transaction in ASPX page Example

  public void Test()
        {
            SqlTransaction trans;
            string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connStr);
            try
            {
                connection.Open();
                trans = connection.BeginTransaction();
                String queryString = "INSERT SOME STUFF";
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Transaction = trans;
                command.ExecuteNonQuery();
                queryString = "INSERT SOME OTHER STUFF";
                command = new SqlCommand(queryString, connection);
                command.Transaction = trans;
                command.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();
                //errMsg.Text = ex.Message;
            }
            finally
            {
                if (connection != null)
                    connection.Close();
            }
        }

No comments:

Post a Comment