我的c#代码生成器(生成SQL2005的和ACCESS的)
程序员文章站
2024-01-05 08:19:07
...
生成 内容 1.ACCESS 2.net2.0的泛型操作的 3.。net3.5-4.0的linq to entity 生成 的net2.0 代码 MODEL using System;using System.Collections.Generic;namespace ChineseCode.Bll{public class News{/// summary/// /// /summarypublic int NewsId{set;get;
生成内容
1.ACCESS
2.net2.0的泛型操作的
3.。net3.5-4.0的linq to entity
生成的net2.0代码
MODEL
using System; using System.Collections.Generic; namespace ChineseCode.Bll { public class News { ////// /// public int NewsId { set; get; } ////// /// public int UserID { set; get; } ////// /// public int TypeID { set; get; } ////// /// public string Author { set; get; } ////// /// public string Title { set; get; } ////// /// public string Excerpts { set; get; } ////// /// public string Body { set; get; } ////// /// public string Image { set; get; } ////// /// public int Views { set; get; } ////// /// public bool IsApprived { set; get; } ////// /// public bool CheckResult { set; get; } ////// /// public int DisplayOrder { set; get; } ////// /// public bool IsCommend { set; get; } ////// /// public string Sourse { set; get; } ////// /// public DateTime CreateDate { set; get; } ////// /// public string PostIP { set; get; } ////// /// public DateTime? UpdateDate { set; get; } //////获取单个模型 /// private Users _Users; public Users Users { get { if (_Users == null) { _Users = UsersManager.GetModel(this.UserID); return _Users; } return _Users; } } //////获取单个模型 /// private NewsType _NewsType; public NewsType NewsType { get { if (_NewsType == null) { _NewsType = NewsTypeManager.GetModel(this.TypeID); return _NewsType; } return _NewsType; } } } }
BLL
using System; using System.Collections.Generic; using System.Text; using JrdLibrary; using System.Data; using System.Data.SqlClient; namespace ChineseCode.Bll { public class NewsManager { #region 获取实体方法 ////// 获取一个实体类 /// public static News GetModel(SqlDataReader reader) { return SqlDataReaderExt.ReaderToModel(reader); } /// /// 获取一个实体类集合 /// public static ListGetList(SqlDataReader reader) { return SqlDataReaderExt.ReaderToList (reader); } #endregion /// /// 创建一个有默认值的Model /// public static News CreateModel() { News model = new News(); model.Views = 0; model.IsApprived = true; model.CheckResult = true; model.DisplayOrder = 0; model.IsCommend = false; model.CreateDate = DateTime.Now; return model; } ////// 增加一条数据 /// public static int Insert(News model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Jrd_News("); strSql.Append("UserID,TypeID,Author,Title,Excerpts,Body,Image,Views,IsApprived,CheckResult,DisplayOrder,IsCommend,Sourse,CreateDate,PostIP)"); strSql.Append(" values ("); strSql.Append("@UserID,@TypeID,@Author,@Title,@Excerpts,@Body,@Image,@Views,@IsApprived,@CheckResult,@DisplayOrder,@IsCommend,@Sourse,@CreateDate,@PostIP)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.Int,4), new SqlParameter("@TypeID", SqlDbType.Int,4), new SqlParameter("@Author",SqlDbType.NVarChar,100), new SqlParameter("@Title",SqlDbType.NVarChar,400), new SqlParameter("@Excerpts",SqlDbType.NVarChar,1000), new SqlParameter("@Body",SqlDbType.NText), new SqlParameter("@Image",SqlDbType.VarChar,100), new SqlParameter("@Views", SqlDbType.Int,4), new SqlParameter("@IsApprived",SqlDbType.Bit,1), new SqlParameter("@CheckResult",SqlDbType.Bit,1), new SqlParameter("@DisplayOrder", SqlDbType.Int,4), new SqlParameter("@IsCommend",SqlDbType.Bit,1), new SqlParameter("@Sourse",SqlDbType.NVarChar,200), new SqlParameter("@CreateDate",SqlDbType.DateTime), new SqlParameter("@PostIP",SqlDbType.VarChar,20) }; parameters[0].Value = model.UserID; parameters[1].Value = model.TypeID; parameters[2].Value = model.Author; parameters[3].Value = model.Title; parameters[4].Value = model.Excerpts; parameters[5].Value = model.Body; parameters[6].Value = model.Image; parameters[7].Value = model.Views; parameters[8].Value = model.IsApprived; parameters[9].Value = model.CheckResult; parameters[10].Value = model.DisplayOrder; parameters[11].Value = model.IsCommend; parameters[12].Value = model.Sourse; parameters[13].Value = model.CreateDate; parameters[14].Value = model.PostIP; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } ////// 更新一条数据 /// public static bool Update(News model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Jrd_News set "); strSql.Append("UserID=@userID,TypeID=@typeID,Author=@author,Title=@title,Excerpts=@excerpts,Body=@body,Image=@image,Views=@views,IsApprived=@isApprived,CheckResult=@checkResult,DisplayOrder=@displayOrder,IsCommend=@isCommend,Sourse=@sourse,PostIP=@postIP,UpdateDate=@updateDate "); strSql.Append(" where NewsId=@NewsId"); SqlParameter[] parameters = { new SqlParameter("@UserID", SqlDbType.Int,4), new SqlParameter("@TypeID", SqlDbType.Int,4), new SqlParameter("@Author",SqlDbType.NVarChar,100), new SqlParameter("@Title",SqlDbType.NVarChar,400), new SqlParameter("@Excerpts",SqlDbType.NVarChar,1000), new SqlParameter("@Body",SqlDbType.NText), new SqlParameter("@Image",SqlDbType.VarChar,100), new SqlParameter("@Views", SqlDbType.Int,4), new SqlParameter("@IsApprived",SqlDbType.Bit,1), new SqlParameter("@CheckResult",SqlDbType.Bit,1), new SqlParameter("@DisplayOrder", SqlDbType.Int,4), new SqlParameter("@IsCommend",SqlDbType.Bit,1), new SqlParameter("@Sourse",SqlDbType.NVarChar,200), new SqlParameter("@PostIP",SqlDbType.VarChar,20), new SqlParameter("@UpdateDate",SqlDbType.DateTime), new SqlParameter("@NewsId", SqlDbType.Int,4) }; parameters[0].Value = model.UserID; parameters[1].Value = model.TypeID; parameters[2].Value = model.Author; parameters[3].Value = model.Title; parameters[4].Value = model.Excerpts; parameters[5].Value = model.Body; parameters[6].Value = model.Image; parameters[7].Value = model.Views; parameters[8].Value = model.IsApprived; parameters[9].Value = model.CheckResult; parameters[10].Value = model.DisplayOrder; parameters[11].Value = model.IsCommend; parameters[12].Value = model.Sourse; parameters[13].Value = model.PostIP; parameters[14].Value = model.UpdateDate; parameters[15].Value = model.NewsId; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } ////// 删除一条数据 /// public static bool Delete(int newsId) { StringBuilder strSql=new StringBuilder(); strSql.Append("delete from Jrd_News "); strSql.Append(" where NewsId=@NewsId"); SqlParameter[] parameters = { new SqlParameter("@NewsId", SqlDbType.Int,4) }; parameters[0].Value = newsId; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } ////// 是否存在该记录 /// public static bool Exists(int newsId) { StringBuilder strSql=new StringBuilder(); strSql.Append("select count(1) from Jrd_News"); strSql.Append(" where NewsId=@NewsId"); SqlParameter[] parameters = { new SqlParameter("@NewsId", SqlDbType.Int,4) }; parameters[0].Value = newsId; return DbHelperSQL.Exists(strSql.ToString(),parameters); } ////// 得到一个对象实体 /// public static News GetModel(int newsId) { StringBuilder strSql=new StringBuilder(); strSql.Append("select top 1 NewsId,UserID,TypeID,Author,Title,Excerpts,Body,Image,Views,IsApprived,CheckResult,DisplayOrder,IsCommend,Sourse,CreateDate,PostIP from Jrd_News "); strSql.Append(" where NewsId=@NewsId"); SqlParameter[] parameters = { new SqlParameter("@NewsId", SqlDbType.Int,4) }; parameters[0].Value = newsId; return GetModel(DbHelperSQL.ExecuteReader(strSql.ToString(),parameters)); } ////// 获得全部数据 /// public static ListGetAll() { StringBuilder strSql=new StringBuilder(); strSql.Append("select NewsId,UserID,TypeID,Author,Title,Excerpts,Body,Image,Views,IsApprived,CheckResult,DisplayOrder,IsCommend,Sourse,CreateDate,PostIP "); strSql.Append(" FROM Jrd_News "); return GetList(DbHelperSQL.ExecuteReader(strSql.ToString(),null)); } /// ///根据UserID获取列表 /// public static ListGetNewsByUserID(string andStr,int userID, int PageSize,int PageCurrent,string FdOrder, ref int totalCount) { SqlQuery query = new SqlQuery(); query.Add("UserID", userID, QueryOperator.Equal); return GetPager(query.GetWhere + andStr, PageSize, PageCurrent, FdOrder, SortType.DESC, ref totalCount); } /// ///根据TypeID获取列表 /// public static ListGetNewsByTypeID(string andStr,int typeID, int PageSize,int PageCurrent,string FdOrder, ref int totalCount) { SqlQuery query = new SqlQuery(); query.Add("TypeID", typeID, QueryOperator.Equal); return GetPager(query.GetWhere + andStr, PageSize, PageCurrent, FdOrder, SortType.DESC, ref totalCount); } /// /// 根据排序分页获取数据列表 /// public static ListGetPagerByDisplayOrder(string strWhere, int PageSize, int PageCurrent,SortType sort, ref int RecordCount) { return GetPager(strWhere, PageSize, PageCurrent, "DisplayOrder" , sort, ref RecordCount); } /// /// 根据浏览分页获取数据列表 /// public static ListGetPagerByViews(string strWhere, int PageSize, int PageCurrent, SortType sort, ref int RecordCount) { return GetPager(strWhere, PageSize, PageCurrent, "Views" , sort, ref RecordCount); } /// /// 分页获取数据列表 /// public static ListGetPager(string strWhere, int PageSize, int PageCurrent, string FdOrder,SortType sort, ref int RecordCount) { string sqlStr="select cast(NewsId as int) as NewsId,UserID,TypeID,Author,Title,Excerpts,Body,Image,Views,IsApprived,CheckResult,DisplayOrder,IsCommend,Sourse,CreateDate,PostIP,UpdateDate from Jrd_News "; if (string.IsNullOrEmpty(FdOrder) || FdOrder == "") FdOrder ="NewsId Desc"; else FdOrder = FdOrder + " " + sort.ToString(); return GetList(PagerBll.GetPagerReader(sqlStr + strWhere, PageSize, PageCurrent, "", FdOrder, ref RecordCount)); } } }