示例#1
0
        public void DeleteAll_Executes_Without_Exception()
        {
            using (var sqlTransaction = _commandBuilder.Connection.BeginTransaction())
            {
                try
                {
                    _dbOperation.DeleteAll(_dsData, _commandBuilder, sqlTransaction);
                    sqlTransaction.Commit();
                }
                catch (Exception)
                {
                    if (sqlTransaction != null)
                    {
                        sqlTransaction.Rollback();
                    }

                    throw;
                }
            }
            Assert.IsTrue(true);
        }
示例#2
0
        public void PerformDbOperation(DbOperationFlag dbOperationFlag)
        {
            checkInitialized();

            if (dbOperationFlag == DbOperationFlag.None)
            {
                return;
            }

            IDbCommandBuilder dbCommandBuilder = GetDbCommandBuilder();
            IDbOperation      dbOperation      = GetDbOperation();

            IDbTransaction dbTransaction = null;
            IDbConnection  dbConnection  = dbCommandBuilder.Connection;

            try
            {
                if (dbConnection.State != ConnectionState.Open)
                {
                    dbConnection.Open();
                }
                dbTransaction = dbConnection.BeginTransaction();

                OperationEventArgs args = new OperationEventArgs();
                args.DbTransaction = dbTransaction;

                if (null != PreOperation)
                {
                    PreOperation(this, args);
                }

                switch (dbOperationFlag)
                {
                case DbOperationFlag.Insert:
                {
                    dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction);
                    break;
                }
                //case DbOperationFlag.InsertIdentity:
                //    {
                //        dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction);
                //        break;
                //    }
                //case DbOperationFlag.Delete:
                //    {
                //        dbOperation.Delete(_dataSet, dbCommandBuilder, dbTransaction);

                //        break;
                //    }
                case DbOperationFlag.DeleteAll:
                {
                    dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                    break;
                }
                    //case DbOperationFlag.Refresh:
                    //    {
                    //        dbOperation.Refresh(_dataSet, dbCommandBuilder, dbTransaction);
                    //        break;
                    //    }
                    //case DbOperationFlag.Update:
                    //    {
                    //        dbOperation.Update(_dataSet, dbCommandBuilder, dbTransaction);
                    //        break;
                    //    }
                    //case DbOperationFlag.CleanInsert:
                    //    {
                    //        dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                    //        dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction);
                    //        break;
                    //    }
                    //case DbOperationFlag.CleanInsertIdentity:
                    //    {
                    //        dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                    //        dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction);
                    //        break;
                    //    }
                }

                if (null != PostOperation)
                {
                    PostOperation(this, args);
                }

                dbTransaction.Commit();
            }
            catch (Exception)
            {
                if (dbTransaction != null)
                {
                    dbTransaction.Rollback();
                }

                throw;
            }
            finally
            {
                if (dbTransaction != null)
                {
                    dbTransaction.Dispose();
                }

                //only close and release the connection if not externally-managed
                if (!ConnectionManager.HasExternallyManagedConnection)
                {
                    if (ConnectionState.Open == dbConnection.State)
                    {
                        dbConnection.Close();
                    }

                    ConnectionManager.ReleaseConnection();
                }
            }
        }
示例#3
0
        public void PerformDbOperation(DbOperationFlag dbOperationFlag)
        {
            checkInitialized();

            if (dbOperationFlag == DbOperationFlag.None)
            {
                return;
            }

            IDbCommandBuilder dbCommandBuilder = GetDbCommandBuilder();
            IDbOperation      dbOperation      = GetDbOperation();

            DbTransaction dbTransaction = null;
            DbConnection  dbConnection  = dbCommandBuilder.Connection;

            using (new OpenConnectionGuard(dbConnection, ConnectionManager.HasExternallyManagedConnection))
            {
                try
                {
                    dbTransaction = dbConnection.BeginTransaction();

                    OperationEventArgs args = new OperationEventArgs();
                    args.DbTransaction = dbTransaction;

                    if (null != PreOperation)
                    {
                        PreOperation(this, args);
                    }

                    switch (dbOperationFlag)
                    {
                    case DbOperationFlag.Insert:
                    {
                        dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.InsertIdentity:
                    {
                        dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.Delete:
                    {
                        dbOperation.Delete(_dataSet, dbCommandBuilder, dbTransaction);

                        break;
                    }

                    case DbOperationFlag.DeleteAll:
                    {
                        dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.Refresh:
                    {
                        dbOperation.Refresh(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.Update:
                    {
                        dbOperation.Update(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.CleanInsert:
                    {
                        dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                        dbOperation.Insert(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }

                    case DbOperationFlag.CleanInsertIdentity:
                    {
                        dbOperation.DeleteAll(_dataSet, dbCommandBuilder, dbTransaction);
                        dbOperation.InsertIdentity(_dataSet, dbCommandBuilder, dbTransaction);
                        break;
                    }
                    }

                    if (null != PostOperation)
                    {
                        PostOperation(this, args);
                    }

                    dbTransaction.Commit();
                }
                catch (Exception)
                {
                    if (dbTransaction != null)
                    {
                        dbTransaction.Rollback();
                    }

                    throw;
                }
                finally
                {
                    if (dbTransaction != null)
                    {
                        dbTransaction.Dispose();
                    }
                }
            }
        }