Пример #1
0
        static void LoadEmployees()
        {
            Console.WriteLine("\nLoading Employees class example..");
            Console.WriteLine();

            // Assign the connection and initialize the ORM client.
            SqlDatabaseOrmClient <Employees> emps = new SqlDatabaseOrmClient <Employees>(InMemoryConnection);

            emps.CreateTable();//Create table EmployeeMaster if it does not exists.

            // Add EmployeeMaster one by one
            // We do not need to provide EmployeeId as it is autogenerated.
            Employees emp = new Employees(); //Create new instance of Departments class

            emp.FirstName    = "John";
            emp.LastName     = "Dekota";
            emp.DepartmentId = 2;
            emps.Add(emp);

            emp = new Employees
            {
                FirstName    = "Mary",
                LastName     = "Denware",
                DepartmentId = 3
            };
            emps.Add(emp);

            emp = new Employees
            {
                FirstName    = "Scott",
                LastName     = "Hamilton",
                DepartmentId = 2
            };
            emps.Add(emp);
            // End of Add department one by one

            // Adding multiple Employees using list
            List <Employees> Employeeslist = new List <Employees>();

            Employeeslist.Add(new Employees()
            {
                FirstName = "Guru", LastName = "Manna", DepartmentId = 3
            });
            Employeeslist.Add(new Employees()
            {
                FirstName = "Robert", LastName = "Olipo", DepartmentId = 1
            });
            Employeeslist.Add(new Employees()
            {
                FirstName = "John", LastName = "Doe", DepartmentId = 3
            });
            emps.AddRange(Employeeslist);

            // Get all records.
            Console.WriteLine("Example to get all records for employees..");
            foreach (Employees e in emps.GetAll())
            {
                Console.Write(string.Format("Id: {0} \t FirstName: {1} \t LastName: {1} \t DepartmentId: {1}", e.EmployeeId, e.FirstName, e.LastName, e.DepartmentId));
                Console.WriteLine();
            }
            Console.WriteLine(); //Empty line
        }
Пример #2
0
        static void LoadDepartments()
        {
            Console.WriteLine("Loading Departments example..");
            Console.WriteLine();

            //Initalize ORM client with Departments object
            SqlDatabaseOrmClient <Departments> depts = new SqlDatabaseOrmClient <Departments>(InMemoryConnection);

            depts.CreateTable();//Create table departments if it does not exists.

            // Add department one by one
            // We do not need to provide departmentid as it is autogenerated.
            Departments dept = new Departments(); //Create new instance of Departments class

            dept.DepartmentName = "Administration";
            depts.Add(dept);

            dept = new Departments
            {
                DepartmentName = "Sales & Marketing"
            };
            depts.Add(dept);

            //Following will produce error due to DepartmentName may not be null.
            try
            {
                dept = new Departments
                {
                    DepartmentName = null
                };
                depts.Add(dept);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error due to null: {0}", e.Message);
                Console.WriteLine();
            }
            // End of Add department one by one

            // Adding multiple departments using list
            List <Departments> departmentslist = new List <Departments>();

            departmentslist.Add(new Departments()
            {
                DepartmentName = "HR"
            });
            departmentslist.Add(new Departments()
            {
                DepartmentName = "Information Technology"
            });
            depts.AddRange(departmentslist);

            // Get all records.
            Console.WriteLine("Example to get all records for departments..");
            foreach (Departments d in depts.GetAll())
            {
                Console.Write(string.Format("Id: {0} \t Name: {1}", d.DepartmentId, d.DepartmentName));
                Console.WriteLine();
            }
            Console.WriteLine(); //Empty line

            // Optional for testing, get records using sql
            Console.WriteLine("Example to fetch records using SQL statement....");
            using (SqlDatabaseCommand cmd = new SqlDatabaseCommand(InMemoryConnection))
            {
                cmd.CommandText = "SELECT * FROM Departments; ";
                SqlDatabaseDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Console.Write(string.Format("Id: {0} \t Name: {1}", dr["DepartmentId"], dr["DepartmentName"]));
                    Console.WriteLine();
                }
            }
            Console.WriteLine(); //Empty line


            //Updating existing records
            Departments DeptToUpdate = new Departments
            {
                DepartmentId   = 4,
                DepartmentName = "IT"
            };

            depts.Update(DeptToUpdate);

            //Filtering records
            SqlDatabaseOrmClient <Departments> .Filter <Departments> DeptFilter = new SqlDatabaseOrmClient <Departments> .Filter <Departments>();

            DeptFilter.WhereWithOR(item => item.DepartmentName == "HR");
            DeptFilter.WhereWithOR(item => item.DepartmentId == 4);

            DeptFilter.LimitAndOffSet(2, 0);
            DeptFilter.OrderByDescending(item => item.DepartmentId);


            // Get records based on filter.
            Console.WriteLine("Filtered Records example....");
            foreach (Departments d in depts.Find(DeptFilter))
            {
                Console.Write(string.Format("Id: {0} \t Name: {1}", d.DepartmentId, d.DepartmentName));
                Console.WriteLine();
            }

            //Drop table
            //depts.DropTable(new Departments());
        }