Пример #1
0
        public HowTo_UseTransactions()
        {
//<document>
            long _iduser = 0L;

            OGen.lib.datalayer.DBConnection _con
                = OGen.lib.datalayer.DBConnectionsupport.CreateInstance(
                      // set your db server type here
                      OGen.lib.datalayer.DBServerTypes.PostgreSQL,
                      // and connectionstring
                      "Server=127.0.0.1;Port=5432;User ID=postgres;Password=passpub;Database=OGen-NTier_UTs;"
                      );

// before beginning a transaction we need to open the connection
            _con.Open();
            try {
                // beginning transaction
                _con.Transaction.Begin();

                // performing some operations on db
                _con.Execute_SQLQuery(
                    string.Format(
                        "delete from \"User\" where \"IDUser\" = {0}",
                        _iduser.ToString()
                        )
                    );

                // commit transaction
                _con.Transaction.Commit();
            } catch (Exception _ex) {
                // rollback transaction
                _con.Transaction.Rollback();
            } finally {
                // terminate transaction
                _con.Transaction.Terminate();
            }
// closing connection
            _con.Close();
            _con.Dispose(); _con = null;
//</document>
        }
Пример #2
0
        public HowTo_UseTransactions_betweenDOs_operations()
        {
//<document>
            string _testid = DateTime.Now.Ticks.ToString();
            bool   _constraint;
            long   _iduser;
            long   _idgroup;

// we need a shared connection between Data Objects
            OGen.lib.datalayer.DBConnection _con = OGen.NTier.UTs.lib.datalayer.DO__utils.DBConnection_createInstance(
                OGen.NTier.UTs.lib.datalayer.DO__utils.DBServerType,
                OGen.NTier.UTs.lib.datalayer.DO__utils.DBConnectionstring,
                string.Empty
                );

// before beginning a transaction we need to open the connection
            _con.Open();
            try {
                // beginning transaction
                _con.Transaction.Begin();

                // sharing connection with User Data Object
                OGen.NTier.UTs.lib.datalayer.DO_User _user
                    = new OGen.NTier.UTs.lib.datalayer.DO_User(_con);
                // performing some operations on User Data Object
                _user.Fields.Login    = _testid;
                _user.Fields.Password = _testid;
                _iduser = _user.insObject(true, out _constraint);
                // handling constraint code should be added here
                _user.Dispose(); _user = null;

                // sharing connection with Group Data Object
                OGen.NTier.UTs.lib.datalayer.DO_Group _group
                    = new OGen.NTier.UTs.lib.datalayer.DO_Group(_con);
                // performing some operations on User Data Object
                _group.Fields.Name       = _testid;
                _idgroup                 = _group.insObject(true);
                _group.Dispose(); _group = null;

                // sharing connection with Group Data Object
                OGen.NTier.UTs.lib.datalayer.DO_UserGroup _usergroup
                    = new OGen.NTier.UTs.lib.datalayer.DO_UserGroup(_con);
                // performing some operations on User Data Object
                _usergroup.Fields.IDGroup         = _idgroup;
                _usergroup.Fields.IDUser          = _iduser;
                _usergroup.Fields.Relationdate    = DateTime.Now;
                _usergroup.Fields.Defaultrelation = false;
                _usergroup.setObject(false);
                _usergroup.Dispose(); _usergroup = null;

                // commit transaction
                _con.Transaction.Commit();
            } catch (Exception _ex) {
                // rollback transaction
                _con.Transaction.Rollback();
            } finally {
                // terminate transaction
                _con.Transaction.Terminate();
            }
// closing connection
            _con.Close();
            _con.Dispose(); _con = null;
//</document>
        }