C#自动代码生成器
- 格式:docx
- 大小:54.59 KB
- 文档页数:20
C#自动代码生成器
自动生成数据表和实体类
namespace SQLApplicationAutoGeneratiTool
{
public partial class Form1 : Form
{
string[] strArray = { "int", "string", "decimal", "bool", "char", "object", "double", "float",
"DateTime" };
String path = @"..\..\..\类文件\";
public Form1()
{
InitializeComponent();
}
private void 新增文本框ToolStripMenuItem_Click(object sender, EventArgs e)
{
ShengChengKongJian();
}
private void ShengChengKongJian()
{
ComboBox cobm = new ComboBox();
for (int i = 0; i < strArray.Count(); i++)
{
cobm.Items.Add(strArray[i]);
}
TextBox textbox = new TextBox(); this.flowLayoutPanel1.Controls.Add(cobm); this.flowLayoutPanel2.Controls.Add(textbox);
}
StringBuilder stbd = null;
List strbldList = new List();
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text.Trim() == "")
{
MessageBox.Show("类名不能为空");
return;
}
stbd = new StringBuilder("using System;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Collections.Generic;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Linq;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Text;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Data;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Data.SqlClient;");
strbldList.Add(stbd);
stbd = new StringBuilder("using System.Windows.Forms;");
strbldList.Add(stbd);
stbd = new StringBuilder("namespace HospitalInformationManagmentSystem");
strbldList.Add(stbd);
stbd = new StringBuilder("{");
strbldList.Add(stbd);
stbd = new StringBuilder(" /* 作者:大理大学软件教研室杜老师");
strbldList.Add(stbd);
stbd = new StringBuilder(" * 日期:" + dateTimePicker1.Text.Trim());
strbldList.Add(stbd);
stbd = new StringBuilder(" * 版权:大理大学");
strbldList.Add(stbd);
stbd = new StringBuilder(" * 你可以免费使用或修改以下代码,但请保留版权信息,否则将追究相关责任");
strbldList.Add(stbd);
stbd = new StringBuilder(" */");
strbldList.Add(stbd);
stbd = new StringBuilder(" public class ");
stbd.Append(comboBox1.Text.Trim());
strbldList.Add(stbd); stbd = new StringBuilder(" {");
strbldList.Add(stbd);
stbd = new StringBuilder(" #region 属性");
strbldList.Add(stbd);
for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
{
StringBuilder std = new StringBuilder(" private ");
std.Append((flowLayoutPanel1.Controls[i]).Text.Trim());
std.Append(" ");
std.Append(flowLayoutPanel2.Controls[i].Text.Trim());
std.Append(";");
strbldList.Add(std);
}
string shuX = "";
string st = "";
string s1 = "";
for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
{
shuX = " public " + flowLayoutPanel1.Controls[i].Text.Trim() + " _" +
flowLayoutPanel2.Controls[i].Text.Trim();
st = " set {" + flowLayoutPanel2.Controls[i].Text.Trim() + "= value; }";
s1 = " get { return " + flowLayoutPanel2.Controls[i].Text.Trim() +
";}";
stbd = new StringBuilder(shuX);
strbldList.Add(stbd);
stbd = new StringBuilder(" {");
strbldList.Add(stbd);
stbd = new StringBuilder(st);
strbldList.Add(stbd);
stbd = new StringBuilder(s1);
strbldList.Add(stbd);
stbd = new StringBuilder(" }");
strbldList.Add(stbd);
}
stbd = new StringBuilder(" #endregion 属性");
strbldList.Add(stbd);
stbd = new StringBuilder(" HIMSSqlConnection sqlcon = new HIMSSqlConnection();");
strbldList.Add(stbd);
stbd = new StringBuilder(" /// ");
strbldList.Add(stbd);
stbd = new StringBuilder(" /// 增加一条记录");
strbldList.Add(stbd);
stbd = new StringBuilder(" /// ");
strbldList.Add(stbd); stbd = new StringBuilder(" public bool Add()");
strbldList.Add(stbd);
stbd = new StringBuilder(" {");
strbldList.Add(stbd);
stbd = new StringBuilder(" int i=0;");
strbldList.Add(stbd);
stbd = new StringBuilder(" using (SqlConnection con =
sqlcon.DataBaseConnection())");
strbldList.Add(stbd);
stbd = new StringBuilder(" {");
strbldList.Add(stbd);
stbd = new StringBuilder(" if (con.State == ConnectionState.Closed)");
strbldList.Add(stbd);
stbd = new StringBuilder(" {");
strbldList.Add(stbd);
stbd = new StringBuilder(" con.Open();");
strbldList.Add(stbd);
stbd = new StringBuilder(" }");
strbldList.Add(stbd);
stbd = new StringBuilder(" try");
strbldList.Add(stbd);