Пример #1
0
        public virtual int Delete([NotNull] string tableName, [NotNull] DbParameter rowSelector, bool useTransactions = true)
        {
            tableName.ExceptionIfNullOrEmpty("tableName");
            rowSelector.ExceptionIfNull("rowSelector");

            Guid marker = Trace.GetMarker();

            using (var connection = this.GetConnection())
            {
                try
                {
                    connection.Open();
                    Trace.Write(marker, "Delete() Connection Open");

                    using (var transaction = connection.BeginTransaction(useTransactions))
                    {
                        if (useTransactions)
                        {
                            Trace.Write(marker, "Delete() transaction started");
                        }

                        using (var command = connection.CreateCommand().Text())
                        {
                            command.Transaction = transaction;

                            command.CommandText = string.Format(
                                "DELETE FROM {0} WHERE {1} = {2}"
                                , QuoteName(tableName)
                                , QuoteName(rowSelector.SourceColumn)
                                , SqlVariable(rowSelector.ParameterName)
                                );

                            command.Parameters.Add(rowSelector);

                            try
                            {
                                var result = command.ExecuteNonQuery();
                                Trace.Write(marker, "Delete() completed");
                                transaction.Commit();
                                if (useTransactions)
                                {
                                    Trace.Write(marker, "Delete() transaction commit");
                                }
                                return(result);
                            }
                            catch (Exception ex)
                            {
                                Trace.Write(marker, "Delete() failed: {0}", ex);
                                transaction.Rollback();
                                if (useTransactions)
                                {
                                    Trace.Write(marker, "Delete() transaction rollback");
                                }
                                throw new StorageException(
                                          SR.StorageDeleteException.FormatWith(tableName, ex.Message), ex);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.Write(marker, "Delete() failed at {0}: {1}", tableName, ex);
                    throw new StorageException(SR.StorageDeleteException.FormatWith(tableName, ex.Message), ex);
                }
            }
        }