Pet Shop 4.0 – DataBase Map to Object

Pet shop uses very simple to map database columns to objects.

Here are database’s table:
mspetshop-database

Take Production Table for example,

dbo.Production will map to ProudctionInfo in PetShop.Model.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
            string connectionString = @"server=server-running-sqlserver;database=MSPetShop4;integrated security=SSPI;min pool size=4;max pool size=4;";
// to get this connection string , you have to decrypted connection section on web.config
//http://windows.emacslisp.com/index.php/2015/12/11/pet-shop-4-0-encrypt-webconfig-section-and-decrypt-webconfig-section-connectionstring/
 
            string productionString = @"SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product";
            List<ProductInfo> products = new List<ProductInfo>();
            ProductInfo product = null;
 
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);
 
            if (conn.State != ConnectionState.Open)
                conn.Open();
 
            cmd.Connection = conn;
            cmd.CommandText = productionString;
 
 
            cmd.CommandType = CommandType.Text;
 
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            cmd.Parameters.Clear();
 
            while (rdr.Read())
            {
                product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));
                products.Add(product);
            }
 
            foreach (ProductInfo p in products)
            {
                Console.WriteLine(p.Image);
            }
This entry was posted in PetShop4.0. Bookmark the permalink.