void SetTreeNodeDatabaseMenu(TreeView tree, TreeViewItem item) { TreeNodeDatabase treenodedatabase = (TreeNodeDatabase)tree.SelectedItem; ISqlControl sqlcontrol = treenodedatabase.sqlcontrol; ContextMenu cm = new ContextMenu(); MenuItem menu_generate = new MenuItem(); menu_generate.Header = "生成Models"; menu_generate.Click += delegate(object obj, RoutedEventArgs earg) { DataTable tables = sqlcontrol.GetTableList(treenodedatabase.connectionstring, treenodedatabase.database); text_content.Text = sqlcontrol.GenerateModels(tables); text_content2.Text = sqlcontrol.GenerateModels2(tables); }; cm.Items.Add(menu_generate); MenuItem menu_generategetlist = new MenuItem(); menu_generategetlist.Header = "生成GetModelList"; menu_generategetlist.Click += delegate(object obj, RoutedEventArgs earg) { DataTable tables = sqlcontrol.GetTableList(treenodedatabase.connectionstring, treenodedatabase.database); DataTable columns = sqlcontrol.GetColumnList(treenodedatabase.connectionstring, treenodedatabase.database); text_content.Text = sqlcontrol.GenerateGetModelList(tables, columns); text_content2.Text = text_content.Text; }; cm.Items.Add(menu_generategetlist); cm.Items.Add(new Separator()); MenuItem menu_generatefile = new MenuItem(); menu_generatefile.Header = "生成Models类库"; menu_generatefile.Click += delegate(object obj, RoutedEventArgs earg) { GenerateFile dialog = new GenerateFile(); dialog.Tag = treenodedatabase; dialog.Owner = this; dialog.ShowDialog(); }; cm.Items.Add(menu_generatefile); item.ContextMenu = cm; }
void SetTreeNodeTable(TreeNodeDatabase treenodedatabase) { ISqlControl sqlcontrol = treenodedatabase.sqlcontrol; DataTable dt_table = sqlcontrol.GetTableList(treenodedatabase.connectionstring, treenodedatabase.database); for (int i = 0; i < dt_table.Rows.Count; i++) { treenodedatabase.childer.Add(new TreeNodeTable() { text = dt_table.Rows[i]["name"].ToString(), connectionstring = treenodedatabase.connectionstring, database = treenodedatabase.database, table = dt_table.Rows[i]["name"].ToString(), sqlcontrol = sqlcontrol }); } }
private void but_ok_Click(object sender, RoutedEventArgs e) { TreeNodeDatabase treenodedatabase = (TreeNodeDatabase)this.Tag; ISqlControl sqlcontrol = treenodedatabase.sqlcontrol; DataTable tables = sqlcontrol.GetTableList(treenodedatabase.connectionstring, treenodedatabase.database); DataTable columns = sqlcontrol.GetColumnList(treenodedatabase.connectionstring, treenodedatabase.database); List <Dictionary <string, object> > tablecolumnslist = new List <Dictionary <string, object> >(); for (int i = 0; i < tables.Rows.Count; i++) { Dictionary <string, object> dic = new Dictionary <string, object>(); DataTable tablecolumns = columns.Clone(); DataRow[] drarr = columns.Select().Where(a => (string)a["tablename"] == (string)tables.Rows[i]["name"]).ToArray(); for (int j = 0; j < drarr.Length; j++) { tablecolumns.Rows.Add(drarr[j].ItemArray); } dic["tablename"] = (string)tables.Rows[i]["name"]; dic["columns"] = tablecolumns; tablecolumnslist.Add(dic); } string modelspath = text_path.Text + "/Models"; Directory.CreateDirectory(modelspath); using (StreamWriter sw = new StreamWriter(modelspath + "/Models.cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateModels(tables)); } for (int i = 0; i < tablecolumnslist.Count; i++) { using (StreamWriter sw = new StreamWriter(modelspath + "/" + tablecolumnslist[i]["tablename"] + ".cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateModel((DataTable)tablecolumnslist[i]["columns"], (string)tablecolumnslist[i]["tablename"])); } } using (StreamWriter sw = new StreamWriter(modelspath + "/GetModelList.cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateGetModelList(tables, columns)); } string notifymodelspath = text_path.Text + "/Notify Models"; Directory.CreateDirectory(notifymodelspath); using (StreamWriter sw = new StreamWriter(notifymodelspath + "/Models.cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateModels2(tables)); } for (int i = 0; i < tablecolumnslist.Count; i++) { using (StreamWriter sw = new StreamWriter(notifymodelspath + "/" + tablecolumnslist[i]["tablename"] + ".cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateModel2((DataTable)tablecolumnslist[i]["columns"], (string)tablecolumnslist[i]["tablename"])); } } using (StreamWriter sw = new StreamWriter(notifymodelspath + "/GetModelList.cs", false, Encoding.UTF8)) { sw.Write(sqlcontrol.GenerateGetModelList(tables, columns)); } SystemError.Information("类库已成功生成"); this.DialogResult = true; }