浅谈代码生成器--附源码
好长时间又没写文章了,发觉没什么自己好写的,技术方面的文章cnblogs每天出的太多了,没事翻看了下自己以前搞过的东西发觉每次项目前期都要用代码生成器来生成一些现成的代码,就来谈谈这个代码生成器吧,我相信基本很多程序员都有写过,说实话就是为了更好的偷懒(呵呵)。第上我的界面,很粗糙,自己用嘛够了!
平台:win7 +vs 2010+sql 2008
源码地址:https://files.cnblogs.com/Royal_WH/代码生成器.rar
看一下解决方案管理器,很简单就二个项目:
Writer类就是用来生成代码的,那个SqlDataBase其实只提供一个数据库连接。
其实用到SqlDataBase里就两个函数:
/// <summary>
/// 获取数据源所有表信息
/// </summary>
/// <returns>返回DateTable对象</returns>
public DataTable GetTables(string sql)
{
Open();
DataTable schemaTable;
cmd = new SqlCommand(sql, cn);
sdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly);
schemaTable = sdr.GetSchemaTable();
Close();
return schemaTable;
}
/// <summary>
/// 获取数据库所有表名
/// </summary>
/// <returns></returns>
public DataTable Getcolumn()
{
Open();
DataTable dt;
dt = GetTable("select name from sysobjects where type ='U'");
Close();
return dt;
}
Writer类中的方法就比较多了:
编写实体类的代码,最简单:
/// <summary>
/// 编写实体类 -> Model
/// </summary>
/// <param name="namespaces"></param>
/// <param name="projectname"></param>
/// <param name="classname"></param>
/// <param name="name"></param>
public string GetModelCode(DataTable dt, string namespaces, string projectname, string classname, string name)
{
try
{
dt = ClearHeard(dt);
StringBuilder str = new StringBuilder();
str.Append("using System;" + Environment.NewLine + "" +
Environment.NewLine + "");
str.Append("namespace " + namespaces + "." + projectname);
str.Append("" + Environment.NewLine + "{" + Environment.NewLine + "");
str.Append(" /// <summary>" + Environment.NewLine
+ " /// 实体类 " + classname + " " + Environment.NewLine
+ " /// 编写者:" + name + " " + Environment.NewLine+ " /// 日期:" + DateTime.Now + "" + Environment.NewLine+ " /// </summary>" + Environment.NewLine + "");
str.Append(" public class " + classname+ "" + Environment.NewLine + "" + " {" + Environment.NewLine + "");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (conver(dt.Rows[i][2].ToString()) == "string")
{
str.Append(" private "+ conver(dt.Rows[i][2].ToString()) + " " + dt.Rows[i][0].ToString().ToLower()+ "=string.Empty" + ";" + Environment.NewLine + "");
}
else if (conver(dt.Rows[i][2].ToString()) == "Guid")
{
str.Append(" private "+ conver(dt.Rows[i][2].ToString()) + " " + dt.Rows[i][0].ToString().ToLower()+ "=Guid.Empty" + ";" + Environment.NewLine + "");
}
else
{
str.Append(" private "+ conver(dt.Rows[i][2].ToString()) + " " + dt.Rows[i][0].ToString().ToLower()+ "=" + conver(dt.Rows[i][2].ToString()) + ".MaxValue;" + Environment.NewLine + "");
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
str.Append("" + Environment.NewLine+ " /// <summary>" + Environment.NewLine + " /// "+ dt.Rows[i][0].ToString() + "" + Environment.NewLine + " /// </summary>"+ Environment.NewLine + "");
str.Append(" public "+ conver(dt.Rows[i][2].ToString()) + " " + dt.Rows[i][0].ToString() + ""+ Environment.NewLine + "");
str.Append(" {" + Environment.NewLine+ " set{" + dt.Rows[i][0].ToString().ToLower() + "=value;}"+ Environment.NewLine + "");
str.Append(" get{ return "+ dt.Rows[i][0].ToString().ToLower() + ";}" + Environment.NewLine+ " }" + Environment.NewLine + "");
}
str.Append(" }" + Environment.NewLine+ "}" + Environment.NewLine + "" + Environment.NewLine + "");
return str.ToString();
}
catch { }
return "出错了";
}
那个dt里的数据是我们从数据库里读出来的表头的信息和表结构的所有信息,里面有很多信息但我们只要一部分,所以我们把其它列的信息去掉:
/// <summary>
/// 清除数据库表头
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public DataTable ClearHeard(DataTable dt)
{
dt.Columns.Remove("NumericScale");
dt.Columns.RemoveAt(1);
dt.Columns.Remove("NumericPrecision");
dt.Columns.Remove("IsUnique");
dt.Columns.Remove("IsKey");
dt.Columns.Remove("BaseServerName");
dt.Columns.Remove("BaseCatalogName");
dt.Columns.Remove("BaseSchemaName");
dt.Columns.Remove("BaseTableName");
dt.Columns.Remove("AllowDBNull");
dt.Columns.Remove("ProviderType");
dt.Columns.Remove("IsAliased");
dt.Columns.Remove("IsExpression");
dt.Columns.Remove("IsIdentity");
dt.Columns.Remove("IsAutoIncrement");
dt.Columns.Remove("IsRowVersion");
dt.Columns.Remove("IsHidden");
dt.Columns.Remove("IsReadOnly");
dt.Columns.Remove("ProviderSpecificDataType");
dt.Columns.Remove("XmlSchemaCollectionDatabase");
dt.Columns.Remove("XmlSchemaCollectionOwningSchema");
dt.Columns.Remove("XmlSchemaCollectionName");
dt.Columns.Remove("UdtAssemblyQualifiedName");
dt.Columns.Remove("IsLong");
dt.Columns.Remove("DataType");
dt.Columns.Remove("NonverSionedProviderType");
dt.Columns.Remove("BaseColumnName");
return dt;
}
我们来看下从数据库读取出来的dt的信息:
第一行是我表里所有属性,然后是列序,大小等等。上面的ClearHeard函数就是把我们不用的去掉。
我们看一下生成好的实体类的代码:
using System;
namespace Thunder.
{
/// <summary>
/// 实体类 ProjectModel
/// 编写者:Royal_WH
/// 日期:2011-02-21 15:10:25
/// </summary>
public class ProjectModel
{
private Guid projectid=Guid.Empty;
private string projectno=string.Empty;
private string constructname=string.Empty;
private string address=string.Empty;
private string contact=string.Empty;
private string tel=string.Empty;
private string projectname=string.Empty;
private string projectaddress=string.Empty;
private double area=double.MaxValue;
private int buildings=int.MaxValue;
private string projectnature=string.Empty;
private string regional=string.Empty;
private decimal bepay=decimal.MaxValue;
private decimal reducepay=decimal.MaxValue;
private string reducenumber=string.Empty;
private decimal betopay=decimal.MaxValue;
private decimal topay=decimal.MaxValue;
private string designunitname=string.Empty;
private string dcontact=string.Empty;
private string dtel=string.Empty;
private string constructunitname=string.Empty;
private string ccontact=string.Empty;
private string ctel=string.Empty;
private string supervisionunitname=string.Empty;
private string scontact=string.Empty;
private string stel=string.Empty;
/// <summary>
/// ProjectID
/// </summary>
public Guid ProjectID
{
set{projectid=value;}
get{ return projectid;}
}
/// <summary>
/// ProjectNo
/// </summary>
public string ProjectNo
{
set{projectno=value;}
get{ return projectno;}
}
/// <summary>
/// ConstructName
/// </summary>
public string ConstructName
{
set{constructname=value;}
get{ return constructname;}
}
/// <summary>
/// Address
/// </summary>
public string Address
{
set{address=value;}
get{ return address;}
}
/// <summary>
/// Contact
/// </summary>
public string Contact
{
set{contact=value;}
get{ return contact;}
}
/// <summary>
/// Tel
/// </summary>
public string Tel
{
set{tel=value;}
get{ return tel;}
}
/// <summary>
/// ProjectName
/// </summary>
public string ProjectName
{
set{projectname=value;}
get{ return projectname;}
}
/// <summary>
/// ProjectAddress
/// </summary>
public string ProjectAddress
{
set{projectaddress=value;}
get{ return projectaddress;}
}
/// <summary>
/// Area
/// </summary>
public double Area
{
set{area=value;}
get{ return area;}
}
/// <summary>
/// Buildings
/// </summary>
public int Buildings
{
set{buildings=value;}
get{ return buildings;}
}
/// <summary>
/// ProjectNature
/// </summary>
public string ProjectNature
{
set{projectnature=value;}
get{ return projectnature;}
}
/// <summary>
/// Regional
/// </summary>
public string Regional
{
set{regional=value;}
get{ return regional;}
}
/// <summary>
/// BePay
/// </summary>
public decimal BePay
{
set{bepay=value;}
get{ return bepay;}
}
/// <summary>
/// ReducePay
/// </summary>
public decimal ReducePay
{
set{reducepay=value;}
get{ return reducepay;}
}
/// <summary>
/// ReduceNumber
/// </summary>
public string ReduceNumber
{
set{reducenumber=value;}
get{ return reducenumber;}
}
/// <summary>
/// BeToPay
/// </summary>
public decimal BeToPay
{
set{betopay=value;}
get{ return betopay;}
}
/// <summary>
/// ToPay
/// </summary>
public decimal ToPay
{
set{topay=value;}
get{ return topay;}
}
/// <summary>
/// DesignUnitName
/// </summary>
public string DesignUnitName
{
set{designunitname=value;}
get{ return designunitname;}
}
/// <summary>
/// DContact
/// </summary>
public string DContact
{
set{dcontact=value;}
get{ return dcontact;}
}
/// <summary>
/// DTel
/// </summary>
public string DTel
{
set{dtel=value;}
get{ return dtel;}
}
/// <summary>
/// ConstructUnitName
/// </summary>
public string ConstructUnitName
{
set{constructunitname=value;}
get{ return constructunitname;}
}
/// <summary>
/// CContact
/// </summary>
public string CContact
{
set{ccontact=value;}
get{ return ccontact;}
}
/// <summary>
/// CTel
/// </summary>
public string CTel
{
set{ctel=value;}
get{ return ctel;}
}
/// <summary>
/// SupervisionUnitName
/// </summary>
public string SupervisionUnitName
{
set{supervisionunitname=value;}
get{ return supervisionunitname;}
}
/// <summary>
/// SContact
/// </summary>
public string SContact
{
set{scontact=value;}
get{ return scontact;}
}
/// <summary>
/// STel
/// </summary>
public string STel
{
set{stel=value;}
get{ return stel;}
}
}
}
同理我们可以生成我们所要的数据层的代码,数据层代码就不在这展示了,要不文章搞的太长又不少博友看不下去了!
这里讲一下数据层,因为数据层要配合数据库操作类来使用的!我是用静态类来写数据库操作的,可能和有些博友不是用的静态类,那就要在数据层声明一个数据操作类的实例了。
先看下我的数据层代码吧:
/// <summary> /// 数据层业务类 -> Access /// </summary> /// <param name="namespaces"></param> /// <param name="projectname"></param> /// <param name="classname"></param> /// <param name="name"></param> public string GetDALCode(DataTable dt, string namespaces, string projectname, string classname, string name) { dt = ClearHeard(dt); StringBuilder str = new StringBuilder(); #region 数据类头信息 1 str.Append("using System;" + Environment.NewLine + ""); str.Append("using System.Text;" + Environment.NewLine + ""); str.Append("using System.Data;" + Environment.NewLine + ""); str.Append("using System.Data.SqlClient;" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append("using Angel.Data;" + Environment.NewLine + ""); str.Append("using " + namespaces + ".Model;" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append("namespace " + namespaces + "." + projectname); str.Append("" + Environment.NewLine + "{" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine + " /// " + classname
+ " 数据库访问类" + Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" public class " + classname + "Access" + Environment.NewLine + "" + " {" + Environment.NewLine); #endregion #region 存在性判断 2 str.Append(" /// <summary>" + Environment.NewLine
+ " /// 根据指定的 " + dt.Rows[0][0].ToString() + " 判断数据记录是否存在" + Environment.NewLine
+ " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"" + dt.Rows[0][0].ToString()
+ "\">" + dt.Rows[0][0].ToString() + "</param>" + Environment.NewLine + ""); str.Append(" /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public bool Exists(Guid " + dt.Rows[0][0].ToString() + ")" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return this.Exists(\"" + dt.Rows[0][0].ToString()
+ "=\" + " + dt.Rows[0][0].ToString() + ".ToString());" + Environment.NewLine + ""); str.Append(" } // Exists" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine
+ " /// 根据指定的存在性判断条件判断相应的数据集是否存在" + Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"condition\">存在性判断条件</param>" + Environment.NewLine + ""); str.Append(" /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public bool Exists(string condition)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" StringBuilder sql = new StringBuilder();" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" sql.Append(\"SELECT Count(*) FROM " + classname + "\");" + Environment.NewLine + ""); str.Append(" if (condition.Trim() != \"\")" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" sql.Append(\" WHERE \" + condition);"
+ Environment.NewLine + " }" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" if (Convert.ToInt32(DatabaseHelper.ExecuteNonQuery(sql.ToString())) > 0)"
+ Environment.NewLine + " {" + Environment.NewLine); str.Append(" return true;" + Environment.NewLine); str.Append(" }" + Environment.NewLine); str.Append(" else" + Environment.NewLine); str.Append(" {" + Environment.NewLine); str.Append(" return false;" + Environment.NewLine); str.Append(" }" + Environment.NewLine); str.Append(" } //Exists" + Environment.NewLine + Environment.NewLine + Environment.NewLine); #endregion #region 增加一条记录 3 str.Append(" /// <summary>" + Environment.NewLine
+ " /// 根据指定的 " + classname + " 实体插入一条记录" + Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"model\">" + classname + " 实体</param>" + Environment.NewLine + ""); str.Append(" /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public string Insert(" + classname + "Model model)" + "" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" StringBuilder sql = new StringBuilder();" + Environment.NewLine + ""); str.Append(" StringBuilder fields = new StringBuilder();" + Environment.NewLine + ""); str.Append(" StringBuilder values = new StringBuilder();" + Environment.NewLine + "" + Environment.NewLine + ""); for (int i = 0; i < dt.Rows.Count - 1; i++) { if (conver(dt.Rows[i + 1][2].ToString()) == "string") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString()
+ " != " + "string.Empty" + ")" + Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " fields.Append(\"" + dt.Rows[i + 1][0].ToString()
+ ",\");" + Environment.NewLine + ""); str.Append(" values.Append(\"'\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString()
+ \"',\");" + Environment.NewLine + " }" + Environment.NewLine + "" + Environment.NewLine + ""); } else if (conver(dt.Rows[i + 1][2].ToString()) == "Guid") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + "Guid.Empty" + ")"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " fields.Append(\"" + dt.Rows[i + 1][0].ToString() + ",\");"
+ Environment.NewLine + ""); str.Append(" values.Append(\"'\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \"',\");"
+ Environment.NewLine + " }" + Environment.NewLine + "" + Environment.NewLine + ""); } else if (conver(dt.Rows[i + 1][2].ToString()) == "DateTime") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + "DateTime.MaxValue" + ")"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " fields.Append(\"" + dt.Rows[i + 1][0].ToString() + ",\");"
+ Environment.NewLine + ""); str.Append(" values.Append(\"'\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \"',\");"
+ Environment.NewLine + " }" + Environment.NewLine + "" + Environment.NewLine + ""); } else { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + conver(dt.Rows[i + 1][2].ToString())
+ ".MaxValue)" + Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " fields.Append(\"" + dt.Rows[i + 1][0].ToString() + ",\");"
+ Environment.NewLine + ""); str.Append(" values.Append(\"\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \",\");"
+ Environment.NewLine + " }" + Environment.NewLine + "" + Environment.NewLine + ""); } } str.Append(" sql.Append(\"INSERT INTO " + classname + "(\");" + Environment.NewLine + ""); str.Append(" sql.Append(fields.ToString().Remove(fields.Length - 1));" + Environment.NewLine + ""); str.Append(" sql.Append(\")\");" + Environment.NewLine + ""); str.Append(" sql.Append(\" VALUES (\");" + Environment.NewLine + ""); str.Append(" sql.Append(values.ToString().Remove(values.Length - 1));" + Environment.NewLine + ""); str.Append(" sql.Append(\"); SELECT @@Identity\");" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteScalar(sql.ToString()).ToString();"); str.Append("" + Environment.NewLine + " } // Insert" + Environment.NewLine + "" + Environment.NewLine + ""
+ Environment.NewLine + ""); #endregion #region 更新一条记录 4 str.Append(" /// <summary>" + Environment.NewLine + " /// 根据指定的 " + classname + " 实体更新一条记录"
+ Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"model\">" + classname + " 实体</param>" + Environment.NewLine + ""); str.Append(" /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public void Update(" + classname + "Model model)" + "" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" StringBuilder sql = new StringBuilder();" + Environment.NewLine + ""); str.Append(" sql.Append(\"UPDATE " + classname + " SET " + "\");" + Environment.NewLine + "" + Environment.NewLine + ""); for (int i = 0; i < dt.Rows.Count - 1; i++) { if (conver(dt.Rows[i + 1][2].ToString()) == "string") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + "string.Empty" + ")"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " sql.Append(\"" + dt.Rows[i + 1][0].ToString()
+ "='\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \"',\");" + Environment.NewLine + " }"
+ Environment.NewLine + "" + Environment.NewLine + ""); } else if (conver(dt.Rows[i + 1][2].ToString()) == "Guid") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + "Guid.MaxValue" + ")"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " sql.Append(\"" + dt.Rows[i + 1][0].ToString()
+ "='\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \"',\");" + Environment.NewLine + " }"
+ Environment.NewLine + "" + Environment.NewLine + ""); } else if (conver(dt.Rows[i + 1][2].ToString()) == "DateTime") { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + "DateTime.MaxValue" + ")"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " sql.Append(\"" + dt.Rows[i + 1][0].ToString()
+ "='\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \"',\");" + Environment.NewLine + " }"
+ Environment.NewLine + "" + Environment.NewLine + ""); } else { str.Append(" if (model." + dt.Rows[i + 1][0].ToString() + " != " + conver(dt.Rows[i + 1][2].ToString()) + ".MaxValue)"
+ Environment.NewLine + " {"); str.Append("" + Environment.NewLine + " sql.Append(\"" + dt.Rows[i + 1][0].ToString()
+ "=\" + model." + dt.Rows[i + 1][0].ToString() + ".ToString() + \",\");" + Environment.NewLine + " }"
+ Environment.NewLine + "" + Environment.NewLine + ""); } } str.Append(" sql.Remove(sql.ToString().LastIndexOf(\",\"), 1);" + Environment.NewLine + ""); str.Append(" sql.Append(\" WHERE " + dt.Rows[0][0].ToString() + "=\" + model." + dt.Rows[0][0].ToString()
+ ".ToString());" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" DatabaseHelper.ExecuteNonQuery(sql.ToString());" + Environment.NewLine + " } // Update"
+ Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion #region 删除一条记录 5 str.Append(" /// <summary>" + Environment.NewLine + " /// 根据指定的 " + dt.Rows[0][0].ToString()
+ " 删除对应的记录" + Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"" + dt.Rows[0][0].ToString() + "\">" + dt.Rows[0][0].ToString()
+ "</param>" + Environment.NewLine + ""); str.Append(" public int Delete(Guid " + dt.Rows[0][0].ToString() + ")" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return this.Delete(\"" + dt.Rows[0][0].ToString() + "=\" + " + dt.Rows[0][0].ToString()
+ ".ToString());" + Environment.NewLine + ""); str.Append(" } // Delete" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine + " /// 根据指定的删除条件删除对应的记录"
+ Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" /// <param name=\"condition\">删除条件</param>" + Environment.NewLine + ""); str.Append(" public int Delete(string condition)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" StringBuilder sql = new StringBuilder();" + Environment.NewLine
+ " sql.Append(\"DELETE FROM " + classname + "\");" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" if (condition.Trim() != \"\")" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" sql.Append(\" WHERE \" + condition);" + Environment.NewLine + ""); str.Append(" }" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" return Convert.ToInt32(DatabaseHelper.ExecuteNonQuery(sql.ToString()));" + Environment.NewLine + ""); str.Append(" } // Delete" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion #region 根据T-SQL语句获取数据集 6 // 方法 1 str.Append(" /// <summary>" + Environment.NewLine + " /// 获取 " + classname + " 数据集"
+ Environment.NewLine + " /// </summary>" + Environment.NewLine + ""); str.Append(" public DataSet GetDataSet()" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return this.GetDataSet(0, \"\", \"\");" + Environment.NewLine + ""); str.Append(" } // GetDataSet" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); // 方法 2 str.Append(" /// <summary>" + Environment.NewLine + " /// 根据指定的数据行数、查询条件及排序方式获取相应的数据记录"
+ Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"top\">数据行数</param>"
+ Environment.NewLine + " /// <param name=\"condition\">查询条件</param>" + Environment.NewLine
+ " /// <param name=\"order\">排序方式</param>" + Environment.NewLine + " /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public DataSet GetDataSet(int top, string condition, string order)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" StringBuilder sql = new StringBuilder();" + Environment.NewLine + ""); str.Append(" sql.Append(\"SELECT \");" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" if (top > 0)" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" sql.Append(\"TOP \" + top.ToString());" + Environment.NewLine + " }" + Environment.NewLine + ""); str.Append(" sql.Append(\"* FROM " + classname + "\");" + Environment.NewLine + ""); str.Append(" if (condition.Trim() != \"\")" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" sql.Append(\" WHERE \" + condition);" + Environment.NewLine + " }" + Environment.NewLine + ""); str.Append(" if (order.Trim() != \"\")" + Environment.NewLine + " {" + Environment.NewLine + ""); str.Append(" sql.Append(\" ORDER BY \" + order);" + Environment.NewLine + " }"
+ Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteDataset(sql.ToString(), \"Table\");" + Environment.NewLine
+ " } // GetDataSet " + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); // 方法 3 str.Append(" /// <summary>" + Environment.NewLine + " /// 获取 " + classname + " 数据集" + Environment.NewLine
+ " /// </summary>" + Environment.NewLine + " /// <param name=\"sql\">自定义 SQL 语句</param>" + Environment.NewLine); str.Append(" public DataSet GetDataSet(string sql)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteDataset(sql.ToString(), \"Table\");" + Environment.NewLine + ""); str.Append(" } // GetDataSet" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion #region 更新表到数据库 7 str.Append(" /// <summary>" + Environment.NewLine + " /// 更新表到数据库" + Environment.NewLine); str.Append(" /// </summary>" + Environment.NewLine); str.Append(" /// <param name=\"dt\">DataTable对象</param>" + Environment.NewLine); str.Append(" /// <param name=\"sql\">SQL语句</param>" + Environment.NewLine); str.Append(" public void UpdateGridView(DataTable dt,string sql)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" DatabaseHelper.FillDatabase(dt,sql);" + Environment.NewLine + ""); str.Append(" } //UpdateGridView" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion #region 执行存储过程返回 DataSet 8 str.Append(" /// <summary>" + Environment.NewLine + " /// 执行存储过程返回 DataSet(需要参数)" + Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"spName\">存储过程名称</param>" + Environment.NewLine
+ " /// <param name=\"param\">参数</param>" + Environment.NewLine + " /// <param name=\"tableName\">表名</param>" + Environment.NewLine
+ " /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public DataSet ExecuteProc(string spName, SqlParameter[] param,string tableName)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteDataset(spName, tableName, param);" + Environment.NewLine + ""); str.Append(" } // ExecuteProc" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine + " /// 执行存储过程返回 DataSet(不需要参数)" + Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"spName\">存储过程名称</param>" + Environment.NewLine + " /// <param name=\"tableName\">表名</param>" + Environment.NewLine
+ " /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public DataSet ExecuteProc(string spName, string tableName)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteDataset(spName, tableName, CommandType.StoredProcedure);"
+ Environment.NewLine + ""); str.Append(" } // ExecuteProc" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine + " /// 执行存储过程返回受影响行数(不带参数)" + Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"spName\">存储过程名称</param>" + Environment.NewLine + " /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public int ExecuteProc(string spName)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return Convert.ToInt32(DatabaseHelper.ExecuteNonQuery(spName, CommandType.StoredProcedure));"
+ Environment.NewLine + ""); str.Append(" } // ExecuteProc" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); str.Append(" /// <summary>" + Environment.NewLine + " /// 执行存储过程返回受影响行数(带参数)" + Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"spName\">存储过程名称</param>"
+ Environment.NewLine + " /// <param name=\"param\">存储过程参数</param>" + Environment.NewLine + " /// <returns></returns>" + Environment.NewLine + ""); str.Append(" public int ExecuteProc(string spName, SqlParameter[] param)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return Convert.ToInt32(DatabaseHelper.ExecuteNonQuery(spName,param));" + Environment.NewLine + ""); str.Append(" } // ExecuteProc" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion #region 根据 SQL 语句返回第一行第一列值 9 str.Append(" /// <summary>" + Environment.NewLine + " /// 根据 SQL 语句返回第一行第一列值" + Environment.NewLine + " /// </summary>" + Environment.NewLine + " /// <param name=\"sql\">自定义 SQL 语句</param>" + Environment.NewLine + " /// <returns></returns>"
+ Environment.NewLine + ""); str.Append(" public string GetFirstNo(string sql)" + Environment.NewLine + ""); str.Append(" {" + Environment.NewLine + ""); str.Append(" return DatabaseHelper.ExecuteScalar(sql).ToString();" + Environment.NewLine + ""); str.Append(" } // GetFirstNo" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""); #endregion str.Append(" }" + Environment.NewLine + "}" + Environment.NewLine + ""); return str.ToString(); }
源码我等会会传上来!