录播教室预约系统(四)-ClassRoom表[带有外键的表]

ClassRoom是与教室信息相关的表,包含教室的名称

 

这个表比前面介绍的DepTable表稍微复杂点,他有一列DepID 这列是与DepTable表中的ID相对应的的。

有了DepID这一列,我们就可以根据DepID 获取某单位的所有教室。

我们暂时把DepID这一列叫做外键列吧。对于拥有外键列的表,仍然可以用我们的codesmith模板生成相关的存储过程和类。有的时候我们希望能够以外键为参数查找相关的数据,

所以在对应的codesmith模板中增加了以外键为参数查找和以外键为参数自动分页查找的相关的代码。分页查找的存储过程和方法也都是自动生成的。

下面我们来举例说明:

 第一步 生成存储过程,如下图,外键列名是必须填写的:

生成的存储过程如下:

 
/****** Object:  Stored Procedure [dbo].ClassRoom_Delete    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_Delete]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_SelectOne    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_SelectOne]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_SelectOne]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_SelectTopList    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_SelectTopList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_SelectTopList]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_GetCount    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_GetCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_GetCount]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_GetListCount    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_GetListCount]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_GetListCount]
GO

    
/****** Object:  Stored Procedure [dbo].ClassRoom_SelectAll    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_SelectAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_SelectAll]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_Insert    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_Insert]
GO

/****** Object:  Stored Procedure [dbo].ClassRoom_Update    Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_Update]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_Update]
GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO


CREATE PROCEDURE [dbo].ClassRoom_Delete

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

@Id int

AS

DELETE FROM [dbo].[ClassRoom]
WHERE
    [Id] = @Id
GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO



SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO



CREATE PROCEDURE [dbo].ClassRoom_GetCount

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

AS

SELECT COUNT(*) FROM [dbo].[ClassRoom]

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO





SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO

CREATE PROCEDURE [dbo].ClassRoom_GetListCount

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

@DepID  int

AS

SELECT COUNT(*) FROM [dbo].[ClassRoom] Where DepID=@DepID

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO



SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE [dbo].ClassRoom_SelectOne

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

@Id int

AS


SELECT
        [Id],
        [RoomName],
        [DepID],
        [Department]
        
FROM
        [dbo].[ClassRoom]
        
WHERE
        [Id] = @Id

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO






SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE [dbo].ClassRoom_SelectTopList 

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

@DepID  int

AS


SELECT  top 10
        [Id],
        [RoomName],
        [DepID],
        [Department]
        
FROM
        [dbo].[ClassRoom]
        
WHERE
         DepID=@DepID

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO







SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO


CREATE PROCEDURE [dbo].ClassRoom_SelectAll

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

AS


SELECT
        [Id],
        [RoomName],
        [DepID],
        [Department]
        
FROM
        [dbo].[ClassRoom]

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO


SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO



CREATE PROCEDURE [dbo].ClassRoom_Insert

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/

@RoomName nvarchar(200),
@DepID int,
@Department nvarchar(200)

    
AS

INSERT INTO     [dbo].[ClassRoom] 
(
                [RoomName],
                [DepID],
                [Department]
) 

VALUES 
(
                @RoomName,
                @DepID,
                @Department
                
)
SELECT @@IDENTITY 


GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO


SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER ON 
GO



CREATE PROCEDURE [dbo].ClassRoom_Update

/*
Author:               msdc
Created:             2015-2-8
Last Modified:         2015-2-8
*/
    
@Id int, 
@RoomName nvarchar(200), 
@DepID int, 
@Department nvarchar(200) 


AS

UPDATE         [dbo].[ClassRoom] 

SET
            [RoomName] = @RoomName,
            [DepID] = @DepID,
            [Department] = @Department
            
WHERE
            [Id] = @Id

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO


/****** Object:  Stored Procedure [dbo].ClassRoom_SelectPage   Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_SelectPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_SelectPage]
GO



CREATE PROCEDURE [dbo].ClassRoom_SelectPage

-- Author:               msdc
-- Created:             2015-2-8
-- Last Modified:         2015-2-8

@PageNumber             int,
@PageSize             int

AS

DECLARE @PageLowerBound int
DECLARE @PageUpperBound int


SET @PageLowerBound = (@PageSize * @PageNumber) - @PageSize
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

/*
Note: temp tables use the server default for collation not the database default
so if adding character columns be sure and specify to use the database collation like this
to avoid collation errors:

CREATE TABLE #PageIndexForUsers
(
IndexID int IDENTITY (1, 1) NOT NULL,
UserName nvarchar(50) COLLATE DATABASE_DEFAULT,
LoginName nvarchar(50) COLLATE DATABASE_DEFAULT
) 


*/

CREATE TABLE #PageIndex 
(
    IndexID int IDENTITY (1, 1) NOT NULL,
Id Int
)

BEGIN

INSERT INTO #PageIndex ( 
Id
)

SELECT
        [Id]
        
FROM
        [dbo].[ClassRoom]
        
-- WHERE

-- ORDER BY

END


SELECT
        t1.*
        
FROM
        [dbo].[ClassRoom] t1

JOIN            #PageIndex t2
ON            
        t1.[Id] = t2.[Id]
        
WHERE
        t2.IndexID > @PageLowerBound 
        AND t2.IndexID < @PageUpperBound
        
ORDER BY t2.IndexID

DROP TABLE #PageIndex

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO



/****** Object:  Stored Procedure [dbo].ClassRoom_SelectPage   Script Date: 2015年2月8日 ******/
if exists (select * from [dbo].sysobjects where id = object_id(N'[dbo].[ClassRoom_SelectListPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ClassRoom_SelectListPage]
GO



CREATE PROCEDURE [dbo].ClassRoom_SelectListPage

-- Author:               msdc
-- Created:             2015-2-8
-- Last Modified:         2015-2-8

@PageNumber             int,
@PageSize             int,
@DepID  int

AS

DECLARE @PageLowerBound int
DECLARE @PageUpperBound int


SET @PageLowerBound = (@PageSize * @PageNumber) - @PageSize
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

/*
Note: temp tables use the server default for collation not the database default
so if adding character columns be sure and specify to use the database collation like this
to avoid collation errors:

CREATE TABLE #PageIndexForUsers
(
IndexID int IDENTITY (1, 1) NOT NULL,
UserName nvarchar(50) COLLATE DATABASE_DEFAULT,
LoginName nvarchar(50) COLLATE DATABASE_DEFAULT
) 


*/

CREATE TABLE #PageIndex 
(
    IndexID int IDENTITY (1, 1) NOT NULL,
Id Int
)

BEGIN

INSERT INTO #PageIndex ( 
Id
)

SELECT
        [Id]
        
FROM
        [dbo].[ClassRoom]  Where DepID=@DepID  order by  
Id
  DESC
        
-- WHERE

-- ORDER BY

END


SELECT
        t1.*
        
FROM
        [dbo].[ClassRoom] t1

JOIN            #PageIndex t2
ON            
        t1.[Id] = t2.[Id]
        
WHERE
        t2.IndexID > @PageLowerBound 
        AND t2.IndexID < @PageUpperBound
        
ORDER BY t2.IndexID

DROP TABLE #PageIndex

GO

SET ANSI_NULLS OFF 
GO

SET QUOTED_IDENTIFIER OFF 
GO
CodeSmith模板生成的存储过程

生成的存储过程如下:

ClassRoom_Delete
ClassRoom_GetCount
ClassRoom_SelectOne
ClassRoom_SelectAll
ClassRoom_Insert
ClassRoom_Update
ClassRoom_SelectPage

比普通表多了3个,如下

ClassRoom_SelectTopList  根据外键获取前10条
ClassRoom_GetListCount  根据外键获取总数
ClassRoom_SelectListPage 根据外键获取分页数据

 

第二步:用CodeSmith模板生成数据层代码:

 

// Author:                    msdc
// Created:                    2015-2-8
// Last Modified:            2015-2-8


using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using mojoPortal.Data;
    
namespace mojoPortal.Data
{
    
    public static class DBClassRoomConfig
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }


        /// <summary>
        /// Inserts a row in the ClassRoomConfig table. Returns new integer id.
        /// </summary>
        /// <param name="daySpan"> daySpan </param>
        /// <param name="stopTime"> stopTime </param>
        /// <param name="roomID"> roomID </param>
        /// <param name="roomName"> roomName </param>
        /// <param name="modifyDate"> modifyDate </param>
        /// <returns>int</returns>
        public static int Create(
            int daySpan, 
            int stopTime, 
            int roomID, 
            string roomName, 
            DateTime modifyDate) 
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoomConfig_Insert", 5);
            sph.DefineSqlParameter("@DaySpan", SqlDbType.Int, ParameterDirection.Input, daySpan);
            sph.DefineSqlParameter("@StopTime", SqlDbType.Int, ParameterDirection.Input, stopTime);
            sph.DefineSqlParameter("@RoomID", SqlDbType.Int, ParameterDirection.Input, roomID);
            sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName);
            sph.DefineSqlParameter("@ModifyDate", SqlDbType.DateTime, ParameterDirection.Input, modifyDate);
            int newID = Convert.ToInt32(sph.ExecuteScalar());
            return newID;
        }


        /// <summary>
        /// Updates a row in the ClassRoomConfig table. Returns true if row updated.
        /// </summary>
        /// <param name="id"> id </param>
        /// <param name="daySpan"> daySpan </param>
        /// <param name="stopTime"> stopTime </param>
        /// <param name="roomID"> roomID </param>
        /// <param name="roomName"> roomName </param>
        /// <param name="modifyDate"> modifyDate </param>
        /// <returns>bool</returns>
        public static bool Update(
            int  id, 
            int daySpan, 
            int stopTime, 
            int roomID, 
            string roomName, 
            DateTime modifyDate) 
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoomConfig_Update", 6);
            sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
            sph.DefineSqlParameter("@DaySpan", SqlDbType.Int, ParameterDirection.Input, daySpan);
            sph.DefineSqlParameter("@StopTime", SqlDbType.Int, ParameterDirection.Input, stopTime);
            sph.DefineSqlParameter("@RoomID", SqlDbType.Int, ParameterDirection.Input, roomID);
            sph.DefineSqlParameter("@RoomName", SqlDbType.NVarChar, 200, ParameterDirection.Input, roomName);
            sph.DefineSqlParameter("@ModifyDate", SqlDbType.DateTime, ParameterDirection.Input, modifyDate);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
            
        }
        
        /// <summary>
        /// Deletes a row from the ClassRoomConfig table. Returns true if row deleted.
        /// </summary>
        /// <param name="id"> id </param>
        /// <returns>bool</returns>
        public static bool Delete(
            int id) 
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "ClassRoomConfig_Delete", 1);
            sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
            
        }
        
         
        /// <summary>
        /// Gets an IDataReader with one row from the ClassRoomConfig table.
        /// </summary>
        /// <param name="id"> id </param>
        public static IDataReader GetOne(
            int  id)  
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectOne", 1);
            sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
            return sph.ExecuteReader();
            
        }
        
        
        
            /// <summary>
        /// Gets an IDataReader with some list row from the ClassRoomConfig table.
        /// </summary>
        /// <param name="id"> id </param>
        public static IDataReader GetTopList(int foreignKey)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectTopList", 1);
              sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, foreignKey);
            return sph.ExecuteReader();
            
        }
        
        
        
        /// <summary>
        /// Gets a count of rows in the ClassRoomConfig table.
        /// </summary>
        public static int GetCount()
        {
            
            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "ClassRoomConfig_GetCount",
                null));
    
        }
        
        
        
        /// <summary>
        /// Gets a Listcount of rows in the ClassRoomConfig table.
        /// </summary>
        public static int GetListCount(int foreignKey)
        {
            SqlParameter  theSqlParameter=new SqlParameter("@DepID",foreignKey);
            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "ClassRoomConfig_GetListCount",
                theSqlParameter));
    
        }
            
        
        
        /// <summary>
        /// Gets an IDataReader with all rows in the ClassRoomConfig table.
        /// </summary>
        public static IDataReader GetAll()
        {
            
            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "ClassRoomConfig_SelectAll",
                null);
    
        }
        
        /// <summary>
        /// Gets a page of data from the ClassRoomConfig table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber, 
            int pageSize,
            out int itemCount)
        {
            itemCount = GetCount();
                        
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();
        
        }
        
        
        public static IDataReader GetListPage(
            int pageNumber, 
            int pageSize,
            int foreignKey,
            out int itemCount)
        {
            itemCount = GetListCount(pid);
                        
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "ClassRoomConfig_SelectListPage", 3);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            sph.DefineSqlParameter("@DepID", SqlDbType.Int, ParameterDirection.Input, foreignKey);
            return sph.ExecuteReader();
        
        }
    
    }

}
CodeSmith模板生成的数据层数据

生成的方法如下:

Create
Update
Delete
GetOne
GetCount
GetAll
GetPage

比普通数据表多了3个方法

GetTopList 根据外键获取前10数据
GetListCount  根据外键获取数量
GetListPage   根据外键获取分页数据

第三步:根据外键获取实体类(客户端与服务器通讯时需使用,序列化方法使用protobuf.net)

生成的代码如下:

// Author:                    msdc
// Created:                    2015-2-8
// Last Modified:            2015-2-8

    
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using mojoportal.Data;
    
namespace mojoportal.Business
{
     [ProtoContract]
    public class ClassRoom
    {

#region Constructors

        public ClassRoom()
        {}
         

#endregion

#region Private Properties

        private int id = -1;
        private string roomName = string.Empty;
        private int depID = -1;
        private string department = string.Empty;
        
#endregion

#region Public Properties

        [ProtoMember(1)]
        public int Id 
        {
            get { return id; }
            set { id = value; }
        }
        [ProtoMember(2)]
        public string RoomName 
        {
            get { return roomName; }
            set { roomName = value; }
        }
        [ProtoMember(3)]
        public int DepID 
        {
            get { return depID; }
            set { depID = value; }
        }
        [ProtoMember(4)]
        public string Department 
        {
            get { return department; }
            set { department = value; }
        }

#endregion

 


    }
    
}
CodeSmith生成的实体类代码

这个和普通表大致相同

 

第四步:生成操作类:

生成代码如下:

// Author:                    msdc
// Created:                    2015-2-8
// Last Modified:            2015-2-8

    
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using mojoportal.Data;
    
namespace mojoportal.Business
{
    
    public class DoClassRoom
    {
 
     #region Private Methods

        /// <summary>
        /// Gets an instance of ClassRoom.
        /// </summary>
        /// <param name="id"> id </param>
        private static ClassRoom GetClassRoom(
            int id) 
        {
            using(IDataReader reader = DBClassRoom.GetOne(
                id)) 
            {
            return PopulateFromReader(reader);
            }
            
        }
        
        
        private static ClassRoom PopulateFromReader(IDataReader reader) 
        {
            ClassRoom classRoom = new ClassRoom();
            if(reader.Read())
            {
                classRoom.Id = Convert.ToInt32(reader["Id"]);
                classRoom.RoomName = reader["RoomName"].ToString();
                classRoom.DepID = Convert.ToInt32(reader["DepID"]);
                classRoom.Department = reader["Department"].ToString();
            
            }
            return classRoom;
         }
        
        /// <summary>
        /// Persists a new instance of ClassRoom. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private static bool Create(ClassRoom classRoom)
        { 
            int newID = 0;
            
            newID = DBClassRoom.Create(
                classRoom.RoomName, 
                classRoom.DepID, 
                classRoom.Department); 
                
            classRoom.Id = newID;
                    
            return (newID > 0);

        }

        
        /// <summary>
        /// Updates this instance of ClassRoom. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private static bool Update(ClassRoom classRoom)
        {

            return DBClassRoom.Update(
                classRoom.Id, 
                classRoom.RoomName, 
                classRoom.DepID, 
                classRoom.Department); 
                
        }
        
        
        


#endregion

#region Public Methods

        /// <summary>
        /// Saves this instance of ClassRoom. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public static bool Save(ClassRoom classRoom)
        {
            if( classRoom.Id > 0)
            {
                return Update(classRoom);
            }
            else
            {
                return Create(classRoom);
            }
        }
        
        
    
        
#endregion

#region Static Methods

        /// <summary>
        /// Deletes an instance of ClassRoom. Returns true on success.
        /// </summary>
        /// <param name="id"> id </param>
        /// <returns>bool</returns>
        public static bool Delete(
            int  id) 
        {
            return DBClassRoom.Delete(
                id); 
        }
        
        
        /// <summary>
        /// Gets a count of ClassRoom. 
        /// </summary>
        public static int GetCount()
        {
            return DBClassRoom.GetCount();
        }
    
        private static IList<ClassRoom> LoadListFromReader(IDataReader reader)
        {
            IList<ClassRoom> classRoomList = new List<ClassRoom>();
            try
            {
                while (reader.Read())
                {
                    ClassRoom classRoom = new ClassRoom();
                    classRoom.Id = Convert.ToInt32(reader["Id"]);
                    classRoom.RoomName = reader["RoomName"].ToString();
                    classRoom.DepID = Convert.ToInt32(reader["DepID"]);
                    classRoom.Department = reader["Department"].ToString();
                    classRoomList.Add(classRoom);
                    
                }
            }
            finally
            {
                reader.Close();
            }
    
            return classRoomList;
        
        }
        
        
        /// <summary>
        /// Gets an IList with some instances of ClassRoom.
        /// </summary>
        public static IList<ClassRoom> GetTopList(int foreignKey)    
        {
            IDataReader reader = DBClassRoom.GetTopList(foreignKey);
             
            return LoadListFromReader(reader);
    
        }
        
    
        /// <summary>
        /// Gets an IList with all instances of ClassRoom.
        /// </summary>
        public static IList<ClassRoom> GetAll()
        {
            IDataReader reader = DBClassRoom.GetAll();
            return LoadListFromReader(reader);
    
        }
    
        /// <summary>
        /// Gets an IList with page of instances of ClassRoom.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IList<ClassRoom> GetPage(int pageNumber, int pageSize, out int itemCount)
        {
            itemCount = 1;
            IDataReader reader = DBClassRoom.GetPage(pageNumber, pageSize, out itemCount);    
            return LoadListFromReader(reader);
        }
        
        
            /// <summary>
        /// Gets an IList with page of instances of ClassRoom.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="itemCount">total items</param>
        public static IList<ClassRoom> GetListPage(int pageNumber, int pageSize,int foreignKey, out int itemCount)
        {
            itemCount = 1;
            IDataReader reader = DBClassRoom.GetListPage(pageNumber, pageSize,foreignKey, out itemCount);    
            return LoadListFromReader(reader);
        }
    

    
#endregion

 


    }
    
}
CodeSmith生成的操作类代码

Delete
GetCount
GetAll
GetPage

Create

Update

比普通表多2个方法

GetTopList 根据外键获取前10数据

GetListPage 根据外键获取分页数据

好的,带有外键的数据表的几层数据我们已经生成完成了。

注意使用CodeSmith生成数据的时候,数据表必须设置有主键,否则生成不成功。

其他表类似,每个表都需要建立相应的存储过程和类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值