录播教室预约系统(三)-DepTable表

DepTable表  主要作用 存放单位名称

如图:

模板下载地址 CodeSmith版本为v6.5

第一步:用CodeSmith模板生成DepTable表相关的存储过程

生成的存储过程如下:

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

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

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

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

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

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

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].DepTable_Delete

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

@Id int

AS

DELETE FROM [dbo].[DepTable]
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].DepTable_GetCount

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

AS

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

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE [dbo].DepTable_SelectOne

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

@Id int

AS

SELECT
        [Id],
        [Department]

FROM
        [dbo].[DepTable]

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].DepTable_SelectAll

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

AS

SELECT
        [Id],
        [Department]

FROM
        [dbo].[DepTable]

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].DepTable_Insert

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

@Department nvarchar(200)

AS

INSERT INTO     [dbo].[DepTable]
(
                [Department]
) 

VALUES
(
                @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].DepTable_Update

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

@Id int,
@Department nvarchar(200) 

AS

UPDATE         [dbo].[DepTable] 

SET
            [Department] = @Department

WHERE
            [Id] = @Id

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

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

CREATE PROCEDURE [dbo].DepTable_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].[DepTable]

-- WHERE

-- ORDER BY

END

SELECT
        t1.*

FROM
        [dbo].[DepTable] 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

模板生成的DepTable表相关的基础存储过程

DepTable_Insert  插入数据
DepTable_Update  更新数据

DepTable_SelectPage  获取分页数据

DepTable_SelectAll   获取所有数据

DepTable_SelectOne  获取某个数据

DepTable_GetCount 获取数量

DepTable_Delete 删除某个数据

第二步:用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 DBDepTable
    {
        /// <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 DepTable table. Returns new integer id.
        /// </summary>
        /// <param name="department"> department </param>
        /// <returns>int</returns>
        public static int Create(
            string department)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "DepTable_Insert", 1);
            sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 200, ParameterDirection.Input, department);
            int newID = Convert.ToInt32(sph.ExecuteScalar());
            return newID;
        }

        /// <summary>
        /// Updates a row in the DepTable table. Returns true if row updated.
        /// </summary>
        /// <param name="id"> id </param>
        /// <param name="department"> department </param>
        /// <returns>bool</returns>
        public static bool Update(
            int  id,
            string department)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "DepTable_Update", 2);
            sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
            sph.DefineSqlParameter("@Department", SqlDbType.NVarChar, 200, ParameterDirection.Input, department);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the DepTable 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(), "DepTable_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 DepTable table.
        /// </summary>
        /// <param name="id"> id </param>
        public static IDataReader GetOne(
            int  id)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "DepTable_SelectOne", 1);
            sph.DefineSqlParameter("@Id", SqlDbType.Int, ParameterDirection.Input, id);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the DepTable table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "DepTable_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the DepTable table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "DepTable_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the DepTable 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(), "DepTable_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }

    }

}

模板生成的数据层代码

主要方法如下:

GetPage  获取分页数据
GetAll  获取所有数据
GetCount 获取总数
GetOne 获取某个数据
Delete  删除
Update 更新

第三步:生成网络通讯程序中使用的实体类(使用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 DepTable
    {

#region Constructors

        public DepTable()
        {}

#endregion

#region Private Properties

        private int id = -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 Department
        {
            get { return department; }
            set { department = value; }
        }

#endregion

    }

}

模板生成的实体类 用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
{

    public class DoDepTable
    {

     #region Private Methods

        /// <summary>
        /// Gets an instance of DepTable.
        /// </summary>
        /// <param name="id"> id </param>
        private static DepTable GetDepTable(
            int id)
        {
            using(IDataReader reader = DBDepTable.GetOne(
                id))
            {
            return PopulateFromReader(reader);
            }

        }

        private static DepTable PopulateFromReader(IDataReader reader)
        {
            DepTable depTable = new DepTable();
            if(reader.Read())
            {
                depTable.Id = Convert.ToInt32(reader["Id"]);
                depTable.Department = reader["Department"].ToString();

            }
            return depTable;
         }

        /// <summary>
        /// Persists a new instance of DepTable. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private static bool Create(DepTable depTable)
        {
            int newID = 0;

            newID = DBDepTable.Create(
                depTable.Department); 

            depTable.Id = newID;

            return (newID > 0);

        }

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

            return DBDepTable.Update(
                depTable.Id,
                depTable.Department); 

        }

#endregion

#region Public Methods

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

#endregion

#region Static Methods

        /// <summary>
        /// Deletes an instance of DepTable. Returns true on success.
        /// </summary>
        /// <param name="id"> id </param>
        /// <returns>bool</returns>
        public static bool Delete(
            int  id)
        {
            return DBDepTable.Delete(
                id);
        }

        /// <summary>
        /// Gets a count of DepTable.
        /// </summary>
        public static int GetCount()
        {
            return DBDepTable.GetCount();
        }

        private static IList<DepTable> LoadListFromReader(IDataReader reader)
        {
            IList<DepTable> depTableList = new List<DepTable>();
            try
            {
                while (reader.Read())
                {
                    DepTable depTable = new DepTable();
                    depTable.Id = Convert.ToInt32(reader["Id"]);
                    depTable.Department = reader["Department"].ToString();
                    depTableList.Add(depTable);

                }
            }
            finally
            {
                reader.Close();
            }

            return depTableList;

        }

        /// <summary>
        /// Gets an IList with all instances of DepTable.
        /// </summary>
        public static IList<DepTable> GetAll()
        {
            IDataReader reader = DBDepTable.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of DepTable.
        /// </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<DepTable> GetPage(int pageNumber, int pageSize, out int itemCount)
        {
            itemCount = 1;
            IDataReader reader = DBDepTable.GetPage(pageNumber, pageSize, out itemCount);
            return LoadListFromReader(reader);
        }

#endregion

    }

}

模板生成的操作类代码

模板生成的是一个基本的操作,如果我们有另外的自定义的需求,那么需要逐层添加代码。

时间: 2024-10-05 07:53:57

录播教室预约系统(三)-DepTable表的相关文章

录播教室预约系统(三)-DepTable表[普通表]

DepTable表  主要作用 存放单位名称 如图: 模板下载地址 CodeSmith版本为v6.5 第一步:用CodeSmith模板生成DepTable表相关的存储过程 生成的存储过程如下:  模板生成的DepTable表相关的基础存储过程 DepTable_Insert  插入数据DepTable_Update  更新数据 DepTable_SelectPage  获取分页数据 DepTable_SelectAll   获取所有数据 DepTable_SelectOne  获取某个数据 De

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

ClassRoom是与教室信息相关的表,包含教室的名称 这个表比前面介绍的DepTable表稍微复杂点,他有一列DepID 这列是与DepTable表中的ID相对应的的. 有了DepID这一列,我们就可以根据DepID 获取某单位的所有教室. 我们暂时把DepID这一列叫做外键列吧.对于拥有外键列的表,仍然可以用我们的codesmith模板生成相关的存储过程和类.有的时候我们希望能够以外键为参数查找相关的数据, 所以在对应的codesmith模板中增加了以外键为参数查找和以外键为参数自动分页查找

录播教室预约系统(一)-数据库表

数据库用的是mssql2005. 数据库表如下: USE [RoomSchedule] GO /****** 对象: Table [dbo].[ClassRoom] 脚本日期: 02/07/2015 22:41:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ClassRoom]( [Id] [int] IDENTITY(1000,1) NOT NULL, [RoomName] [nv

录播教室预约系统序言

大概一年多年前吧,朋友让我帮他做一个预约系统,当时刚好在学习networkcomms.net的2.3.1版本(本人当时已购买商业版),正好想测试一下networkcomms.net通讯框架的稳定性,于是便开发了此预约系统,基于networkcomms2.3.1 .部署后,系统非常的稳定,平时的cpu占有率非常的低,很少有达到5%以上的情况,当然用户数也并不多,并发数最多大概100左右,没有出现过宕机等情况. 下面将会写一系列文章,来介绍录播教室预约系统的开发,诸位朋友可以了解如何基于networ

录播教室预约系统(八)-客户端部门管理员增加教室

录播教室预约系统(八)-客户端部门管理员增加教室 管理员增加完成后,其他用户就可以看到多个教室了,如图: 我们看一下客户端增加教室的代码: 添加教室:             //声明一个教室实体类             ClassRoom classRoom = == = tcpConnection.SendReceiveObject<ResMessage>(, ,  (resMessage.Message == =  + 我们看一下服务器端的处理程序 构造函数中声明: //学校管理员添加

录播教室预约系统(九)-客户端部门管理员设定教室的预约规则

录播教室预约系统(九)-客户端部门管理员设定教室的预约规则 设置预约规则的客户端代码:              int stopHourInt = (int)stopHour.Value;            int stopMinuteInt = (int)stopMinute.Value;            //截止的时间             int stopTimeInt = stopHourInt * 100 + stopMinuteInt;            //提前的

录播教室预约系统(十)-客户端部门管理员重置本单位其他人员密码

录播教室预约系统(十)-客户端部门管理员重置本单位其他人员密码 客户端代码:  //发出的数据类型为int类型  ItemID为用户对应的ID             ResMessage resMessage = tcpConnnection.SendReceiveObject<ResMessage>("ReqResetPsw", "ResResetPsw", 5000, itemID);            if (resMessage.Messa

录播教室预约系统(六)-注册新用户

预约系统中,新用户可以自己注册,然后管理员开通. 注册界面如下: 客户端新用户注册代码:       button1_Click((txtUserID.Text.Trim ()==|| txtUserName.Text.Trim ()=== ==== = newTcpConnection.SendReceiveObject<ResMessage>(, ,  (resMessage.Message ==  + 服务器端的相关处理代码: 构造函数中声明:   //注册新用户            

录播教室预约系统(二)-服务器端与数据库的交互

每个表都对应这么多类,Data类库中的类还使用了很多存储过程,如果都需要我们手工书写,会很麻烦.所以我们采用了CodeSmith模板的方法. 需要注意的是,在我们的开发过程中,模板只生成基础操作相关的一部分代码,还是会有一部分需要我们手工添加上,即使这样,模板也帮助我们做了很多工作. 模板会生成  基础数据操作部分的: (1)存储过程 (2)Data层的相关的类 每个数据表对应一个 (3)Business层相关的类  每个数据表对应两个(实体类,操作类) www.cnblogs.com/netw