{"id":46,"date":"2015-12-19T05:33:15","date_gmt":"2015-12-19T05:33:15","guid":{"rendered":"http:\/\/windows.emacslisp.com\/?p=46"},"modified":"2015-12-19T05:37:29","modified_gmt":"2015-12-19T05:37:29","slug":"pet-shop-4-0-database-map-to-object","status":"publish","type":"post","link":"http:\/\/windows.emacslisp.com\/index.php\/2015\/12\/19\/pet-shop-4-0-database-map-to-object\/","title":{"rendered":"Pet Shop 4.0 &#8211; DataBase Map to Object"},"content":{"rendered":"<p>Pet shop uses very simple to map database columns to objects.<\/p>\n<p>Here are database&#8217;s table:<br \/>\n<a href=\"http:\/\/windows.emacslisp.com\/wp-content\/uploads\/2015\/12\/mspetshop-database.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/windows.emacslisp.com\/wp-content\/uploads\/2015\/12\/mspetshop-database.png\" alt=\"mspetshop-database\" width=\"230\" height=\"225\" class=\"alignnone size-full wp-image-47\" \/><\/a><\/p>\n<p>Take Production Table for example,<\/p>\n<p>dbo.Production will map to <strong>ProudctionInfo<\/strong> in PetShop.Model.<\/p>\n<pre lang=\"csharp\" line=\"1\"> \r\n            \r\n            string connectionString = @\"server=server-running-sqlserver;database=MSPetShop4;integrated security=SSPI;min pool size=4;max pool size=4;\";\r\n\/\/ to get this connection string , you have to decrypted connection section on web.config\r\n\/\/http:\/\/windows.emacslisp.com\/index.php\/2015\/12\/11\/pet-shop-4-0-encrypt-webconfig-section-and-decrypt-webconfig-section-connectionstring\/\r\n \r\n            string productionString = @\"SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product\";\r\n            List<ProductInfo> products = new List<ProductInfo>();\r\n            ProductInfo product = null;\r\n\r\n            SqlCommand cmd = new SqlCommand();\r\n            SqlConnection conn = new SqlConnection(connectionString);\r\n\r\n            if (conn.State != ConnectionState.Open)\r\n                conn.Open();\r\n\r\n            cmd.Connection = conn;\r\n            cmd.CommandText = productionString;\r\n\r\n\r\n            cmd.CommandType = CommandType.Text;\r\n\r\n            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);\r\n            cmd.Parameters.Clear();\r\n\r\n            while (rdr.Read())\r\n            {\r\n                product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));\r\n                products.Add(product);\r\n            }\r\n\r\n            foreach (ProductInfo p in products)\r\n            {\r\n                Console.WriteLine(p.Image);\r\n            }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pet shop uses very simple to map database columns to objects. Here are database&#8217;s table: Take Production Table for example, dbo.Production will map to ProudctionInfo in PetShop.Model. string connectionString = @&#8221;server=server-running-sqlserver;database=MSPetShop4;integrated security=SSPI;min pool size=4;max pool size=4;&#8221;; \/\/ to get this &hellip; <a href=\"http:\/\/windows.emacslisp.com\/index.php\/2015\/12\/19\/pet-shop-4-0-database-map-to-object\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/46"}],"collection":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":3,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":50,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/posts\/46\/revisions\/50"}],"wp:attachment":[{"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/windows.emacslisp.com\/index.php\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}