懒人一个,所以不想做重复攻,自己一个小时写了个实体类生成工具。
没有美化,功能够用。(限SQLSERVER数据库)
不多说,上图上代码。
没什么高端的代码,就是拼接字符串而已~。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace 实体类代码生成工具 { public partial class Login : Form { public Login() { InitializeComponent(); } private void BtnLogin_Click(object sender, EventArgs e) { string slm = txtSLM.Text.Trim(); string user = txtName.Text.Trim(); string pwd = txtPwd.Text.Trim(); string sjk=txtSJK.Text.Trim(); SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(); sb.DataSource = slm; sb.InitialCatalog =sjk; sb.UserID = user; sb.Password = pwd; SqlConnection con = new SqlConnection(sb.ToString()); try { con.Open(); Main m = new Main(sb); m.Show(); } catch (SqlException EX) { MessageBox.Show(EX.Message); } finally { con.Close(); con.Dispose(); } } private void BtnClose_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); } } }
然后是主窗体
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace 实体类代码生成工具 { public partial class Main : Form { SqlConnection con; SqlDataAdapter da; DataTable dt; string sqlstr; StringBuilder str = new StringBuilder(); public Main(SqlConnectionStringBuilder builder) { sqlstr = builder.ToString(); InitializeComponent(); } private void Main_Load(object sender, EventArgs e) { dt = GetDataTableA("select name from sysobjects where xtype='U' order by name "); foreach (DataRow dr in dt.Select()) { listBox1.Items.Add(dr[0]); } } /// <summary> /// 离线模式填充datatable并返回,数据少可以用此方法 /// </summary> /// <param name="sql"></param> /// <returns></returns> public DataTable GetDataTableA(string sql) { using (con = new SqlConnection(sqlstr)) { da = new SqlDataAdapter(sql, con); da.MissingSchemaAction = MissingSchemaAction.AddWithKey; dt = new DataTable(); da.Fill(dt); return dt; } } private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = ""; if (String.IsNullOrEmpty(txtSpace.Text+txtclass.Text)) { MessageBox.Show("NameSpace必须填写"); } else { str.Append("using System;" + Environment.NewLine); str.Append("using System.Collections.Generic;" + Environment.NewLine); str.Append("using System.ComponentModel;"+Environment.NewLine); str.Append("using System.Data;"+Environment.NewLine); str.Append("using System.Text;"+Environment.NewLine); str.Append("namespace " + txtSpace.Text + " " + Environment.NewLine); str.Append("{" + Environment.NewLine); str.Append(" public class "+txtclass.Text +Environment.NewLine); str.Append(" {" + Environment.NewLine); dt = GetDataTableA("select * from " + listBox1.Text + " where 1=2 "); foreach (DataColumn d in dt.Columns) { if (d.AllowDBNull) { str.Append(" public " + d.DataType.ToString() + "? " + d.ColumnName +"{get;set}" + Environment.NewLine); } else { str.Append(" public " + d.DataType.ToString() + " " + d.ColumnName + "{get;set}" + Environment.NewLine); } } str.Append(" }" + Environment.NewLine); str.Append("}" + Environment.NewLine); richTextBox1.Text = str.ToString(); } } private void button2_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text,str.ToString() );//复制内容到剪切板 } } }
OK..改一改字符串啥的。就能跑了。