Beispiel #1
0
        void RunProductTest()
        {
            var conn = new Connection(@"localhost\sqlexpress", "PRSdb");

            conn.Open();
            Products.Connection = conn;
            Vendors.Connection  = conn;

            var PRODUCT = Products.GetByPartNbr("30372");

            //VendorId = Vendors.GetByCode("DNRUS").Id;
            Console.WriteLine(PRODUCT);

            //var product = new Products() {
            //    PartNbr = "38462", Name = "Refrosted Flakes", Price = 382, Unit = "40032", PhotoPath = null, VendorId = 1
            //};
            //try {
            //    //insert
            //    var success = Products.Insert(product);
            //    //update product.id
            //    var p = Products.GetByPK(1);
            //    p.Name = "Dino Bones";
            //    success = Products.Update(p);
            //}
            //catch (Exception ex) {
            //    Console.WriteLine($"Exception occured {ex.Message}");
            //}



            //var lemons = Products.GetByPK(7);
            //Console.WriteLine(lemons);

            //var products = Products.GetAll();
            //foreach(var p in products) {
            //    Console.WriteLine($"Product {p.Name} from Vendor {p.Vendor.Name} is priced at ${p.Price}");
            //}


            var VENDOR = Vendors.GetByCode("BGHTS");

            Console.WriteLine(VENDOR);

            var prodlist = Vendors.GetProducts("DNRUS");

            Console.WriteLine(prodlist);


            conn.Close();
        }
Beispiel #2
0
        void RunProductsTest()
        {
            var conn = new Connection(@"localhost\sqlexpress", "PrsDb7");

            conn.Open();
            Products.Connection = conn;
            Vendors.Connection  = conn;

            var product = new Products()
            {
                PartNbr  = "XYZ001", Name = "XYZ Part", Price = 10, Unit = "Each", PhotoPath = null,
                VendorId = Vendors.GetByCode("AMAZ").Id
            };

            try {
                Console.WriteLine("Product list for Vendor Amazon:");
                var productList = Vendors.GetProducts("AMAZ");
                foreach (var prod in productList)
                {
                    Console.WriteLine(prod);
                }
                // insert
                //var success = Products.Insert(product);
                // update product.id = 3
                //var p = Products.GetByPartNbr("XYZ001");
                //p.Name = "Greg New Part XYZ";
                //p.VendorId = Vendors.GetByCode("XXX").Id;
                //success = Products.Update(p);

                //p = Products.GetByPartNbr("XYZ001");
                //Console.WriteLine(p);
                //success = Products.Delete(p.Id);
            } catch (Exception ex) {
                Console.WriteLine($"Exception occurred: {ex.Message}");
            }

            var products = Products.GetAll();

            foreach (var p in products)
            {
                Console.WriteLine($"Product {p.Name} from Vendor {p.Vendor.Name} is priced at {p.Price}");
            }

            conn.Close();
        }
Beispiel #3
0
        // INSTANCE METHODS, WITHIN DRIVER CLASS, CALLED ON INSTANCE OF PROGRAM CLASS (CALLED FROM MAIN)

        void RunProductsTest()
        {
            var conn = new Connection(@"localhost\sqlexpress", "PrsDb"); // instantiate/construct an object, and...

            conn.Open();                                                 // ...and call a method on this new object
            Products.Connection = conn;
            Vendors.Connection  = conn;                                  // these two lines appear here together because of GetProducts()


            //   *****  *****  New, popular, alternate syntax for constructor. Note semicolon AFTER the curly braces.  Good esp for testing.
            //   *****  *****  Can choose to initialize with any properties you want without constructing multiple constructors to do so.
            //   *****  *****  Note semicolon AFTER the curly braces.  Good esp for testing.
            var product = new Products()
            {
                PartNbr  = "XYZ001", Name = "XYKZ Part", Price = 10, Unit = "EACH", PhotoPath = null, //note LACK of semicolon on this line
                VendorId = Vendors.GetByCode("PSFX").Id                                               //The Id of Vendor Pustefix, which is 1
            };



            try {       // Here's a nice try-catch block.  It tells the caller what's wrong.
                Console.WriteLine("Product list for Vendor NYC Clown Studio:");
                var productList = Vendors.GetProducts("NYCC");
                foreach (var prod in productList)
                {
                    Console.WriteLine(prod);
                }


                // INSERT
                var success = Products.Insert(product);


                // UPDATE
                Products p = Products.GetByPartNbr("XYZ001"); //  formerly:  Products p = Products.GetByPk(4);
                p.Name     = "GregPartXYZff";
                p.VendorId = Vendors.GetByCode("NYCC").Id;    // is this working???
                success    = Products.Update(p);              // Result:  The product named "GregPartXKYZff" is now vended/sold by NYCC (New York City Clown Studio).


                //DELETE
                success = Products.Delete(1);   //Is THIS one working?????
                Console.WriteLine("\nProduct with Id=1 deleted?  " + success);


                ////This is a much better query when you need an id. Query by Part Number, don't need to knokw the id. I just know it's STEPLADD.
                var prd = Products.GetByPartNbr("STEPLADD");
                Console.WriteLine();
                Console.WriteLine(prd);
            }
            catch (Exception ex) {
                Console.WriteLine($"Exception occurred: {ex.Message}");
            }



            // GetByPk
            Products unicycle = Products.GetByPk(2);

            Console.WriteLine($"Product {unicycle.Name} from Vendor {unicycle.Vendor.Name} is priced at {unicycle.Price}"); // not sure this line is right       */
            // featuring fancier print stmt than the ToString override below in GetAll()


            // GETALL
            var products = Products.GetAll();  // List<T>

            foreach (var prod in products)
            {
                Console.WriteLine(prod);                     //  ToString override
            }

            conn.Close();
        }