.net代码生成模板(MyGeneration版)

         经过一段时间的努力,终于把.net代码生成模板完成了。高兴!!

背景:
         前段时间比较有空,就拿几个ORM框架来看看,试用之后发觉ActiveRecord和SubSonic还不错,于是就深入一点拿来开发,没想到在开发的过程中却遇到许多莫名其妙的问题,心里真是郁闷!后来想想既然这些东西都是不可控制的,那就找一个代码生成器吧,至少生成的代码自己是可以看到的,听朋友说动软的代码生成器还不错就Down了一个下来,不过生成代码之后多少有些令人遗憾,因为里面的方法太少了,而且软件的版本更新也太慢了,最后想想为什么自己不写一个出来呢,用代码生成器来写,只要把平时经常要操作的方法写出来就OK了。

代码生成器MyGeneration:
        我用的代码生成器是MyGeneration,本人认为这是一个相当不错的东东,免费的(下个版本即将开源),安装文件也小,才2.21M,可用C#编写,更重要的是它提供了一个完整的操作数据库所用到的类库。有关MyGeneration的文档请参考DDLLY写的《强大的代码生成工具MyGeneration 》以及MyGeneration的官方网站:http://www.mygenerationsoftware.com/

.net代码生成模板:
使用步骤:
1、把下载的压缩包(在文章的末尾处下载)解压之后,发现里面会有两个文件:ED.zeus和DbHelper.cs;
2、把DbHelper.cs复制到MyGeneration的安装目录下,如:c:\Program Files\MyGeneration;
3、在你工程中的配置文件Web.config里加上连接数据库的字符串,如:

None.gif < connectionStrings >
None.gif    
< add  name  ="Test"  connectionString  ="server=(local);database=Test;uid=sa;pwd="  providerName  ="System.Data.SqlClient" />
None.gif
</ connectionStrings >

记得加上providerName 这个参数哦。
4、打开ED.zeus,Edit-->Default Settings-->Connection选项卡-->Database TargetMapping框-->设DbTarget为DbType。



运行,界面如下:



其中的Save按钮是用来把窗口内的东西保存到注册表中的,下次打开的时候就不用再修改了。其它的都是一眼就能看得懂的,这里不再讲述。
5、填写好参数后,点击OK按钮,生成代码。

注:目前只支持sql server 2000,因为我对其它的数据库不熟。

生成的代码:
生成的代码默认为Model和DAL两个文件夹:
Model是用来放实体类文件的,而DAL是用来放操作类文件及数据库操作基础类和公共类。

实体类文件:
由私有变量、默认构造函数、属性三部分组成。其中属性支持外键的功能,当然了,只有1:N中的主表,如果也包含从表的话,就会造成资源的浪费和性能的下降。

None.gif namespace  Coree.Model
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Child
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有成员#region 私有成员
InBlock.gif
InBlock.gif        
private int _ChildID; 
InBlock.gif        
private Guid _ParentID; 
InBlock.gif        
private string _Name; 
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
默认构造函数#region 默认构造函数
InBlock.gif
InBlock.gif        
public Child()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _ChildID 
= 0;
InBlock.gif            _ParentID 
= Guid.Empty;
InBlock.gif            _Name 
= String.Empty;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有属性#region 公有属性
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 主键
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int ChildID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ChildID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ChildID = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 父类
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Guid ParentID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ParentID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ParentID = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 姓名
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 父类
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Parent Parent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(_ParentID != Guid.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                     
return (new Coree.DAL.Parent()).GetModel(_ParentID);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                     
return null;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

数据操作类文件:
由一些常用的方法组成,有Exist,GetCount,GetModel,GetByID,GetList,Add,Update,Delete等,这些方法都有重载的方法。其中GetList返回的是DataSet,Delete方法用到了事务,具体可看生成的代码。

None.gif namespace  Coree.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Child
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------Exist-----------#region -----------Exist-----------
InBlock.gif
InBlock.gif        
public bool Exist()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Exist(""null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <param name="query">格式如: where  Name=? and Pwd=? order by Name desc</param>
InBlock.gif        public bool Exist(string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "select count(*) from Child " + CommonUtil.ProcessQuery(query, parameterValues);
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            CommonUtil.AddInParameter(db, dbCommand, 
"Child", query, parameterValues);
InBlock.gif            
return Convert.ToInt32(db.ExecuteScalar(dbCommand)) > 0 ? true : false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------数据统计-----------#region -----------数据统计-----------
InBlock.gif
InBlock.gif        
public int GetCount()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetCount(""null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <param name="query">格式如: where  Name=? and Pwd=? order by Name desc</param>
InBlock.gif        public int GetCount(string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "select count(*) from Child " + CommonUtil.ProcessQuery(query, parameterValues);
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            CommonUtil.AddInParameter(db, dbCommand, 
"Child", query, parameterValues);
InBlock.gif            
return Convert.ToInt32(db.ExecuteScalar(dbCommand));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------得到一个对象实体---------#region -----------得到一个对象实体---------
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到一个对象实体
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Coree.Model.Child GetModel(object ChildID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            ds 
= GetByID(ChildID);
InBlock.gif            Coree.Model.Child model 
= new Coree.Model.Child();
InBlock.gif            
if (ds.Tables.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                model.ChildID 
= (int)ds.Tables[0].Rows[0]["ChildID"];
InBlock.gif                model.ParentID 
= (Guid)ds.Tables[0].Rows[0]["ParentID"];
InBlock.gif                model.Name 
= (string)ds.Tables[0].Rows[0]["Name"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return model;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------GetByID-----------#region -----------GetByID-----------
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// GetByID
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DataSet GetByID(object ChildID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetList("where ChildID=?", ChildID);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------数据(DataSet)------------#region -----------数据(DataSet)------------
InBlock.gif
InBlock.gif        
public DataSet GetList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetList("");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <param name="query">格式如: where  Name=? and Pwd=? order by Name desc</param>
InBlock.gif        public DataSet GetList(string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "select * from Child" + CommonUtil.ProcessQuery(query, parameterValues);
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            CommonUtil.AddInParameter(db, dbCommand, 
"Child", query, parameterValues);
InBlock.gif            
return db.ExecuteDataSet(dbCommand);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public DataSet GetList(int StartIndex, int PageSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetList(StartIndex, PageSize, "");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <param name="query">格式如: where  Name=? and Pwd=? order by Name desc</param>
InBlock.gif        public DataSet GetList(int StartIndex, int PageSize, string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = CommonUtil.BuildPagerQuery("Child""ChildID""int", StartIndex, PageSize, query, parameterValues);
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            db.AddInParameter(dbCommand, 
"@StartIndex", DbType.Int32, StartIndex);
InBlock.gif            db.AddInParameter(dbCommand, 
"@PageSize", DbType.Int32, PageSize);
InBlock.gif            CommonUtil.AddInParameter(db, dbCommand, 
"Child", query, parameterValues);
InBlock.gif            
return db.ExecuteDataSet(dbCommand);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------添加-----------#region -----------添加-----------
InBlock.gif
InBlock.gif        
public int Add(Coree.Model.Child model)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!new Parent().Exist("where ParentID=?", model.ParentID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("父表Parent中不存在ParentID值为" + model.ParentID + "的列");
ExpandedSubBlockEnd.gif            }

InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "insert into Child(ParentID,Name) values(@ParentID,@Name)";
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ParentID", CommonUtil.GetDbType("Child""ParentID"), model.ParentID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@Name", CommonUtil.GetDbType("Child""Name"), model.Name);
InBlock.gif            
return db.ExecuteNonQuery(dbCommand);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Add(Guid ParentID,string Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!new Parent().Exist("where ParentID=?", ParentID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("父表Parent中不存在ParentID值为" + ParentID + "的列");
ExpandedSubBlockEnd.gif            }

InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "insert into Child(ParentID,Name) values(@ParentID,@Name)";
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ParentID", CommonUtil.GetDbType("Child""ParentID"), ParentID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@Name", CommonUtil.GetDbType("Child""Name"), Name);
InBlock.gif            
return db.ExecuteNonQuery(dbCommand);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------更新-----------#region -----------更新-----------
InBlock.gif
InBlock.gif        
public int Update(Coree.Model.Child model)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!new Parent().Exist("where ParentID=?", model.ParentID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("父表Parent中不存在ParentID值为" + model.ParentID + "的列");
ExpandedSubBlockEnd.gif            }

InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "update Child set ParentID = @ParentID,Name = @Name where ChildID=@ChildID";
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), model.ChildID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ParentID", CommonUtil.GetDbType("Child""ParentID"), model.ParentID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@Name", CommonUtil.GetDbType("Child""Name"), model.Name);
InBlock.gif            
return db.ExecuteNonQuery(dbCommand);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Update(int ChildID,Guid ParentID,string Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!new Parent().Exist("where ParentID=?", ParentID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("父表Parent中不存在ParentID值为" + ParentID + "的列");
ExpandedSubBlockEnd.gif            }

InBlock.gif            DbHelper db 
= new DbHelper("Test");
InBlock.gif            
string sqlQuery = "update Child set ParentID = @ParentID,Name = @Name where ChildID=@ChildID";
InBlock.gif            DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), ChildID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@ParentID", CommonUtil.GetDbType("Child""ParentID"), ParentID);
InBlock.gif            db.AddInParameter(dbCommand, 
"@Name", CommonUtil.GetDbType("Child""Name"), Name);
InBlock.gif            
return db.ExecuteNonQuery(dbCommand);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------删除-----------#region -----------删除-----------
InBlock.gif
InBlock.gif        
public int Delete(Coree.Model.Child model)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Delete(model, null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal int Delete(Coree.Model.Child model,Transaction t)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int count = 0;
InBlock.gif            
if (t == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (t = new Transaction("Test"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DbHelper db 
= new DbHelper("Test");
InBlock.gif                        
string sqlQuery = "delete from Child where ChildID=@ChildID";
InBlock.gif                        DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                        db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), model.ChildID);
InBlock.gif                        count 
= db.ExecuteNonQuery(dbCommand, t);
InBlock.gif                        t.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        t.RollBack();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DbHelper db 
= new DbHelper("Test");
InBlock.gif                
string sqlQuery = "delete from Child where ChildID=@ChildID";
InBlock.gif                DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), model.ChildID);
InBlock.gif                count 
= db.ExecuteNonQuery(dbCommand, t);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return count;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Delete(object ChildID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Delete(ChildID, null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal int Delete(object ChildID, Transaction t)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int count = 0;
InBlock.gif            
if (t == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (t = new Transaction("Test"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DbHelper db 
= new DbHelper("Test");
InBlock.gif                        
string sqlQuery = "delete from Child where ChildID=@ChildID";
InBlock.gif                        DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                        db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), ChildID);
InBlock.gif                        count 
= db.ExecuteNonQuery(dbCommand, t);
InBlock.gif                        t.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        t.RollBack();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DbHelper db 
= new DbHelper("Test");
InBlock.gif                
string sqlQuery = "delete from Child where ChildID=@ChildID";
InBlock.gif                DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                db.AddInParameter(dbCommand, 
"@ChildID", CommonUtil.GetDbType("Child""ChildID"), ChildID);
InBlock.gif                count 
= db.ExecuteNonQuery(dbCommand, t);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return count;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Delete(IList ChildID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Delete(ChildID, null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal int Delete(IList ChildID, Transaction t)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int count = 0;
InBlock.gif            
if (t == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (t = new Transaction("Test"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        StringBuilder sb 
= new StringBuilder();
InBlock.gif                        
for (int i = 0; i < ChildID.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sb.Append(ChildID[i].ToString()).Append(
",");
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
string inStr = sb.ToString().Remove(sb.ToString().Length - 1);
InBlock.gif                        DbHelper db 
= new DbHelper("Test");
InBlock.gif                        
string sqlQuery = "declare @sql nvarchar(1000) set @sql='delete from Child where ChildID in ('+@ChildID+')' exec (@sql)";
InBlock.gif                        DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                        db.AddInParameter(dbCommand, 
"@ChildID", DbType.String, inStr);
InBlock.gif                        count 
= db.ExecuteNonQuery(dbCommand, t);
InBlock.gif                        t.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        t.RollBack();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringBuilder sb 
= new StringBuilder();
InBlock.gif                
for (int i = 0; i < ChildID.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sb.Append(ChildID[i].ToString()).Append(
",");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
string inStr = sb.ToString().Remove(sb.ToString().Length - 1);
InBlock.gif                DbHelper db 
= new DbHelper("Test");
InBlock.gif                
string sqlQuery = "declare @sql nvarchar(1000) set @sql='delete from Child where ChildID in ('+@ChildID+')' exec (@sql)";
InBlock.gif                DbCommand dbCommand 
= db.GetSqlStringCommand(sqlQuery);
InBlock.gif                db.AddInParameter(dbCommand, 
"@ChildID", DbType.String, inStr);
InBlock.gif                count 
= db.ExecuteNonQuery(dbCommand, t);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return count;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

DbTypeStructs.cs:
用来映射数据库字段的数据类型,在分析sql语句,添加参数的时候用到。

None.gif namespace  Coree.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
-----------Child-----------#region -----------Child-----------
InBlock.gif
InBlock.gif    
public partial struct Struct_child
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static System.Data.DbType childid = DbType.Int32;
InBlock.gif        
public static System.Data.DbType parentid = DbType.Guid;
InBlock.gif        
public static System.Data.DbType name = DbType.String;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
-----------Parent-----------#region -----------Parent-----------
InBlock.gif
InBlock.gif    
public partial struct Struct_parent
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static System.Data.DbType parentid = DbType.Guid;
InBlock.gif        
public static System.Data.DbType name = DbType.String;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ExpandedBlockEnd.gif}

CommonUtil.cs:
公共类,里面提供处理查询字符串、添加参数、创建分页查询语句的方法,供各个数据操作类使用。

None.gif namespace  Coree.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class CommonUtil
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------处理查询字符串-----------#region -----------处理查询字符串-----------
InBlock.gif
InBlock.gif        
public static string ProcessQuery(string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (String.IsNullOrEmpty(query))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return " ";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (!query.Contains("?"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return " " + query + " ";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string[] querys = query.Split('?');
InBlock.gif            
if (parameterValues == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                parameterValues 
= new object[] dot.gifnull };
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (querys.Length - 1 == parameterValues.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringBuilder sb 
= new StringBuilder(" ");
InBlock.gif                
for (int i = 0; i < querys.Length - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string q = querys[i];
InBlock.gif                    
if (parameterValues[i] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(q 
+ "@param" + i.ToString());
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (q.Contains("="))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(q.Replace(
"="" is null "));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (q.Contains("<>"|| q.Contains("!="))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Append(q.Replace(
"<>"" is not null ").Replace("!="" is not null "));
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
throw new Exception("下列语句中的谓词不与 null 组合查询:\r\n" + q);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return sb.Append(querys[querys.Length - 1]).Append(" ").ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("提供的参数与语句中的参数个数不符\r\n" + query);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------添加参数-----------#region -----------添加参数-----------
InBlock.gif
InBlock.gif        
public static void AddInParameter(DbHelper db, DbCommand dbCommand, string TableName, string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (parameterValues == null || String.IsNullOrEmpty(query)|| !query .Contains("?"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            StringBuilder sb 
= new StringBuilder(query.ToLower());
InBlock.gif            sb 
= sb.Replace("where""").Replace("like""").Replace("%""").Replace("<""").Replace(">""").Replace("=""");
InBlock.gif            
string[] querys = sb.ToString().Split('?');
InBlock.gif            
for (int i = 0; i < querys.Length - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string q = querys[i];
InBlock.gif                
if (parameterValues[i] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string ColumnName = q.Replace("and""").Replace("between""").Trim();
InBlock.gif                    
if (String.IsNullOrEmpty(ColumnName) && querys[i - 1].Contains("between"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ColumnName 
= querys[i - 1].Replace("and""").Replace("between""").Trim();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    db.AddInParameter(dbCommand, 
"@param" + i.ToString(), GetDbType(TableName, ColumnName), parameterValues[i]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------由列名获得列类型-----------#region -----------由列名获得列类型-----------
InBlock.gif
InBlock.gif        
public static System.Data.DbType GetDbType(string TableName, string ColumnName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ColumnName 
= ColumnName.ToLower();
InBlock.gif            
if (Type.GetType("Coree.DAL.Struct_" + TableName.ToLower()) == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("找不到表:" + TableName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (Type.GetType("Coree.DAL.Struct_" + TableName.ToLower()).GetField(ColumnName.ToLower()) == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("找不到表:" + TableName + " 中的字段:" + ColumnName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (System.Data.DbType)Type.GetType("Coree.DAL.Struct_" + TableName.ToLower()).GetField(ColumnName.ToLower()).GetValue(0);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-----------创建分页查询语句----------#region -----------创建分页查询语句----------
InBlock.gif
InBlock.gif        
public static string BuildPagerQuery(string TableName, string PK, string PKType, int StartIndex, int PageSize, string query, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            query 
= ProcessQuery(query, parameterValues);
InBlock.gif            StringBuilder sb 
= new StringBuilder();
InBlock.gif            sb.Append(
"set nocount on ");
InBlock.gif            sb.Append(
"declare @indextable table(ttid int identity(1,1),nid " + PKType + "");
InBlock.gif            sb.Append(
"declare @PageUpperBound int ");
InBlock.gif            sb.Append(
"set @PageUpperBound=@StartIndex+@PageSize-1 ");
InBlock.gif            sb.Append(
"set rowcount @PageUpperBound ");
InBlock.gif            sb.Append(
"insert into @indextable(nid) select " + PK + " from " + TableName + query + " ");
InBlock.gif            sb.Append(
"select * from " + TableName + " a ");
InBlock.gif            sb.Append(
"inner join @indextable t on t.nid=a." + PK + " ");
InBlock.gif            sb.Append(
"where t.ttid between @StartIndex and @PageUpperBound order by t.ttid ");
InBlock.gif            sb.Append(
"set nocount off ");
InBlock.gif            
return sb.ToString();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

DbHelper.cs:
顾名思义,就是访问数据库的帮助类,我前面的文章中也提到过。

就到此吧,有什么问题可以来问我,模板中有设计不当的地方还请多多指教!!或者技术上的东西我们也可以切磋切磋。

声明:
1、如果你把这个代码生成模板用到你的工程当中,遇到什么麻烦或引起什么损失的话,本人概不负责,我也没有义务为你提供技术支持。

2、这是一个自由的东东,大家可以任意修改和使用,如果大家有什么好的想法或建议也记得通知我哦,呵呵!

下载

转载于:https://www.cnblogs.com/leitwolf/archive/2007/07/27/833255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值