Exemplo n.º 1
0
        void SetTreeNodeTableMenu(TreeView tree, TreeViewItem item)
        {
            TreeNodeTable treenodetable = (TreeNodeTable)tree.SelectedItem;
            ISqlControl   sqlcontrol    = treenodetable.sqlcontrol;
            ContextMenu   cm            = new ContextMenu();

            MenuItem menu_generate = new MenuItem();

            menu_generate.Header = "生成Model";
            menu_generate.Click += delegate(object obj, RoutedEventArgs earg)
            {
                DataTable columns = sqlcontrol.GetColumnList(treenodetable.connectionstring, treenodetable.database, treenodetable.table);
                text_content.Text  = sqlcontrol.GenerateModel(columns, treenodetable.table);
                text_content2.Text = sqlcontrol.GenerateModel2(columns, treenodetable.table);
            };
            cm.Items.Add(menu_generate);

            item.ContextMenu = cm;
        }
Exemplo n.º 2
0
        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;
        }