铜梁视窗代码生成器C#,自动生成Model,BaseDAL,包含所有Insert,Update,Delete和QueryList方法
我的这个方法跟中软代码生成器有明显区别:只生成你需要的,不生成那些不好操作的,适合初级人员使用
Model 案例
using System;
using Daneas.Utility.Data;
namespace Daneas.Model
{
/// <summary>
/// CMS_Area:实体类(属性说明自动提取数据库字段的描述信息)
/// </summary>
[Serializable]
public partial class CMS_Area
{
///<summary>
///字段描述:地区ID
///</summary>
public System.Int32 Aid { get; set; }
///<summary>
///字段描述:
///</summary>
public System.String AreaName { get; set; }
///<summary>
///字段描述:
///</summary>
public System.String Keywords { get; set; }
///<summary>
///字段描述:
///</summary>
public System.String Description { get; set; }
///<summary>
///字段描述:
///</summary>
public System.Int32 ParentId { get; set; }
}
}
BaseDAL案例
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using Daneas.Utility.Data;
using Daneas.Utility.Universals;
using Daneas.Model;
namespace Daneas.DAL
{
/// <summary>
/// CMS_Area:基础数据操作类(属性说明自动提取数据库字段的描述信息)
/// </summary>
public partial class CMS_AreaBaseDAL
{
/// <summary>
/// 判断数据是否存在
///<param name="dic">key:字段名称、value字段值</param>
/// <returns>true为存在</returns>
public static bool IsExist(Dictionary<string,object> dic)
{
string sql = "SELECT top 1 1 FROM CMS_Area WHERE 1=1";
List<SqlParameter> paramList = new List<SqlParameter>();
if (dic == null || dic.Count <= 0)
{
return true;
}
foreach (var item in dic)
{
sql += " AND " + item.Key + "=@" + item.Key;
paramList.Add(DBHelper.InitSqlParam("@" + item.Key, item.Value));
}
int num= (int)DBHelper.ExecuteScalar(sql,paramList.ToArray());
return num > 0;
}
/// <summary>
/// 获取实例
/// </summary>
/// <param name="ID">ID必须是数字</param>
/// <returns></returns>
public static CMS_Area GetById(System.Int32 ID)
{
string sql="SELECT * FROM CMS_Area WHERE Aid="+ID;
DataTable dt=DBHelper.ExecutQuery(sql.ToString()).Tables[0] ;
if(dt==null||dt.Rows.Count<=0){return null;}
return Convers<CMS_Area>.T2L(dt)[0];
}
/// <summary>
/// 获取实例(事务)
/// </summary>
/// <param name="ID">id必须是数字</param>
/// <returns></returns>
public static CMS_Area GetById(System.Int32 ID,SqlCommand cmd)
{
string sql="SELECT * FROM CMS_Area WHERE Aid="+ID;
DataTable dt= DBHelper.ExecutQuery(sql.ToString(),cmd).Tables[0] ;
if(dt==null||dt.Rows.Count<=0){return null;}
return Convers<CMS_Area>.T2L(dt)[0];
}
/// <summary>
/// 新增实例(返回ID)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static System.Int32 Create(CMS_Area model)
{
StringBuilder sql = new StringBuilder();
sql.Append("INSERT INTO CMS_Area ( ");
sql.Append("AreaName,Keywords,Description,ParentId");
sql.Append(") VALUES( ");
sql.Append("@AreaName,@Keywords,@Description,@ParentId");
sql.Append(") ");
sql.Append(";select @@IDENTITY ");
List<SqlParameter> paramList = new List<SqlParameter>();
paramList.Add(DBHelper.InitSqlParam("@AreaName",model.AreaName==null?"":model.AreaName));
paramList.Add(DBHelper.InitSqlParam("@Keywords",model.Keywords==null?"":model.Keywords));
paramList.Add(DBHelper.InitSqlParam("@Description",model.Description==null?"":model.Description));
paramList.Add(DBHelper.InitSqlParam("@ParentId",model.ParentId));
object obj= DBHelper.ExecuteScalar(sql.ToString(),paramList.ToArray()) ;
if(obj!=null){ return Convert.ToInt32(obj) ;}else{return -1;}
}
/// <summary>
/// 新增实例(事务)(返回ID)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static System.Int32 Create(CMS_Area model,SqlCommand cmd)
{
StringBuilder sql = new StringBuilder();
sql.Append("INSERT INTO CMS_Area ( ");
sql.Append("AreaName,Keywords,Description,ParentId");
sql.Append(") VALUES( ");
sql.Append("@AreaName,@Keywords,@Description,@ParentId");
sql.Append(") ");
sql.Append(";select @@IDENTITY ");
List<SqlParameter> paramList = new List<SqlParameter>();
paramList.Add(DBHelper.InitSqlParam("@AreaName",model.AreaName==null?"":model.AreaName));
paramList.Add(DBHelper.InitSqlParam("@Keywords",model.Keywords==null?"":model.Keywords));
paramList.Add(DBHelper.InitSqlParam("@Description",model.Description==null?"":model.Description));
paramList.Add(DBHelper.InitSqlParam("@ParentId",model.ParentId));
object obj= DBHelper.ExecuteScalar(sql.ToString(),paramList.ToArray(),cmd);
if(obj!=null){ return Convert.ToInt32(obj) ;}else{return -1;}
}
/// <summary>
/// 更新实例
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static bool Update(CMS_Area model)
{
StringBuilder sql = new StringBuilder();
sql.Append("UPDATE CMS_Area ");
sql.Append("SET AreaName=@AreaName");
sql.Append(",Keywords=@Keywords");
sql.Append(",Description=@Description");
sql.Append(",ParentId=@ParentId");
sql.Append(" WHERE Aid=@Aid");
List<SqlParameter> paramList = new List<SqlParameter>();
paramList.Add(DBHelper.InitSqlParam("@Aid",model.Aid));
paramList.Add(DBHelper.InitSqlParam("@AreaName",model.AreaName==null?"":model.AreaName));
paramList.Add(DBHelper.InitSqlParam("@Keywords",model.Keywords==null?"":model.Keywords));
paramList.Add(DBHelper.InitSqlParam("@Description",model.Description==null?"":model.Description));
paramList.Add(DBHelper.InitSqlParam("@ParentId",model.ParentId));
return (int)DBHelper.ExecuteNonQuery(sql.ToString(),paramList.ToArray())>0 ;
}
/// <summary>
/// 更新实例(事务)
/// </summary>
/// <param name="model"></param>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool Update(CMS_Area model,SqlCommand cmd)
{
StringBuilder sql = new StringBuilder();
sql.Append("UPDATE CMS_Area ");
sql.Append("SET AreaName=@AreaName");
sql.Append(",Keywords=@Keywords");
sql.Append(",Description=@Description");
sql.Append(",ParentId=@ParentId");
sql.Append(" WHERE Aid=@Aid");
List<SqlParameter> paramList = new List<SqlParameter>();
paramList.Add(DBHelper.InitSqlParam("@Aid",model.Aid));
paramList.Add(DBHelper.InitSqlParam("@AreaName",model.AreaName==null?"":model.AreaName));
paramList.Add(DBHelper.InitSqlParam("@Keywords",model.Keywords==null?"":model.Keywords));
paramList.Add(DBHelper.InitSqlParam("@Description",model.Description==null?"":model.Description));
paramList.Add(DBHelper.InitSqlParam("@ParentId",model.ParentId));
return (int)DBHelper.ExecuteNonQuery(sql.ToString(),paramList.ToArray(),cmd)>0 ;
}
/// <summary>
/// 删除实例
/// </summary>
/// <param name="Id">id必须是数字</param>
/// <returns></returns>
public static bool DeleteById(Int64 Id)
{
string sql="DELETE FROM CMS_Area WHERE Aid=@Id";
SqlParameter paramList = DBHelper.InitSqlParam("@Id",Id);
return (int)DBHelper.ExecuteNonQuery(sql,paramList)>0;
}
/// <summary>
/// 删除实例(事务)
/// </summary>
/// <param name="Id">id必须是数字</param>
/// <param name="cmd"></param>
/// <returns></returns>
public static bool DeleteById(Int64 Id,SqlCommand cmd)
{
string sql="DELETE FROM CMS_Area WHERE Aid=@Id";
SqlParameter paramList = DBHelper.InitSqlParam("@Id",Id);
return (int)DBHelper.ExecuteNonQuery(sql,paramList,cmd)>0;
}
/// <summary>
/// 获取实体列表,不分页
/// </summary>
/// <param name="dic">查询字段,值,And关系</param>
/// <returns></returns>
public static List<CMS_Area> GetList(Dictionary<string,object> dic)
{
string sql="SELECT * FROM CMS_Area WHERE 1=1";
List<SqlParameter> paramList = new List<SqlParameter>();
foreach (var item in dic)
{
sql += " AND " + item.Key + "=@" + item.Key;
paramList.Add(DBHelper.InitSqlParam("@" + item.Key, item.Value));
}
DataTable dt= DBHelper.ExecutQuery(sql.ToString(),paramList.ToArray()).Tables[0] ;
return Convers<CMS_Area>.T2L(dt);;
}
/// <summary>
/// 获取实例对象
/// </summary>
/// <param name="model">实例对象中的参数,或关系</param>
/// <returns></returns>
public static List<CMS_Area> GetList(CMS_Area model,int pageIndex,int pageSize,out int total)
{
string sql="SELECT * FROM CMS_Area WHERE 1=1";
List<SqlParameter> paramList = new List<SqlParameter>();
if(model.Aid!=Int32.MinValue && model.Aid!=Int32.MinValue)
{
sql+=" OR Aid = @Aid";
paramList.Add(DBHelper.InitSqlParam("@Aid",model.Aid));
}
if(!string.IsNullOrWhiteSpace(model.AreaName))
{
sql+=" OR AreaName LIKE '%'+@AreaName+'%'";
paramList.Add(DBHelper.InitSqlParam("@AreaName",model.AreaName));
}
if(!string.IsNullOrWhiteSpace(model.Keywords))
{
sql+=" OR Keywords LIKE '%'+@Keywords+'%'";
paramList.Add(DBHelper.InitSqlParam("@Keywords",model.Keywords));
}
if(!string.IsNullOrWhiteSpace(model.Description))
{
sql+=" OR Description LIKE '%'+@Description+'%'";
paramList.Add(DBHelper.InitSqlParam("@Description",model.Description));
}
if(model.ParentId!=Int32.MinValue && model.ParentId!=Int32.MinValue)
{
sql+=" OR ParentId = @ParentId";
paramList.Add(DBHelper.InitSqlParam("@ParentId",model.ParentId));
}
string OrderByStr = " ORDER BY Aid DESC";
DataTable dt= DBHelper.QueryPage(sql.ToString(),OrderByStr,paramList.ToArray(),pageIndex,pageSize,out total) ;
return Convers<CMS_Area>.T2L(dt);
}
/// <summary>
/// 获取实体列表,分页
/// </summary>
/// <param name="dic">查询字段,值,或关系</param>
/// <returns></returns>
public static List<CMS_Area> GetQueryList(Dictionary<string,object> dic,string sortColumn,int pageIndex,int pageSize,out int total)
{
string sql="SELECT * FROM CMS_Area WHERE 1=1";
List<SqlParameter> paramList = new List<SqlParameter>();
if(dic!=null&&dic.Count>0){
int i=0;
sql+=" And (";
foreach (var item in dic)
{
if(i==0){
sql += item.Key + " LIKE '%'+ @" + item.Key+" + '%'";}
else{ sql += " Or " + item.Key + " LIKE '%'+ @" + item.Key+" + '%'";}
paramList.Add(DBHelper.InitSqlParam("@" + item.Key, item.Value));
i+=1;
}
sql+=")";
}
List<SortParam> sorts=new List<SortParam>();
sorts.Add(new SortParam() { SortColunm = sortColumn , SortType = SortType.DESC});
DataTable dt= DBHelper.QueryPage(sql.ToString(),sorts,paramList.ToArray(),pageIndex,pageSize,out total);
return Convers<CMS_Area>.T2L(dt);
}
/// <summary>
/// 获取实体列表,分页
/// </summary>
/// <param name="dic">查询字段,值,Dic1或关系,Dic2与关系</param>
/// <returns></returns>
public static List<CMS_Area> GetQueryList(Dictionary<string,object> dic1,Dictionary<string,object> dic2,string sortColumn,int pageIndex,int pageSize,out int total)
{
string sql="SELECT * FROM CMS_Area WHERE 1=1";
List<SqlParameter> paramList = new List<SqlParameter>();
if(dic1!=null&&dic1.Count>0){
int i=0;
sql+=" And (";
foreach (var item in dic1)
{
if(i==0){
sql += item.Key + " LIKE '%'+ @" + item.Key+" + '%'";}
else{ sql += " Or " + item.Key + " LIKE '%'+ @" + item.Key+" + '%'";}
paramList.Add(DBHelper.InitSqlParam("@" + item.Key, item.Value));
i+=1;
}
sql+=")";
}
if(dic2!=null&&dic2.Count>0){
foreach (var item in dic2)
{
sql += " And " + item.Key + "=@" + item.Key;
paramList.Add(DBHelper.InitSqlParam("@" + item.Key, item.Value));
}
}
List<SortParam> sorts=new List<SortParam>();
sorts.Add(new SortParam() { SortColunm = sortColumn , SortType = SortType.DESC});
DataTable dt= DBHelper.QueryPage(sql.ToString(),sorts,paramList.ToArray(),pageIndex,pageSize,out total);
return Convers<CMS_Area>.T2L(dt);
}
/// <summary>
/// 获取所有实例对象
/// </summary>
/// <returns></returns>
public static List<CMS_Area> GetAllModels()
{
string sql="SELECT * FROM CMS_Area";
DataTable dt= DBHelper.ExecutQuery(sql).Tables[0] ;
return Convers<CMS_Area>.T2L(dt);
}
/// <summary>
/// 获取所有实例对象
/// </summary>
/// <returns></returns>
public static List<CMS_Area> GetTopBy(int top,string orderby)
{
string sql="SELECT top( "+top+") * FROM CMS_Area order by "+orderby +" desc ";
DataTable dt= DBHelper.ExecutQuery(sql).Tables[0] ;
return Convers<CMS_Area>.T2L(dt);
}
}
}
记住,web.config的conn命名默认default
<appSettings>
<add key="BaseCodePath" value="E:/快盘/TLSC/"/>
<add key="ModelNameSpace" value="Daneas.Model"/>
<add key="DALNameSpace" value="Daneas.DAL"/>
<add key="connName" value="default"/>
如有研究,请下载附件 附件地址 http://pan.baidu.com/s/1o6AFQiu