NetworkComms网络通信框架配套CodeSmith模板使用简介

NetworkComms网络通信程序开发过程中,与数据库的交互部分,很多代码我们采用CodeSmith模板来生成。

此套CodeSmith模板目前只支持sql server 数据库

模板下载地址:  【模板下载】分享我所使用的数据库框架

我们以Sql server数据库为例 简单的看一下如何使用CodeSmith模板生成相关文件

NetworkComms网络通信框架配套CodeSmith模板使用简介_第1张图片

首先双击打开模板文件

NetworkComms网络通信框架配套CodeSmith模板使用简介_第2张图片

第二步:打开属性窗口

NetworkComms网络通信框架配套CodeSmith模板使用简介_第3张图片

 

NetworkComms网络通信框架配套CodeSmith模板使用简介_第4张图片

NetworkComms网络通信框架配套CodeSmith模板使用简介_第5张图片

数据库配置完成后 ,选择相应的表 

NetworkComms网络通信框架配套CodeSmith模板使用简介_第6张图片

NetworkComms网络通信框架配套CodeSmith模板使用简介_第7张图片

生成的代码如下:

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

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 DBPlDocs
    {
        /// <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 PlDocs table. Returns new integer id.
        /// </summary>
        /// <param name="title"> title </param>
        /// <param name="description"> description </param>
        /// <returns>int</returns>
        public static int Create(
            string title, 
            string description) 
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "PlDocs_Insert", 2);
            sph.DefineSqlParameter("@Title", SqlDbType.NVarChar, 200, ParameterDirection.Input, title);
            sph.DefineSqlParameter("@Description", SqlDbType.NVarChar, 500, ParameterDirection.Input, description);
            int newID = Convert.ToInt32(sph.ExecuteScalar());
            return newID;
        }


        /// <summary>
        /// Updates a row in the PlDocs table. Returns true if row updated.
        /// </summary>
        /// <param name="id"> id </param>
        /// <param name="title"> title </param>
        /// <param name="description"> description </param>
        /// <returns>bool</returns>
        public static bool Update(
            int  id, 
            string title, 
            string description) 
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "PlDocs_Update", 3);
            sph.DefineSqlParameter("@ID", SqlDbType.Int, ParameterDirection.Input, id);
            sph.DefineSqlParameter("@Title", SqlDbType.NVarChar, 200, ParameterDirection.Input, title);
            sph.DefineSqlParameter("@Description", SqlDbType.NVarChar, 500, ParameterDirection.Input, description);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
            
        }
        
        /// <summary>
        /// Deletes a row from the PlDocs 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(), "PlDocs_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 PlDocs table.
        /// </summary>
        /// <param name="id"> id </param>
        public static IDataReader GetOne(
            int  id)  
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "PlDocs_SelectOne", 1);
            sph.DefineSqlParameter("@ID", SqlDbType.Int, ParameterDirection.Input, id);
            return sph.ExecuteReader();
            
        }
        
     
        
        
        /// <summary>
        /// Gets a count of rows in the PlDocs table.
        /// </summary>
        public static int GetCount()
        {
            
            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "PlDocs_GetCount",
                null));
    
        }
        
        
     
        
        /// <summary>
        /// Gets an IDataReader with all rows in the PlDocs table.
        /// </summary>
        public static IDataReader GetAll()
        {
            
            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "PlDocs_SelectAll",
                null);
    
        }
        
        /// <summary>
        /// Gets a page of data from the PlDocs 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(), "PlDocs_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();
        
        }
        
      
    
    }

}
View Code

 

你可能感兴趣的:(NetworkComms网络通信框架配套CodeSmith模板使用简介)