コード例 #1
0
ファイル: Program.cs プロジェクト: Sindukova/MySQL
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");

            IDbConnection conn = new MySqlConnection(connString);
            DapperDepartmentRepository repo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Hello user. Here are current departments in the database:");

            var depos = repo.GetAllDepartments();

            foreach (var depo in depos)
            {
                Console.WriteLine($"ID-{depo.DepartmentID} Name-{depo.Name}");
            }

            Console.WriteLine("Do you want to add another department?");
            String userResponce = Console.ReadLine();

            if (userResponce.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of the department you want to add: ");
                var newDep = Console.ReadLine();
                repo.InsertDepartment(newDep);
                repo.GetAllDepartments();
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();
            string        connString = config.GetConnectionString("DefaultConnection");
            IDbConnection conn       = new MySqlConnection(connString);

            DapperDepartmentRepository repo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Hello user, here are the current departments:");
            Console.WriteLine("Please press enter . . .");
            Console.ReadLine();

            var depos = repo.GetAllDepartments();

            Print(depos);

            Console.WriteLine("Do you want to add a department?");
            string userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is this new department called?");
                userResponse = Console.ReadLine();

                repo.InsertDepartment(userResponse);
                Print(repo.GetAllDepartments());
            }
            Console.WriteLine("Have a nice day!");
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: johndward01/27_SQL
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            //get the connection string value from the JSON key value pair
            var connString = config.GetConnectionString("DefaultConnection");


            //-------------------------------IoC-------------------------------
            //container to implement IoC
            var container = new Container(x =>
            {
                x.AddTransient <IDbConnection>((c) =>
                {
                    return(new MySqlConnection(connString));
                });

                x.AddTransient <IDepartmentRepository, DapperDepartmentRepository>();
            });

            //var repo = container.GetService<IDepartmentRepository>();
            /*-------------------------------IoC Ends----------------------------*/


            //-------------------------------Without Dapper------------------------
            //
            // IDbConnection conn = new MySqlConnection(connString);
            // var repo = new DepartmentRepository(conn);
            //
            // ----------------------------Without Dapper Ends-------------------*/


            //-------------------------------Dapper--------------------------------
            IDbConnection conn = new MySqlConnection(connString);
            var           repo = new DapperDepartmentRepository(conn);
            //-----------------------------Dapper Ends-----------------------------


            //Console.WriteLine("Type a new Department name");

            //var newDepartment = Console.ReadLine();

            //repo.InsertDepartment(newDepartment);

            var departments = repo.GetAllDepartments();

            Console.WriteLine("ID -- NAME \n---------------------------");
            foreach (var dept in departments)
            {
                Console.WriteLine($"{dept.DepartmentID}  -- {dept.Name}");
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: jlemartin/IntroSQL
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json")
                         .Build();

            // Creating connection string using info from appsettings.json file
            string connString = config.GetConnectionString("DefaultConnection");

            // Using that string to make connection to database
            IDbConnection conn = new MySqlConnection(connString);

            var deptRepo = new DapperDepartmentRepository(conn);

            //Console.WriteLine("Type a new Department name");

            //var newDepartment = Console.ReadLine();

            //deptRepo.InsertDepartment(newDepartment);

            var departments = deptRepo.GetAllDepartments();

            foreach (var dept in departments)
            {
                Console.WriteLine($"{dept.DepartmentID} : {dept.Name}");
            }
            Console.WriteLine();

            // Creating new Product Repository
            var prodRepo = new DapperProductRepository(conn);

            // Bringing in all Products from database
            var allProducts = prodRepo.GetAllProducts();

            // take a look
            //foreach (var prod in allProducts)
            //{
            //    Console.WriteLine($"{prod.ProductID} : {prod.Name} : {prod.Price} : {prod.CategoryID} : {prod.OnSale} : {prod.StockLevel}");
            //}

            // Adding a new item in the Products table
            //prodRepo.CreateProduct("MacBook Air", 1000.00, 1);

            //foreach (var prod in allProducts)
            //{
            //    Console.WriteLine($"{prod.ProductID} : {prod.Name} : {prod.Price} : {prod.CategoryID} : {prod.OnSale} : {prod.StockLevel}");
            //}

            //prodRepo.UpdateStockLevel(940, "100");

            // prodRepo.DeleteProduct(941);

            var allComputers  = prodRepo.GetComputers();
            var catOfProducts = prodRepo.GetCategoryProducts(2);

            //foreach (var comp in catOfProducts)
            //{
            //    Console.WriteLine($"{comp.ProductID} : {comp.Name} : {comp.Price}");
            //}

            var empRepo      = new DapperEmployeeRepository(conn);
            var allEmployees = empRepo.GetAllEmployees();

            //Console.WriteLine("What is the new employee's First Name?");
            //var FirstName = Console.ReadLine();
            //Console.WriteLine("What is the new employee's Middle Initial?");
            //var MiddleInitial = Console.ReadLine();
            //Console.WriteLine("What is the new employee's Last Name?");
            //var LastName = Console.ReadLine();
            //Console.WriteLine("What is the new employee's Job Title?");
            //var Title = Console.ReadLine();

            // empRepo.AddEmployee(FirstName, MiddleInitial, LastName, Title);

            foreach (var emp in allEmployees)
            {
                Console.WriteLine($"{emp.EmployeeID} : {emp.LastName} : {emp.Title}");
            }

            // Updating EmailAddress in database with method
            var emailAddress = "*****@*****.**";

            empRepo.UpdateEmail(34618, emailAddress);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: ruby149/DapperClass
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsetting.json")
                         .Build();

            string connString = config.GetConnectionString("DefaultConnection");

            IDbConnection conn = new MySqlConnection(connString);

            var productsRepo = new DapperDepartmentRepository(conn);

            Console.WriteLine($"What is the name of the new product?");
            var productName = Console.ReadLine();

            double priceOfProduct;
            bool   response;

            do
            {
                Console.WriteLine($"How much does {productName} cost?");
                response = double.TryParse(Console.ReadLine(), out priceOfProduct);
            }while (response == false);

            Console.WriteLine($"What is the CategoryID for {productName}?");
            var categoryID = int.Parse(Console.ReadLine());

            productsRepo.CreateProduct(productName, priceOfProduct, categoryID);

            var products = productsRepo.GetAllProducts();

            foreach (var item in products)
            {
                Console.WriteLine($"{item.productID} {item.Name} {item.Price}");
            }

            DapperDepartmentRepository repo = new DapperDepartmentRepository(conn);

            Console.WriteLine("Hello user, here are the current departments:");
            Console.WriteLine("Please press enter...");
            Console.ReadLine();
            var depos = repo.GetAllDepartments();

            foreach (var depo in depos)
            {
                Console.WriteLine($"ID:{ depo.DepartmentID} Name:{ depo.Name}");
            }

            Console.WriteLine("Do you want to add a department???");
            string userResponse = Console.ReadLine();

            if (userResponse.ToLower() == "yes")
            {
                Console.WriteLine("What is the name of your new Department???");
                userResponse = Console.ReadLine();
                repo.InsertDepartment(userResponse);
                Print(repo.GetAllDepartments());
            }

            Console.WriteLine("have a great day.");
        }