登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

Category  

2010-06-03 16:44:21|  分类: C#技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
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 interface ICategory {

        /// <summary>
        /// Method to get all categories
        /// </summary>       
        /// <returns>Interface to Model Collection Generic of categories</returns>
        IList<CategoryInfo> GetCategories();

        /// <summary>
        /// Get information on a specific category
        /// </summary>
        /// <param name="categoryId">Unique identifier for a category</param>
        /// <returns>Business Entity representing an category</returns>
        CategoryInfo GetCategory(string categoryId);
    }

    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);               
        }
    }
}
  评论这张
 
阅读(749)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018