NetworkComms網路通訊架構配套CodeSmith模板使用簡介

來源:互聯網
上載者:User

標籤:

NetworkComms網路通訊程式開發過程中,與資料庫的互動部分,很多代碼我們採用CodeSmith模板來產生。

此套CodeSmith模板目前只支援sql server 資料庫

模板:  【模板下載】分享我所使用的資料庫架構

我們以Sql server資料庫為例 簡單的看一下如何使用CodeSmith模板產生相關檔案

首先雙擊開啟模板檔案

第二步:開啟屬性視窗

 

資料庫配置完成後 ,選擇相應的表 

產生的程式碼如下:

// 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模板使用簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.