コード例 #1
0
        //Delete an auction
        internal bool DeleteAuctionById(int id)
        {
            bool successful = false;
            var  options    = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.RepeatableRead
            };

            ImageHandler imageHandler = new ImageHandler();

            bool deleted = false;
            //queries statements
            string deleteAuction = "DELETE FROM Auction WHERE id = @Id";
            string deleteImage   = "DELETE FROM Image WHERE Auction_Id = @Id";
            string deleteBid     = "DELETE FROM Bid WHERE Auction_Id = @Id";
            string getUser       = "******";

            using (var scope = new TransactionScope(TransactionScopeOption.Required, options)) {
                using (var conn = new SqlConnection(_connectionString))
                {
                    try
                    {
                        conn.Open();
                        using (var scopeGetAndCalc = new TransactionScope(TransactionScopeOption.Required, options))
                        {
                            using (var DBAuction = new SqlCommand(getUser, conn))
                            {
                                int user_id = 0;
                                DBAuction.Parameters.AddWithValue("@Id", id);
                                SqlDataReader reader = DBAuction.ExecuteReader();

                                while (reader.Read())
                                {
                                    user_id = reader.GetInt32(0);
                                }
                                reader.Close();
                                imageHandler.DeleteAuctionFolder(id, user_id);   //delete auctionfolder with images
                            }

                            using (var DBAuction = new SqlCommand(deleteImage, conn))
                            {
                                DBAuction.Parameters.AddWithValue("@Id", id);
                                DBAuction.ExecuteNonQuery();
                            }

                            using (var DBAuction = new SqlCommand(deleteBid, conn))
                            {
                                DBAuction.Parameters.AddWithValue("@Id", id);
                                DBAuction.ExecuteNonQuery();
                            }
                            using (var DBAuctions = new SqlCommand(deleteAuction, conn))
                            {   //add the @Id and the id value from the parameters
                                DBAuctions.Parameters.AddWithValue("@Id", id);

                                DBAuctions.ExecuteNonQuery();
                            }

                            scopeGetAndCalc.Complete(); //close nested transaction
                            successful = true;
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
                scope.Complete();//close whole transaction
            }

            return(successful);
        }
コード例 #2
0
        /// <summary>
        /// Inserts pictures into database and folders.
        /// </summary>
        /// <param name="images"></param>
        /// <returns></returns>
        public bool InsertPictures(List <ImageData> images)
        {
            bool successful = false;

            //Set isolation level
            var options = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.ReadCommitted
            };

            string insertImage = "INSERT INTO Image (Auction_Id, Img_URL, DateAdded, Description, Name) " +
                                 "VALUES(@auctionId,@imgUrl,@dateAdded, @description, @name)";

            //Transaction
            using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                using (var conn = new SqlConnection(_connectionString))
                {
                    try
                    {
                        //Open connection to database.
                        conn.Open();

                        foreach (ImageData image in images)
                        {
                            using (var cmdIImage = new SqlCommand(insertImage, conn))
                            {
                                cmdIImage.Parameters.AddWithValue("auctionId", image.AuctionId);
                                cmdIImage.Parameters.AddWithValue("imgUrl", image.ImgUrl);
                                cmdIImage.Parameters.AddWithValue("dateAdded", image.DateAdded);
                                cmdIImage.Parameters.AddWithValue("description", image.Description);
                                cmdIImage.Parameters.AddWithValue("name", image.FileName);

                                cmdIImage.ExecuteScalar();
                            }
                        }

                        ImageHandler iHandler   = new ImageHandler();
                        bool         succsesful = iHandler.InsertPicturesToFolder(images);
                        if (!succsesful)
                        {
                            scope.Dispose();
                            return(successful = false);
                        }


                        //If everything went well, will commit.
                        scope.Complete();
                    }
                    catch (TransactionAbortedException e)
                    {
                        successful = false;
                        throw e;
                    }
                    finally
                    {
                        scope.Dispose();
                    }
                }
            }
            return(successful);
        }