SQLServerDAL

product:

using System;using System.Data.SqlClient;using PetShop.Model;using PetShop.IDAL;using System.Data;using System.Text;using System.Collections.Generic;using PetShop.DBUtility;
namespace PetShop.SQLServerDAL {
    public class Product : IProduct {
        //Static constants        private const string SQL_SELECT_PRODUCTS_BY_CATEGORY = "SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product WHERE Product.CategoryId = @Category";        private const string SQL_SELECT_PRODUCTS_BY_SEARCH1 = "SELECT ProductId, Name, Descn, Product.Image, Product.CategoryId FROM Product WHERE ((";        private const string SQL_SELECT_PRODUCTS_BY_SEARCH2 = "LOWER(Name) LIKE '%' + {0} + '%' OR LOWER(CategoryId) LIKE '%' + {0} + '%'";        private const string SQL_SELECT_PRODUCTS_BY_SEARCH3 = ") OR (";        private const string SQL_SELECT_PRODUCTS_BY_SEARCH4 = "))";        private const string SQL_SELECT_PRODUCT = "SELECT Product.ProductId, Product.Name, Product.Descn, Product.Image, Product.CategoryId FROM Product WHERE Product.ProductId  = @ProductId";        private const string PARM_CATEGORY = "@Category";        private const string PARM_KEYWORD = "@Keyword";        private const string PARM_PRODUCTID = "@ProductId";
        /// <summary>        /// Query for products by category        /// </summary>        /// <param name="category">category name</param>          /// <returns>A Generic List of ProductInfo</returns>        public IList<ProductInfo> GetProductsByCategory(string category) {
            IList<ProductInfo> productsByCategory = new List<ProductInfo>();
            SqlParameter parm = new SqlParameter(PARM_CATEGORY, SqlDbType.VarChar, 10);            parm.Value = category;
            //Execute a query to read the products            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_PRODUCTS_BY_CATEGORY, parm)) {                while (rdr.Read()) {                    ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));                    productsByCategory.Add(product);                }            }
            return productsByCategory;        }
        /// <summary>        /// Query for products by keywords.         /// The results will include any product where the keyword appears in the category name or product name        /// </summary>        /// <param name="keywords">string array of keywords</param>        /// <returns>A Generic List of ProductInfo</returns>        public IList<ProductInfo> GetProductsBySearch(string[] keywords) {
            IList<ProductInfo> productsBySearch = new List<ProductInfo>();
            int numKeywords = keywords.Length;
            //Create a new query string            StringBuilder sql = new StringBuilder(SQL_SELECT_PRODUCTS_BY_SEARCH1);
            //Add each keyword to the query            for (int i = 0; i < numKeywords; i++) {                sql.Append(string.Format(SQL_SELECT_PRODUCTS_BY_SEARCH2, PARM_KEYWORD + i));                sql.Append(i + 1 < numKeywords ? SQL_SELECT_PRODUCTS_BY_SEARCH3 : SQL_SELECT_PRODUCTS_BY_SEARCH4);            }
            string sqlProductsBySearch = sql.ToString();            SqlParameter[] parms = SqlHelper.GetCachedParameters(sqlProductsBySearch);
            // If the parameters are null build a new set            if (parms == null) {                parms = new SqlParameter[numKeywords];
                for (int i = 0; i < numKeywords; i++)                    parms[i] = new SqlParameter(PARM_KEYWORD + i, SqlDbType.VarChar, 80);
                SqlHelper.CacheParameters(sqlProductsBySearch, parms);            }
            // Bind the new parameters            for (int i = 0; i < numKeywords; i++)                parms[i].Value = keywords[i];
            //Finally execute the query            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlProductsBySearch, parms)) {                while (rdr.Read()) {                    ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));                    productsBySearch.Add(product);                }            }
            return productsBySearch;        }
        /// <summary>        /// Query for a product        /// </summary>        /// <param name="productId">Product Id</param>        /// <returns>ProductInfo object for requested product</returns>        public ProductInfo GetProduct(string productId) {            ProductInfo product = null;            SqlParameter parm = new SqlParameter(PARM_PRODUCTID, SqlDbType.VarChar, 10);            parm.Value = productId;
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_PRODUCT, parm))                if (rdr.Read())                    product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));                else                    product = new ProductInfo();
            return product;        }
        /// <summary>        /// Get the SqlCommand used to retrieve a list of products by category id        /// </summary>        /// <param name="id">Category id</param>        /// <returns>Sql Command object used to retrieve the data</returns>        public static SqlCommand GetCommand(string id) {
            //Create a parameter            SqlParameter parm = new SqlParameter(PARM_CATEGORY, SqlDbType.VarChar, 10);            parm.Value = id;
            // Create and return SqlCommand object            SqlCommand command = new SqlCommand(SQL_SELECT_PRODUCTS_BY_CATEGORY);            command.Parameters.Add(parm);            return command;        }    }}

Other:

using System;using System.Data.SqlClient;using System.Data;using System.Collections.Generic;using System.Text;using PetShop.Model;using PetShop.IDAL;using PetShop.DBUtility;
namespace PetShop.SQLServerDAL {
    public class Order : IOrder {
        //Static constants private const string SQL_INSERT_ORDER = "Declare @ID int; Declare @ERR int; INSERT INTO Orders VALUES(@UserId, @Date, @ShipAddress1, @ShipAddress2, @ShipCity, @ShipState, @ShipZip, @ShipCountry, @BillAddress1, @BillAddress2, @BillCity, @BillState, @BillZip, @BillCountry, 'UPS', @Total, @BillFirstName, @BillLastName, @ShipFirstName, @ShipLastName, @AuthorizationNumber, 'US_en'); SELECT @ID=@@IDENTITY; INSERT INTO OrderStatus VALUES(@ID, @ID, GetDate(), 'P'); SELECT @ERR=@@ERROR;";        private const string SQL_INSERT_ITEM = "INSERT INTO LineItem VALUES( ";        private const string SQL_SELECT_ORDER = "SELECT o.OrderDate, o.UserId, o.CardType, o.CreditCard, o.ExprDate, o.BillToFirstName, o.BillToLastName, o.BillAddr1, o.BillAddr2, o.BillCity, o.BillState, BillZip, o.BillCountry, o.ShipToFirstName, o.ShipToLastName, o.ShipAddr1, o.ShipAddr2, o.ShipCity, o.ShipState, o.ShipZip, o.ShipCountry, o.TotalPrice, l.ItemId, l.LineNum, l.Quantity, l.UnitPrice FROM Orders as o, lineitem as l WHERE o.OrderId = @OrderId AND o.orderid = l.orderid";        private const string PARM_USER_ID = "@UserId";        private const string PARM_DATE = "@Date";        private const string PARM_SHIP_ADDRESS1 = "@ShipAddress1";        private const string PARM_SHIP_ADDRESS2 = "@ShipAddress2";        private const string PARM_SHIP_CITY = "@ShipCity";        private const string PARM_SHIP_STATE = "@ShipState";        private const string PARM_SHIP_ZIP = "@ShipZip";        private const string PARM_SHIP_COUNTRY = "@ShipCountry";        private const string PARM_BILL_ADDRESS1 = "@BillAddress1";        private const string PARM_BILL_ADDRESS2 = "@BillAddress2";        private const string PARM_BILL_CITY = "@BillCity";        private const string PARM_BILL_STATE = "@BillState";        private const string PARM_BILL_ZIP = "@BillZip";        private const string PARM_BILL_COUNTRY = "@BillCountry";        private const string PARM_TOTAL = "@Total";        private const string PARM_BILL_FIRST_NAME = "@BillFirstName";        private const string PARM_BILL_LAST_NAME = "@BillLastName";        private const string PARM_SHIP_FIRST_NAME = "@ShipFirstName";        private const string PARM_SHIP_LAST_NAME = "@ShipLastName"; private const string PARM_AUTHORIZATION_NUMBER = "@AuthorizationNumber";          private const string PARM_ORDER_ID = "@OrderId";        private const string PARM_LINE_NUMBER = "@LineNumber";        private const string PARM_ITEM_ID = "@ItemId";        private const string PARM_QUANTITY = "@Quantity";        private const string PARM_PRICE = "@Price";
        public void Insert(OrderInfo order) {            StringBuilder strSQL = new StringBuilder();
            // Get each commands parameter arrays            SqlParameter[] orderParms = GetOrderParameters();
            SqlCommand cmd = new SqlCommand();
            // Set up the parameters            orderParms[0].Value = order.UserId;            orderParms[1].Value = order.Date;            orderParms[2].Value = order.ShippingAddress.Address1;            orderParms[3].Value = order.ShippingAddress.Address2;            orderParms[4].Value = order.ShippingAddress.City;            orderParms[5].Value = order.ShippingAddress.State;            orderParms[6].Value = order.ShippingAddress.Zip;            orderParms[7].Value = order.ShippingAddress.Country;            orderParms[8].Value = order.BillingAddress.Address1;            orderParms[9].Value = order.BillingAddress.Address2;            orderParms[10].Value = order.BillingAddress.City;            orderParms[11].Value = order.BillingAddress.State;            orderParms[12].Value = order.BillingAddress.Zip;            orderParms[13].Value = order.BillingAddress.Country;            orderParms[14].Value = order.OrderTotal;            orderParms[15].Value = order.BillingAddress.FirstName;            orderParms[16].Value = order.BillingAddress.LastName;            orderParms[17].Value = order.ShippingAddress.FirstName;            orderParms[18].Value = order.ShippingAddress.LastName; orderParms[19].Value = order.AuthorizationNumber.Value;
            foreach (SqlParameter parm in orderParms)                cmd.Parameters.Add(parm);
            // Create the connection to the database            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringOrderDistributedTransaction)) {
                // Insert the order status                strSQL.Append(SQL_INSERT_ORDER);                SqlParameter[] itemParms;                // For each line item, insert an orderline record                int i = 0;                foreach (LineItemInfo item in order.LineItems) {                    strSQL.Append(SQL_INSERT_ITEM).Append(" @ID").Append(", @LineNumber").Append(i).Append(", @ItemId").Append(i).Append(", @Quantity").Append(i).Append(", @Price").Append(i).Append("); SELECT @ERR=@ERR+@@ERROR;");
                    //Get the cached parameters                    itemParms = GetItemParameters(i);
                    itemParms[0].Value = item.Line;                    itemParms[1].Value = item.ItemId;                    itemParms[2].Value = item.Quantity;                    itemParms[3].Value = item.Price;                    //Bind each parameter                    foreach (SqlParameter parm in itemParms)                        cmd.Parameters.Add(parm);                    i++;                }
                conn.Open();                cmd.Connection = conn;                cmd.CommandType = CommandType.Text;                cmd.CommandText = strSQL.Append("SELECT @ID, @ERR").ToString();
                // Read the output of the query, should return error count                using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {                    // Read the returned @ERR                    rdr.Read();                    // If the error count is not zero throw an exception                    if (rdr.GetInt32(1) != 0)                        throw new ApplicationException("DATA INTEGRITY ERROR ON ORDER INSERT - ROLLBACK ISSUED");                }                //Clear the parameters                cmd.Parameters.Clear();            }        }
        /// <summary>        /// Read an order from the database        /// </summary>        /// <param name="orderId">Order Id</param>        /// <returns>All information about the order</returns>        public OrderInfo GetOrder(int orderId) {
            OrderInfo order = new OrderInfo();
            //Create a parameter            SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);            parm.Value = orderId;
            //Execute a query to read the order            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringOrderDistributedTransaction, CommandType.Text, SQL_SELECT_ORDER, parm)) {
                if (rdr.Read()) {
                    //Generate an order header from the first row                    AddressInfo billingAddress = new AddressInfo(rdr.GetString(5), rdr.GetString(6), rdr.GetString(7), rdr.GetString(8), rdr.GetString(9), rdr.GetString(10), rdr.GetString(11), rdr.GetString(12), null, "email");                    AddressInfo shippingAddress = new AddressInfo(rdr.GetString(13), rdr.GetString(14), rdr.GetString(15), rdr.GetString(16), rdr.GetString(17), rdr.GetString(18), rdr.GetString(19), rdr.GetString(20), null, "email");
                    order = new OrderInfo(orderId, rdr.GetDateTime(0), rdr.GetString(1), null, billingAddress, shippingAddress, rdr.GetDecimal(21), null, null);
                    IList<LineItemInfo> lineItems = new List<LineItemInfo>();                    LineItemInfo item = null;
                    //Create the lineitems from the first row and subsequent rows                    do {                        item = new LineItemInfo(rdr.GetString(22), string.Empty, rdr.GetInt32(23), rdr.GetInt32(24), rdr.GetDecimal(25));                        lineItems.Add(item);                    } while (rdr.Read());
                    order.LineItems = new LineItemInfo[lineItems.Count];                    lineItems.CopyTo(order.LineItems, 0);                }            }
            return order;        }
        /// <summary>        /// Internal function to get cached parameters        /// </summary>        /// <returns></returns>        private static SqlParameter[] GetOrderParameters() {            SqlParameter[] parms = SqlHelper.GetCachedParameters(SQL_INSERT_ORDER);
            if (parms == null) { parms = new SqlParameter[] { new SqlParameter(PARM_USER_ID, SqlDbType.VarChar, 80), new SqlParameter(PARM_DATE, SqlDbType.DateTime, 12), new SqlParameter(PARM_SHIP_ADDRESS1, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_ADDRESS2, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_CITY, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_STATE, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_ZIP, SqlDbType.VarChar, 50), new SqlParameter(PARM_SHIP_COUNTRY, SqlDbType.VarChar, 50), new SqlParameter(PARM_BILL_ADDRESS1, SqlDbType.VarChar, 80), new SqlParameter(PARM_BILL_ADDRESS2, SqlDbType.VarChar, 80), new SqlParameter(PARM_BILL_CITY, SqlDbType.VarChar, 80), new SqlParameter(PARM_BILL_STATE, SqlDbType.VarChar, 80), new SqlParameter(PARM_BILL_ZIP, SqlDbType.VarChar, 50), new SqlParameter(PARM_BILL_COUNTRY, SqlDbType.VarChar, 50), new SqlParameter(PARM_TOTAL, SqlDbType.Decimal, 8), new SqlParameter(PARM_BILL_FIRST_NAME, SqlDbType.VarChar, 80), new SqlParameter(PARM_BILL_LAST_NAME, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_FIRST_NAME, SqlDbType.VarChar, 80), new SqlParameter(PARM_SHIP_LAST_NAME, SqlDbType.VarChar, 80), new SqlParameter(PARM_AUTHORIZATION_NUMBER, SqlDbType.Int)};
                SqlHelper.CacheParameters(SQL_INSERT_ORDER, parms);            }
            return parms;        }
        private static SqlParameter[] GetItemParameters(int i) {            SqlParameter[] parms = SqlHelper.GetCachedParameters(SQL_INSERT_ITEM + i);
            if (parms == null) {                parms = new SqlParameter[] { new SqlParameter(PARM_LINE_NUMBER + i, SqlDbType.Int, 4), new SqlParameter(PARM_ITEM_ID+i, SqlDbType.VarChar, 10), new SqlParameter(PARM_QUANTITY+i, SqlDbType.Int, 4), new SqlParameter(PARM_PRICE+i, SqlDbType.Decimal, 8)};
                SqlHelper.CacheParameters(SQL_INSERT_ITEM + i, parms);            }
            return parms;        }    }}

Item:

using System;using System.Data;using System.Data.SqlClient;using PetShop.Model;using PetShop.IDAL;using System.Collections.Generic;using PetShop.DBUtility;
namespace PetShop.SQLServerDAL {
    public class Item : IItem {
        // Static constants        private const string SQL_SELECT_ITEMS_BY_PRODUCT = "SELECT Item.ItemId, Item.Name, Inventory.Qty, Item.ListPrice, Product.Name, Item.Image, Product.CategoryId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId INNER JOIN Inventory ON Item.ItemId = Inventory.ItemId WHERE Item.ProductId = @ProductId";                private const string SQL_SELECT_ITEM = "SELECT Item.ItemId, Item.Name, Item.ListPrice, Product.Name, Item.Image, Product.CategoryId, Product.ProductId FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId WHERE Item.ItemId = @ItemId";
        private const string PARM_PRODUCT_ID = "@ProductId";        private const string PARM_ITEM_ID = "@ItemId";
        /// <summary>        /// Function to get a list of items within a product group        /// </summary> /// <param name="productId">Product Id</param>            /// <returns>A Generic List of ItemInfo</returns> public IList<ItemInfo> GetItemsByProduct(string productId) {
            IList<ItemInfo> itemsByProduct = new List<ItemInfo>();
            SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);            parm.Value = productId;
            //Execute the query against the database using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm)) {                // Scroll through the results                while (rdr.Read()) {                    ItemInfo item = new ItemInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));                    //Add each item to the arraylist                    itemsByProduct.Add(item);                }            }            return itemsByProduct;        }

        /// <summary>        /// Get an individual item based on a unique key        /// </summary>        /// <param name="itemId">unique key</param>        /// <returns>Details about the Item</returns>        public ItemInfo GetItem(string itemId) {
            //Set up a return value            ItemInfo item = null;
            //Create a parameter            SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.VarChar, 10);            //Bind the parameter            parm.Value = itemId;
            //Execute the query             using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEM, parm)) {                if (rdr.Read())                    item = new ItemInfo(rdr.GetString(0), rdr.GetString(1), 0, rdr.GetDecimal(2), rdr.GetString(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6));                else                    item = new ItemInfo();            }            return item;        }
        /// <summary>        /// Get the SqlCommand used to retrieve a list of items by product        /// </summary>        /// <param name="id">Product id</param>        /// <returns>Sql Command object used to retrieve the data</returns>        public static SqlCommand GetCommand(string id) {
            //Create a parameter            SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);            parm.Value = id;
            // Create and return SqlCommand object            SqlCommand command = new SqlCommand(SQL_SELECT_ITEMS_BY_PRODUCT);            command.Parameters.Add(parm);            return command;        }    }}

Inventory:

using System;using System.Data;using System.Data.SqlClient;using System.Text;using PetShop.Model;using PetShop.IDAL;using PetShop.DBUtility;
namespace PetShop.SQLServerDAL {
    public class Inventory : IInventory {
        // Static constants        private const string SQL_SELECT_INVENTORY = "SELECT Qty FROM Inventory WHERE ItemId = @ItemId";        private const string SQL_TAKE_INVENTORY = "UPDATE Inventory SET Qty = Qty - ";
        /// <summary>        /// Function to get the current quantity in stock        /// </summary>        /// <param name="ItemId">Unique identifier for an item</param>        /// <returns>Current Qty in Stock</returns>        public int CurrentQtyInStock(string itemId) {
            int qty = 0;            SqlParameter parm = new SqlParameter("@ItemId", SqlDbType.VarChar, 10);            parm.Value = itemId;
            qty = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_INVENTORY, parm));
            return qty;        }
        /// <summary>        /// Function to update inventory based on purchased items        /// Internally the function uses a batch query so the command is only sent to the database once        /// </summary>        /// <param name="items">Array of items purchased</param>        public void TakeStock(LineItemInfo[] items) {
            SqlParameter[] inventoryParms;            SqlCommand cmd = new SqlCommand();
            //Open a connection            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringInventoryDistributedTransaction)) {
                StringBuilder strSQL = new StringBuilder();                int i = 0;
                //Append a statement to the batch for each item in the array                foreach (LineItemInfo item in items) {
                    strSQL.Append(SQL_TAKE_INVENTORY);
                    inventoryParms = GetInventoryParameters(i);
                    strSQL.Append("@Quantity").Append(i).Append(" WHERE ItemId = @ItemId").Append(i).Append(";");
                    //Bind parameters                    inventoryParms[0].Value = item.Quantity;                    inventoryParms[1].Value = item.ItemId;
                    foreach (SqlParameter parm in inventoryParms)                        cmd.Parameters.Add(parm);                    i++;                }
                // Open the connection                conn.Open();
                //Set up the command                cmd.Connection = conn;                cmd.CommandType = CommandType.Text;                cmd.CommandText = strSQL.ToString();
                //Execute the query                cmd.ExecuteNonQuery();                cmd.Parameters.Clear();
            }        }
        /// <summary>        /// Internal function to get cached parameters        /// </summary>        /// <param name="i"></param>        /// <returns></returns>        private static SqlParameter[] GetInventoryParameters(int i) {            SqlParameter[] parms = SqlHelper.GetCachedParameters(SQL_TAKE_INVENTORY + i);
            if (parms == null) {                parms = new SqlParameter[] { new SqlParameter("@Quantity" + i, SqlDbType.Int), new SqlParameter("@ItemId"+i, SqlDbType.VarChar, 10)};
                SqlHelper.CacheParameters(SQL_TAKE_INVENTORY + i, parms);            }
            return parms;        }    }}

Category:

using System;using System.Data;using System.Data.SqlClient;using PetShop.Model;using PetShop.IDAL;using System.Collections.Generic;using PetShop.DBUtility;
namespace PetShop.SQLServerDAL {
    public class Category : ICategory {
        // Static constants        private const string SQL_SELECT_CATEGORIES = "SELECT CategoryId, Name, Descn FROM Category";        private const string SQL_SELECT_CATEGORY = "SELECT CategoryId, Name, Descn FROM Category WHERE CategoryId = @CategoryId";        private const string PARM_CATEGORY_ID = "@CategoryId";

        /// <summary>        /// Method to get all categories /// </summary>              public IList<CategoryInfo> GetCategories() {
IList<CategoryInfo> categories = new List<CategoryInfo>();
            //Execute a query to read the categories using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORIES, null)) {                while (rdr.Read()) {                    CategoryInfo cat = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));                    categories.Add(cat);                }            }              return categories;        }
        /// <summary>        /// Get an individual category based on a provided id        /// </summary>        /// <param name="categoryId">Category id</param>        /// <returns>Details about the Category</returns>        public CategoryInfo GetCategory(string categoryId) {
            //Set up a return value            CategoryInfo category = null;
            //Create a parameter            SqlParameter parm = new SqlParameter(PARM_CATEGORY_ID, SqlDbType.VarChar, 10);            //Bind the parameter            parm.Value = categoryId;
            //Execute the query             using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_CATEGORY, parm)) {                if (rdr.Read())
                    category = new CategoryInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetString(2));                else                    category = new CategoryInfo();            }            return category;        }
        /// <summary>        /// Get the SqlCommand used to retrieve a list of categories        /// </summary>        /// <param name="id">Category id</param>        /// <returns>Sql Command object used to retrieve the data</returns>        public static SqlCommand GetCommand() {            return new SqlCommand(SQL_SELECT_CATEGORIES);                       }    }}

posted on 2011-04-03 21:51  xingya  阅读(266)  评论(0)    收藏  举报