Ejemplo n.º 1
0
        private static void Main()
        {
            var db = new Db(new MyConfig());

            // execute following code in a transaction, but don't commit
            using (new TransactionScope())
            {
                // find by id
                var emp = db.Find<Employee>(1);
                Console.WriteLine("FOUND ENTITY : \n{0}\n", emp);

                // update
                emp.EmployeeName = "Hoge";
                db.Update(emp);
                Console.WriteLine("UPDATED ENTITY : \n{0}\n", emp);

                // delete
                db.Delete(emp);
                Console.WriteLine("DELETED ENTITY : \n{0}\n", emp);

                // insert
                emp = new Employee { EmployeeName = "Allen", DepartmentId = 2 };
                db.Insert(emp);
                Console.WriteLine("INSERTED ENTITY : \n{0}\n", emp);

                // query and map results to entities. parameters are bindable with "Anonymous Types".
                var empList = db.Query<Employee>(@"
                    select
                        e.EmployeeId,
                        e.EmployeeName,
                        e.DepartmentId,
                        e.VersionNo
                    from
                        Employee e
                    where
                        e.DepartmentId = /* emp.DepartmentId */0
                    ", new { emp });
                Console.WriteLine("QUERRY RESULTS AS ENTITIES :");
                foreach (var e in empList)
                {
                    Console.WriteLine(e);
                }
                Console.WriteLine();

                // query and map results to dynamic objects. parameters are bindable with "Anonymous Types".
                var empList2 = db.Query<dynamic>(@"
                    select
                        e.EmployeeId,
                        e.EmployeeName,
                        e.DepartmentId,
                        e.VersionNo
                    from
                        Employee e
                    where
                        e.DepartmentId = /* emp.DepartmentId */0
                    ", new { emp });
                Console.WriteLine("QUERY RESULTS AS DYNAMIC OBJECTS :");
                foreach (var e in empList2)
                {
                    Console.WriteLine("EmployeeId={0}, EmployeeName={1}", e.EmployeeId, e.EmployeeName);
                }
                Console.WriteLine();

                // call procedure
                var procedure = new ProcResultAndOut { EmployeeId = 1 };
                db.Call(procedure);
                Console.WriteLine("PROCEDURE OUTPUT VALUE : \n{0}\n", procedure.EmployeeCount);
                Console.WriteLine("PROCEDURE RESULT ENTITIES :");
                foreach (var e in procedure.Employees)
                {
                    Console.WriteLine(e);
                }
                Console.WriteLine();

                // execute arbitrary SQL
                var rows = db.Execute("delete from Employee");
                Console.WriteLine("AFFECTED ROWS : \n{0}\n", rows);
            }
            Console.ReadKey();
        }