|
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace DemoDataLayer
{
/// <summary>
/// Represenets product that has ID, Name and Price
/// </summary>
public class Product
{
#region Members
private int id;
public int ID { get { return id; } }
private string name;
public string Name { get { return name; } set { name = value; } }
private double price;
public double Price { get { return price; } set { price = value; } }
public Product(int id, string name, double price)
{
this.id = id; this.name = name; this.price = price;
}
#endregion
}
/// <summary>
/// Represents category with ID, Name and web page theme
/// </summary>
public class Category
{
#region Members
private string theme;
public string Theme { get { return theme; } set { theme = value; } }
private int id;
public int ID { get { return id;} }
private string name;
public string Name { get { return name;} set { name = value;} }
public Category(int id, string name, string theme)
{
this.id = id; this.name = name; this.theme = theme;
}
#endregion
}
/// <summary>
/// Access to data (data are stored in memory)
/// </summary>
public class Data
{
#region Configuration
const string connStr = "Server=.;Database=Northwind;Integrated Security=Yes;";
#endregion
#region Members
public List<Category> GetCategories()
{
List<Category> ret = new List<Category>();
using(SqlConnection cn = new SqlConnection(connStr))
{
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT [CategoryID], [CategoryName] FROM [Categories]", cn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ret.Add(new Category((int)reader["CategoryID"], (string)reader["CategoryName"],
GetTheme((int)reader["CategoryID"])));
}
}
}
return ret;
}
public Category GetCategory(int id)
{
using (SqlConnection cn = new SqlConnection(connStr))
{
cn.Open();
SqlCommand cmd = new SqlCommand(string.Format("SELECT TOP 1 [CategoryID], [CategoryName] FROM "+
"[Categories] WHERE [CategoryID]={0}", id), cn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
return new Category((int)reader["CategoryID"], (string)reader["CategoryName"], GetTheme((int)reader["CategoryID"]));
}
}
return null;
}
public List<Product> GetProducts(int category)
{
List<Product> ret = new List<Product>();
using (SqlConnection cn = new SqlConnection(connStr))
{
cn.Open();
SqlCommand cmd = new SqlCommand(string.Format("SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] "+
"WHERE [CategoryID]={0}", category), cn); // this is ok for integers
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ret.Add(new Product((int)reader["ProductID"], (string)reader["ProductName"], (double)(decimal)reader["UnitPrice"]));
}
}
}
return ret;
}
public Product GetProduct(int id)
{
List<Product> ret = new List<Product>();
using (SqlConnection cn = new SqlConnection(connStr))
{
cn.Open();
SqlCommand cmd = new SqlCommand(string.Format("SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] " +
"WHERE [ProductID]={0}", id), cn); // this is ok for integers
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
return new Product((int)reader["ProductID"], (string)reader["ProductName"], (double)(decimal)reader["UnitPrice"]);
}
}
return null;
}
private string GetTheme(int p)
{
switch (p)
{
case 1:
case 5:
case 6: return "green"; // food&drinks
default: return "default"; // other
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I live in Prague, the capital city of Czech republic (most of the time
). I've been very interested in functional programming recently and I have a passion for the new Microsoft F# language. I'm writing a book about
Functional Programming in the Real World that shows the ideas using examples in C# 3.0 and F#.
I've been Microsoft MVP (for C#) since 2004 and I'm one of the most active members of the F# community. I'm a computer science student at
Charles University of Prague. My hobbies include photography, fractals and of course many things related to computers (except fixing them). My favorite book writers are Terry Pratchett and Philip K Dick and I like paintings by M. C. Escher.
PS: My favorite codeproject icon is .