用ASP.NET写你自己的代码生成器(3)。
2024-07-10 13:01:58
供稿:网友
#region make form
/// <summary>
/// 生成表单项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_click(object sender, system.eventargs e)
{
if(this.dropdownlist1.selectedindex!=-1)
{
m_scon.connectionstring = "user id="+this.txtusername.text+
";password="+this.txtpassword.text+
";initial catalog="+this.dropdownlist3.selecteditem.text+
";data source="+this.txtservername.text;
sqlcommand m_scmd = new sqlcommand("sp_columns",m_scon);
m_scmd.commandtype = commandtype.storedprocedure;
sqlparameter myparm = m_scmd.parameters.add("@table_name",sqldbtype.varchar,100);
myparm.value = this.dropdownlist1.selecteditem.value;
m_scon.open();
sqldatareader m_sdr = m_scmd.executereader();
string colname = "";
string coltype = "";
while(m_sdr.read())
{
colname = m_sdr["column_name"].tostring();
coltype = m_sdr["type_name"].tostring();
}
m_sdr.close();
m_scon.close();
}
}
#endregion
#region make object
/// <summary>
/// 生成实体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_click(object sender, system.eventargs e)
{
if(this.dropdownlist1.selectedindex!=-1)
{
m_scon.connectionstring = "user id="+this.txtusername.text+
";password="+this.txtpassword.text+
";initial catalog="+this.dropdownlist3.selecteditem.text+
";data source="+this.txtservername.text;
sqlcommand m_scmd = new sqlcommand("sp_columns",m_scon);
m_scmd.commandtype = commandtype.storedprocedure;
sqlparameter myparm = m_scmd.parameters.add("@table_name",sqldbtype.varchar,100);
myparm.value = this.dropdownlist1.selecteditem.value;
m_scon.open();
sqldatareader m_sdr = m_scmd.executereader();
string tablename = this.dropdownlist1.selecteditem.value;
string m_layout = "";
m_layout = m_layout + "using system;/r/n";
m_layout = m_layout + "using system.data;/r/n" ;
m_layout = m_layout + "using system.data.sqlclient;/r/n";
m_layout = m_layout + "/r/n";
m_layout = m_layout + "namespace oceansoft/r/n";
m_layout = m_layout + "{/r/n";
m_layout = m_layout + bl(4) + "///<comment>/r/n";
m_layout = m_layout + bl(4) + "////r/n";
m_layout = m_layout + bl(4) + "///</comment>/r/n";
string colname = ""; //列名
string coltype = ""; //列的数据类型
string coldefine = ""; //列定义
string colproperty = ""; //列属性
while(m_sdr.read())
{
colname = m_sdr["column_name"].tostring();
coltype = gt(m_sdr["type_name"].tostring());
coldefine = coldefine + bl(8) + "private " + coltype + " m_" + colname + ";/r/n";
colproperty = colproperty + bl(8) + "/r/n";
colproperty = colproperty + bl(8) + "public " + coltype + " " + colname + "/r/n";
colproperty = colproperty + bl(8) + "{/r/n";
colproperty = colproperty + bl(12) + "get/r/n";
colproperty = colproperty + bl(12) + "{/r/n";
colproperty = colproperty + bl(16) + "return m_" + colname + " ;/r/n";
colproperty = colproperty + bl(12) + "}/r/n";
colproperty = colproperty + bl(12) + "set/r/n";
colproperty = colproperty + bl(12) + "{/r/n";
colproperty = colproperty + bl(16) + "m_" + colname + " = value ;/r/n";
colproperty = colproperty + bl(12) + "}/r/n";
colproperty = colproperty + bl(8) + "}/r/n";
}
m_sdr.close();
m_scon.close();
m_layout = m_layout + bl(4) + "public class " + tablename + "/r/n";
m_layout = m_layout + bl(4) + "{/r/n";
m_layout = m_layout + bl(4) + "/r/n";
m_layout = m_layout + coldefine; //定义
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "///构造函数/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "public " + tablename + "()/r/n"; //构造函数
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(8) + "}/r/n/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "///析构函数/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "~" + tablename + "()/r/n"; //析构函数
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(8) + "}/r/n";
m_layout = m_layout + colproperty; //实体属性
m_layout = m_layout + bl(4) + "}/r/n";
m_layout = m_layout + "}/r/n";
this.txtlayout.text = m_layout;
}
}
#endregion
#region test
private void button5_click(object sender, system.eventargs e)
{
//
}
#endregion
#region functions
private string bl(int values)
{
switch(values)
{
case 4:
return "/t";
case 8:
return "/t/t";
case 12:
return "/t/t/t";
case 16:
return "/t/t/t/t";
case 20:
return "/t/t/t/t/t";
}
return "";
}
private string gt(string values)
{
switch(values.toupper())
{
case "int":
return "int";
case "nvarchar":
return "string";
case "tinyint":
return "int";
case "int identity":
return "int";
case "bit":
return "int";
case "uniqueidentifier":
return "string";
case "datetime":
return "string";
case "varchar":
return "string";
default:
return "string";
}
}
#endregion
#region make object collection class
private void button6_click(object sender, system.eventargs e)
{
if(this.dropdownlist1.selectedindex!=-1)
{
string tablename = this.dropdownlist1.selecteditem.value;
string m_layout = "";
m_layout = m_layout + "using system;/r/n";
m_layout = m_layout + "using system.data;/r/n" ;
m_layout = m_layout + "using system.data.sqlclient;/r/n";
m_layout = m_layout + "using system.collections;/r/n";
m_layout = m_layout + "/r/n";
m_layout = m_layout + "namespace e3.pantheon.workflow.structure/r/n";
m_layout = m_layout + "{/r/n";
m_layout = m_layout + bl(4) + "///<comment>/r/n";
m_layout = m_layout + bl(4) + "///公司名称:苏州中软公司/r/n";
m_layout = m_layout + bl(4) + "///作者:/r/n";
m_layout = m_layout + bl(4) + "///创建日期:"+system.datetime.now.toshortdatestring()+"/r/n";
m_layout = m_layout + bl(4) + "///用途说明:/r/n";
m_layout = m_layout + bl(4) + "///修改记录:/r/n";
m_layout = m_layout + bl(4) + "///</comment>/r/n";
m_layout = m_layout + bl(4) + "public class " + tablename + "s : system.collections.collectionbase/r/n";
m_layout = m_layout + bl(4) + "{/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "///构造函数/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "public " + tablename + "s()/r/n"; //构造函数
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(12) + "//todo:在这里增加构造函数逻辑/r/n";
m_layout = m_layout + bl(8) + "}/r/n/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "///析构函数/r/n";
m_layout = m_layout + bl(8) + "///<summary>/r/n";
m_layout = m_layout + bl(8) + "~" + tablename + "s()/r/n"; //析构函数
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(12) + "//todo:在这里增加析构函数逻辑/r/n";
m_layout = m_layout + bl(8) + "}/r/n/r/n";
m_layout = m_layout + bl(8) + "public void remove(int index)/r/n"; //删除
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(12) + "if(index>counter-1||counter<0)/r/n";
m_layout = m_layout + bl(12) + "{}/r/n";
m_layout = m_layout + bl(12) + "else/r/n";
m_layout = m_layout + bl(12) + "{/r/n";
m_layout = m_layout + bl(16) + "list.removeat(index);/r/n";
m_layout = m_layout + bl(12) + "}/r/n";
m_layout = m_layout + bl(8) + "}/r/n/r/n";
m_layout = m_layout + bl(8) + "public void add("+tablename+" m_"+tablename+")/r/n"; //增加
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(12) + "list.add("+" m_"+tablename+");/r/n";
m_layout = m_layout + bl(8) + "}/r/n/r/n";
m_layout = m_layout + bl(8) + "public "+tablename+" this[int index]/r/n"; //集合的元素
m_layout = m_layout + bl(8) + "{/r/n";
m_layout = m_layout + bl(12) + "get/r/n";
m_layout = m_layout + bl(12) + "{/r/n";
m_layout = m_layout + bl(16) + "if(index <list.count)/r/n";
m_layout = m_layout + bl(16) + "{/r/n";
m_layout = m_layout + bl(20) + "return ("+tablename+")list[index];/r/n";
m_layout = m_layout + bl(16) + "}/r/n";
m_layout = m_layout + bl(16) + "else/r/n";
m_layout = m_layout + bl(16) + "{/r/n";
m_layout = m_layout + bl(16) + "return null;/r/n";
m_layout = m_layout + bl(16) + "}/r/n";
m_layout = m_layout + bl(12) + "}/r/n";
m_layout = m_layout + bl(12) + "set/r/n";
m_layout = m_layout + bl(12) + "{/r/n";
m_layout = m_layout + bl(16) + "if(iindex<list.count)/r/n";
m_layout = m_layout + bl(16) + "{/r/n";
m_layout = m_layout + bl(20) + "list[index]=value;/r/n";
m_layout = m_layout + bl(16) + "}/r/n";
m_layout = m_layout + bl(12) + "}/r/n";
m_layout = m_layout + bl(8) + "}/r/n";
m_layout = m_layout + bl(4) + "}/r/n"; //class
m_layout = m_layout + "}/r/n"; //namespace
this.txtlayout.text = m_layout;
}
}
#endregion