Ejemplo n.º 1
0
        public void TransactionTest()
        {
            using (var transaction = db.BeginTransaction())
            {
                Assert.AreEqual(transaction.T("INSERT INTO testTable ( Name, Content ) VALUES ( {...} )", "Ivony", "Test").ExecuteNonQuery(), 1, "插入数据测试失败");
                Assert.AreEqual(transaction.T("SELECT * FROM testTable").ExecuteDynamics().Length, 1, "插入数据后查询测试失败");
            }

            Assert.AreEqual(db.T("SELECT * FROM testTable").ExecuteDynamics().Length, 0, "自动回滚事务测试失败");

            using (var transaction = db.BeginTransaction())
            {
                Assert.AreEqual(transaction.T("INSERT INTO testTable ( Name, Content) VALUES ( {...} )", "Ivony", "Test").ExecuteNonQuery(), 1, "插入数据测试失败");
                Assert.AreEqual(transaction.T("SELECT * FROM testTable").ExecuteDynamics().Length, 1, "插入数据后查询测试失败");

                transaction.Rollback();
            }

            Assert.AreEqual(db.T("SELECT * FROM testTable").ExecuteDynamics().Length, 0, "手动回滚事务测试失败");



            using (var transaction = db.BeginTransaction())
            {
                Assert.AreEqual(transaction.T("INSERT INTO testTable ( Name, Content) VALUES ( {...} )", "Ivony", "Test").ExecuteNonQuery(), 1, "插入数据测试失败");
                Assert.AreEqual(transaction.T("SELECT * FROM testTable").ExecuteDynamics().Length, 1, "插入数据后查询测试失败");

                transaction.Commit();
            }

            Assert.AreEqual(db.T("SELECT * FROM testTable").ExecuteDynamics().Length, 1, "手动提交事务测试失败");



            {
                Exception exception   = null;
                var       transaction = (MySqlDbTransactionContext)db.BeginTransaction();

                try
                {
                    using ( transaction )
                    {
                        transaction.T("SELECT * FROM Nothing").ExecuteNonQuery();
                        transaction.Commit();
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }

                Assert.IsNotNull(exception, "事务中出现异常测试失败");
                Assert.AreEqual(transaction.Connection.State, ConnectionState.Closed);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <returns>0=成功;1=帐号密码错误;2=帐号已经过期;3=帐号已经锁定</returns>
        public int AdminLogin(string F002TB001, string F003TB001, string F006TB001, ref EMC.Model.tb001 model, ref string Message)
        {
            int res = 1;

            #region 管理员登录
            string sqlStr = "select * from TB001 where F002TB001={0} and F003TB001={1}";
            model = db.T(sqlStr, F002TB001, F003TB001).ExecuteDynamicObject();
            if (model != null)
            {
                if (model.F004TB001 > DateTime.Now || model.F005TB001 < DateTime.Now)
                {
                    res     = 2;
                    Message = "帐号已经过期";
                }
                else if (model.F099TB001 != 1)
                {
                    res     = 3;
                    Message = "帐号已经锁定";
                }
                else
                {
                    res     = 0;
                    Message = "登录成功";
                }
            }
            else
            {
                res     = 1;
                Message = "用户名或者密码错误";
            }
            #endregion
            #region 插入登录日志
            try
            {
                using (var tran = db.BeginTransaction())
                {
                    //更新最后登录时间日志
                    if (res == 0)
                    {
                        tran.T("update TB001 set F006TB001={1},F007TB001=now() where NOIDTB001={0}", model.NOIDTB001, F006TB001).ExecuteNonQuery();
                    }

                    //插入登录日志
                    Model.tb1001 model_TB1001 = new Model.tb1001();
                    model_TB1001.F001TB1001 = F002TB001;
                    model_TB1001.F002TB1001 = F003TB001;
                    model_TB1001.F003TB1001 = res;
                    model_TB1001.F004TB1001 = Message;
                    model_TB1001.F005TB1001 = F006TB001;
                    model_TB1001.TIMETB1001 = DateTime.Now;

                    TModel tmodel = EMC.DbUtility.Utility.GetInsertTModel <EMC.Model.tb1001>(model_TB1001);
                    tran.T(tmodel.SQL, tmodel.ParameterValues).ExecuteNonQuery();

                    tran.Commit();
                }
            }
            catch
            {
                //插入日志失败 暂不做处理
            }
            #endregion
            return(res);
        }