string conn = "Data Source={0};Initial Catalog={1};User Id={2};Password={3}"; //点击链接 private void btnLink_Click(object sender, EventArgs e) { SqlHelper.connstr = string.Format(conn, txtServer.Text.Trim(), "master", txtUserName.Text.Trim() , txtPWD.Text.Trim()); cboDatabase.DisplayMember = "name"; cboDatabase.DataSource = SqlHelper.ExecuteDataTable("select name from SYS.databases ", CommandType.Text); } //当选择数据库的索引发生改变时 private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e) { treeView1.Nodes.Clear(); string databasename = cboDatabase.Text; SqlHelper.connstr = string.Format(conn, txtServer.Text.Trim(), databasename, txtUserName.Text.Trim(), txtPWD.Text.Trim()); TreeNode partent = new TreeNode(); partent.Text = databasename; treeView1.Nodes.Add(partent); TreeNode sheet = new TreeNode(); sheet.Text = "数据表"; partent.Nodes.Add(sheet); DataTable dtsheet = SqlHelper.ExecuteDataTable("select TABLE_NAME ,TABLE_TYPE from INFORMATION_SCHEMA.TABLES where TABLE_TYPE ='BASE TABLE'", CommandType.Text); AddTreeNode(dtsheet, sheet); TreeNode view = new TreeNode(); view.Text = "视图"; partent.Nodes.Add(view); DataTable dtview = SqlHelper.ExecuteDataTable("select TABLE_NAME ,TABLE_TYPE from INFORMATION_SCHEMA.TABLES where TABLE_TYPE ='VIEW'", CommandType.Text); AddTreeNode(dtview, view); partent.ExpandAll(); ChangeText(); } void ChangeText() { txtBll.Text = cboDatabase.Text + "Bll"; txtDAL.Text = cboDatabase.Text + "DAL"; txtModel.Text = cboDatabase.Text + "Model"; txtDatabase.Text = "DBHelper"; } //把得到的每个数据表或视图加载到节点上 private void AddTreeNode(DataTable dt, TreeNode tn) { foreach (DataRow item in dt.Rows) { TreeNode node = new TreeNode(); node.Text = item["TABLE_NAME"].ToString(); tn.Nodes.Add(node); } } private void Form1_Load(object sender, EventArgs e) { rboModel.Checked = true; } //点击代码生成 private void btnRes_Click(object sender, EventArgs e) { tabControl1.SelectedIndex = ; if (rboModel.Checked) { //生成实体类 ) { txtCode .Text = CreateModel(); } else { MessageBox.Show("请选择数据表或视图"); } } if (rboDAL.Checked) { //生成数据访问层类 } if (rboBll.Checked) { //生成业务逻辑层类 } } //生成Model类 string CreateModel() { string sheetname = treeView1.SelectedNode.Text; StringBuilder sb = new StringBuilder(); DataTable dt = SqlHelper.ExecuteDataTable("select COLUMN_NAME ,DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME ='" + sheetname + "'", CommandType.Text); sb.AppendLine("using System;"); sb.AppendLine("namespace " + ToPascal(txtModel.Text) + ""); sb.AppendLine("{"); sb.AppendLine("\tpublic class " + ToPascal(sheetname) + ""); sb.AppendLine("\t{"); foreach (DataRow item in dt.Rows) { string columnname = item["COLUMN_NAME"].ToString(); string type = ChangeType(item["DATA_TYPE"].ToString()); sb.AppendLine("\t\tprivate " + type + " " + ToCamel(columnname) + ""); sb.AppendLine("\t\tpublic " + type + " " + ToPascal(columnname) + ""); sb.AppendLine("\t\t\tget{return " + ToCamel(columnname) + ";}"); sb.AppendLine("\t\t\tset{" + ToCamel(columnname) + "=value;}"); } sb.AppendLine("\t}"); sb.AppendLine("}"); return sb.ToString(); } //判断数据类型 string ChangeType(string str) { string res = "string"; switch (str) { case "int": res = "int"; break; case "datatime": res = "DataTime"; break; case "bit": res = "bool"; break; default: break; } return res; } //把一个字符串的首字母转换成小写 string ToCamel(string str) { , ).ToLower() + str.Substring(); } //把一个字符串的首字母转换成小写 string ToPascal(string str) { , ).ToUpper() + str.Substring(); }