標籤:
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模板使用簡介