예제 #1
0
        public void TransactionTest()
        {
            using (var db = new SqlServerDatabase(Config.ConnectionString))
            {
                var countCmd = new SqlCommand("SELECT COUNT(*) FROM Person");
                var count    = db.ExecuteScalar <int>(countCmd);

                using (db.BeginTransaction())
                {
                    var nope = db.Insert("Person", new { FirstName = "Nope" });
                    Assert.IsNotNull(nope);
                    Assert.AreEqual("Nope", nope.FirstName);
                    Assert.IsNull(nope.LastName);

                    db.Update("Person", new { Id = (int)nope.Id }, new { LastName = "McSurly" });

                    var cmd = new SqlCommand("SELECT * FROM Person WHERE Id = @Id");
                    cmd.Parameters.AddWithValue("Id", (int)nope.Id);
                    var nope2 = db.GetDynamic(cmd);
                    Assert.IsNotNull(nope2);
                    Assert.AreEqual("McSurly", nope2.LastName);

                    // There is no commit, so this transaction will automatically get rolled back.
                }

                var newcount = db.ExecuteScalar <int>(countCmd);
                Assert.AreEqual(count, newcount);                 // We rolled the transaction back, so we should still have the same number of rows.
            }
        }