예제 #1
0
        public void 交易()
        {
            this.Inserts();
            var memberToDb = new Member
            {
                ID     = 1,
                NAME   = "yao",
                AGE    = 20,
                REMARK = TestHook.TestData
            };

            using (var db = new MemberDb())
            {
                db.BeginTransaction();
                try
                {
                    db.Insert(memberToDb);
                    throw new Exception();

                    db.CommitTransaction();
                }
                catch (Exception e)
                {
                    db.RollbackTransaction();
                }
            }
        }
예제 #2
0
        private void Inserts()
        {
            var membersToDb    = new List <Member>();
            var identitiesToDb = new List <Identity>();

            for (int i = 0; i < 10; i++)
            {
                membersToDb.Add(new Member
                {
                    ID     = i + 1,
                    NAME   = Name.FullName(),
                    AGE    = RandomNumber.Next(1, 120),
                    REMARK = TestHook.TestData
                });
                identitiesToDb.Add(new Identity
                {
                    MEMBER_ID = membersToDb[i].ID,
                    ACCOUNT   = Name.First(),
                    PASSWORD  = "******",
                    REMARK    = TestHook.TestData
                });
            }

            using (var db = new MemberDb())
            {
                db.BeginTransaction();
                try
                {
                    //db.BulkCopy(membersToDb);
                    for (int i = 0; i < membersToDb.Count; i++)
                    {
                        var member   = membersToDb[i];
                        var identity = identitiesToDb[i];
                        db.Insert(member);
                        db.Insert(identity);
                    }

                    db.CommitTransaction();
                }
                catch (Exception e)
                {
                    Console.WriteLine(db.LastQuery);
                    db.RollbackTransaction();
                }
            }
        }
예제 #3
0
        public static void Delete()
        {
            using (var db = new MemberDb())
            {
                db.BeginTransaction();
                try
                {
                    db.Members.Where(p => p.REMARK == TestData).Delete();
                    db.Identities.Where(p => p.REMARK == TestData).Delete();

                    db.CommitTransaction();
                }
                catch (Exception e)
                {
                    Console.WriteLine(db.LastQuery);
                    Console.WriteLine(e);
                    db.RollbackTransaction();
                    throw;
                }
            }
        }