导航

思胜PetShop学习笔记之二

Posted on 2012-06-08 11:34  yiyishuitian  阅读(91)  评论(0)    收藏  举报

PetShop

总结:

创建 Model 类库,然后添加Category

namespace PetShop.Model

{

    publicclassCategory

    {

        publicstring CategoryId { get; set; }

        publicstring CategoryName { get; set; }

        publicstring Description { get; set; }

    }

}

创建类库项目ClassLibrary,只有类定义的项目添加接口IdataAccessLayer 项目名称解决方案名称PetShop

namespace PetShop.IDataAccessLayer

{

   publicinterfaceICategory

    {

       //获取所有的分类信息,注意返回类型为可遍历的集合

       public System.Collections.Generic.IEnumerable<PetShop.Model.Category> GetCategories();

       //获取某种分类的详细信息

       public PetShop.Model.Category GetcategoryById(string categoryId);

    }

}

 

新建类库 SQLServerDAL 添加类 Category

 

 

using System.Data.SqlClient;

 

namespace PetShop.SQLServerDAL

{

    //基于SQL的数据访问实现

    publicclassCategory :

        PetShop.IDataAccessLayer.ICategory

    {

        publicIEnumerable<Model.Category> GetCategories()

        {

            string sql = "select CategoryId,Name,Descn from Category";

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["petshop4"].ConnectionString;

 

            //创建保存查询结果的集合

            //注意,list实现了接口IEnumberable<T>

            List<PetShop.Model.Category> list = newList<Model.Category>();

 

            using (SqlConnection connection = newSqlConnection(connectionString))

            {

                SqlCommand command = newSqlCommand(sql, connection);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())

                {

                    //每条记录要变成对象

                    PetShop.Model.Category category =

                        new Model.Category()

                        {

                            CategoryId = reader.GetString(0),

                            CategoryName = reader.GetString(1),

                            Description = reader.GetString(2)

                        };

                    list.Add(category);

 

                }

                reader.Close();

            }

            return list;

        }

 

        public Model.Category GetcategoryById(string categoryId)

        {

            thrownewNotImplementedException();

        }

    }

}

 

新建控制台应用程序ConsoleApplication1

namespace ConsoleApplication1

{

    classProgram

    {

        staticvoid Main(string[] args)

        {

            PetShop.IDataAccessLayer.ICategory categoryDAL

                = new PetShop.SQLServerDAL.Category();

            foreach(PetShop.Model.Category c in categoryDAL.GetCategories() )

            {

                Console.WriteLine(c.CategoryName);

 

            }

        }

    }

}

 

 

五App.config 文件中配置

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <connectionStrings>

    <add name="petshop4" connectionString="server=.;database=MSPetShop4;Integrated Security=True"></add>

  </connectionStrings>

 

 

</configuration>