Exemplo n.º 1
0
        /// <summary>
        /// Shows how to simply update a single value without bothering to fetch data from the
        /// database first.
        /// </summary>
        public static void UpdateBirthDate()
        {
            var p = new Person();

            p.Id        = 1002;
            p.BirthDate = DateTime.Now.AddYears(-10);

            // Validate the data before saving it (only validates the fields that are set).
            var errs = p.Validate();

            if (errs.Count() > 0)
            {
                Console.WriteLine("ERROR!!!");
                foreach (var err in errs)
                {
                    Console.WriteLine(err.ErrorMessage);
                }
            }
            else
            {
                using (var db = BaDatabase.Create("MyDatabase"))
                {
                    db.Save(p);

                    var p2 = db.Get <Person>(new { p.Id }).FirstOrDefault();
                    Console.WriteLine($"[{p2.Id}] {p2.FirstName} {p2.LastName}, born {p2.BirthDate:D}.");
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Shows how to use GetDynamic for stored procedures.
 /// </summary>
 public static void ExtractDataFromSproc()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         // NOTE: GetObjects is an extension method from BizArk.Data.SprocExt.
         var bart = db.GetDynamic("MyPeopleSproc", new { Id = 1003 });
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Shows how to save data.
 /// </summary>
 public static void SaveData()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         var cmd = new SqlCommand("UPDATE Person SET BirthDate = @BirthDate WHERE PersonID = @PersonID");
         cmd.AddParameters(new { BirthDate = DateTime.Now.AddYears(-10) });
         db.ExecuteNonQuery(cmd);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Shows how to use GetDynamics to extract data.
 /// </summary>
 /// <remarks>
 /// This is the recommended way to extract data into dynamic objects. Dynamic objects are
 /// particularly useful when you are simply taking data from the database and stuffing it
 /// into a response (such as a JSON blob in an API request or using it in a CSHTML file).
 /// </remarks>
 public static void ExtractDynamicData()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         var cmd = new SqlCommand("SELECT * FROM Person");
         // NOTE: GetDynamics is an extension method from BizArk.Data.ExtractExt.
         var people = db.GetDynamics(cmd);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Shows how to use a BizArk style repository.
 /// </summary>
 public void ExtensionRepository()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         // Cast BaDatabase to an IBaRepository so that when we use it, we don't have a
         // bunch of BaDatabase methods cluttering the interface.
         var rep = (IBaRepository)db;
         rep.GetPerson(1002);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Shows how to use TryTransaction.
 /// </summary>
 public static void TryTransaction()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         db.TryTransaction(() =>
         {
             // Some complex database updates
             // NOTE: There is no need to call commit or rollback. It's handled for you.
         });
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Shows how to use BeginTransaction.
        /// </summary>
        public static void BeginTransaction()
        {
            using (var db = BaDatabase.Create("MyDatabase"))
                using (var trans = db.BeginTransaction())
                {
                    // Some complex database updates

                    // NOTE: Must call commit, but rollback is called automatically if you don't call
                    // Commit before the transaction is disposed.
                    trans.Commit();
                }
        }
Exemplo n.º 8
0
        private static DataTable GetSchema()
        {
            if (sSchema == null)
            {
                // Use a short-lived database connection so that it doesn't get caught up in any transactions.
                // Since sSchema is static, it should only need to happen once.
                using (var db = BaDatabase.Create("MyDatabase"))
                {
                    sSchema = db.GetSchema("Person");
                }
            }

            return(sSchema);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Shows how to use GetObjects to extract data.
 /// </summary>
 /// <remarks>
 /// This is the recommended way to extract data into strongly typed objects.
 /// </remarks>
 public static void ExtractStronglyTypedData()
 {
     using (var db = BaDatabase.Create("MyDatabase"))
     {
         var cmd = new SqlCommand("SELECT * FROM Person");
         // NOTE: GetObjects is an extension method from BizArk.Data.ExtractExt.
         var people = db.GetObjects <Person>(cmd, rdr =>
         {
             return(new Person()
             {
                 Id = rdr.GetInt("Id"),
                 FirstName = rdr.GetString("FirstName"),
                 LastName = rdr.GetString("LastName"),
                 BirthDate = rdr.GetDateTime("BirthDate")
             });
         });
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Shows how to use ExecuteReader to extract data.
        /// </summary>
        /// <remarks>
        /// This is the most basic way to extract data. Typically you would use one of the other
        /// extraction methods, such as GetObjects().
        /// </remarks>
        public static void ExtractDataUsingExecuteReader()
        {
            using (var db = BaDatabase.Create("MyDatabase"))
            {
                var cmd    = new SqlCommand("SELECT * FROM Person");
                var people = new List <Person>();
                db.ExecuteReader(cmd, (rdr) =>
                {
                    var p = new Person()
                    {
                        Id        = rdr.GetInt32(0),
                        FirstName = rdr.GetString(1),
                        LastName  = rdr.GetString(2),
                        BirthDate = rdr.GetDateTime(3)
                    };
                    people.Add(p);

                    return(true);
                });
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Shows how to use a strongly typed BaTableObject, including validation and saving it to
        /// the database.
        /// </summary>
        public static void CreatePerson()
        {
            var p = new Person();

            p.FirstName = "Bartlet";
            p.LastName  = "Simpson";
            p.BirthDate = DateTime.Now.AddYears(-10);

            // Validate the data before saving it.
            var errs = p.Validate();

            if (errs.Count() > 0)
            {
                Console.WriteLine("ERROR!!!");
                foreach (var err in errs)
                {
                    Console.WriteLine(err.ErrorMessage);
                }
            }
            else
            {
                using (var db = BaDatabase.Create("MyDatabase"))
                    using (var trans = db.BeginTransaction())
                    {
                        // NOTE: Save is an extension method from BizArk.Data.SqlServer.BaTableExt.
                        db.Save(p);

                        // When Save is called on a new record, the object is updated with the values
                        // from the database after inserting. So the object will have the identity
                        // field (and any other defaults that are set on INSERT).

                        var p2 = db.Get <Person>(new { p.Id }).FirstOrDefault();
                        Console.WriteLine($"[{p2.Id}] {p2.FirstName} {p2.LastName}, born {p2.BirthDate:D}.");

                        trans.Commit();
                    }
            }
        }
Exemplo n.º 12
0
        public void RegisterDbFactoryTest()
        {
            Assert.ThrowsException <InvalidOperationException>(() =>
            {
                BaDatabase.Create("test");
            });

            BaDatabase.Register("test", () =>
            {
                return(new SqlServerDatabase("test"));
            });

            var db = BaDatabase.Create("test");

            Assert.IsNotNull(db);
            Assert.IsInstanceOfType(db, typeof(SqlServerDatabase));

            BaDatabase.Unregister("test");
            Assert.ThrowsException <ArgumentException>(() =>
            {
                BaDatabase.Create("test");
            });
        }
Exemplo n.º 13
0
        public void RegisterSqlServerDbFactoryTest()
        {
            var connStr1 = "db=test1";
            var connStr2 = "db=test2";

            var factory = new SqlServerDbFactory(connStr1);

            BaDatabase.Register("test1", factory.Create);

            factory = new SqlServerDbFactory(connStr2);
            BaDatabase.Register("test2", factory.Create);

            var db = BaDatabase.Create("test1") as SqlServerDatabase;

            Assert.IsNotNull(db);
            Assert.AreEqual(connStr1, db.ConnectionString);

            db = BaDatabase.Create("test2") as SqlServerDatabase;
            Assert.IsNotNull(db);
            Assert.AreEqual(connStr2, db.ConnectionString);

            BaDatabase.Unregister("test1");
            BaDatabase.Unregister("test2");
        }