/// <summary> /// 刷新数据库表和视图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 刷新ToolStripMenuItem2_Click(object sender, EventArgs e) { TreeNode node = Treeview.SelectedNode; if (node.Level == 3) { node = node.Parent; } if (node.Level == 4) { node = node.Parent.Parent; } node.Nodes.Clear(); Connection conModel = null; try { conModel = _ConnectList.Find(delegate(Connection con) { return(con.ID.ToString().Equals(node.Parent.Tag.ToString())); }); IDbObject dbObject; if (conModel.DbType.Equals(DatabaseType.MsAccess.ToString())) { dbObject = new WEF.DbDAL.OleDb.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(""), dbObject.GetVIEWs("")); } else if (conModel.DbType.Equals(DatabaseType.SqlServer.ToString())) { dbObject = new WEF.DbDAL.SQL2000.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(DatabaseType.SqlServer9.ToString())) { dbObject = new WEF.DbDAL.SQL2005.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(DatabaseType.Oracle.ToString())) { dbObject = new WEF.DbDAL.Oracle.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(DatabaseType.MySql.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(DatabaseType.Sqlite3.ToString())) { dbObject = new WEF.DbDAL.SQLite.DbObject(conConnectionString); ShowTablesAndViews(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(DatabaseType.MongoDB.ToString())) { var mongoDBTool = MongoDBTool.Connect(conConnectionString); ShowCollections(node, mongoDBTool.GetCollections(node.Text)); } node.ExpandAll(); } catch (Exception ex) { MessageBox.Show(ex.Message, "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 获取数据库服务器 /// </summary> private void getDatabaseinfo() { LoadForm.ShowLoading(this); TreeNode node = Treeview.SelectedNode; Task.Factory.StartNew(() => { try { Connection conModel = null; this.Invoke(new Action(() => { conModel = _ConnectList.Find(delegate(Connection con) { return(con.ID.ToString().Equals(node.Tag.ToString())); }); })); conConnectionString = conModel.ConnectionString; IDbObject dbObject; if (conModel.DbType.Equals(DatabaseType.MsAccess.ToString())) { dbObject = new WEF.DbDAL.OleDb.DbObject(conConnectionString); TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(""); var views = dbObject.GetVIEWs(""); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } else if (conModel.DbType.Equals(DatabaseType.Sqlite3.ToString())) { dbObject = new WEF.DbDAL.SQLite.DbObject(conConnectionString); TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(""); var views = dbObject.GetVIEWs(""); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } else if (conModel.DbType.Equals(DatabaseType.SqlServer.ToString()) || conModel.DbType.Equals(DatabaseType.SqlServer9.ToString())) { if (conModel.DbType.Equals(DatabaseType.SqlServer.ToString())) { dbObject = new WEF.DbDAL.SQL2000.DbObject(conConnectionString); } else { dbObject = new WEF.DbDAL.SQL2005.DbObject(conConnectionString); } if (conModel.Database.Equals("all")) { DataTable dt = dbObject.GetDBList(); foreach (DataRow dr in dt.Rows) { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(dr[0].ToString(), 1, 1); tnode.Tag = conConnectionString.Replace("master", dr[0].ToString()); tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else if (conModel.DbType.Equals(DatabaseType.Oracle.ToString())) { dbObject = new WEF.DbDAL.Oracle.DbObject(conConnectionString); TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } else if (conModel.DbType.Equals(DatabaseType.MySql.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(conConnectionString); if (conModel.Database.Equals("all")) { DataTable dt = dbObject.GetDBList(); foreach (DataRow dr in dt.Rows) { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(dr[0].ToString(), 1, 1); tnode.Tag = conConnectionString.Replace("master", dr[0].ToString()); tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else if (conModel.DbType.Equals(DatabaseType.PostgreSQL.ToString())) { dbObject = new WEF.DbDAL.PostgreSQL.DbObject(conConnectionString); if (conModel.Database.Equals("all")) { DataTable dt = dbObject.GetDBList(); foreach (DataRow dr in dt.Rows) { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(dr[0].ToString(), 1, 1); tnode.Tag = conConnectionString.Replace("postgres", dr[0].ToString()); tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); })); var tables = dbObject.GetTables(tnode.Text); var views = dbObject.GetVIEWs(tnode.Text); this.Invoke(new Action(() => { ShowTablesAndViews(tnode, tables, views); })); } } else if (conModel.DbType.Equals(DatabaseType.MongoDB.ToString())) { var dataBaseName = "admin"; var mongoDBTool = MongoDBTool.Connect(conConnectionString); if (conModel.Database.Equals("all")) { var dataBaseNames = mongoDBTool.GetDataBases(); foreach (var dbs in dataBaseNames) { TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(dbs, 1, 1); tnode.Tag = conConnectionString.Replace(dataBaseName, dbs); tnode.ContextMenuStrip = contextMenuStripOneMongoDB; node.Nodes.Add(tnode); })); var cs = mongoDBTool.GetCollections(dbs); this.Invoke(new Action(() => { ShowCollections(tnode, cs); })); } } else { dataBaseName = conModel.Database; TreeNode tnode = null; this.Invoke(new Action(() => { tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conConnectionString; tnode.ContextMenuStrip = contextMenuStripOneMongoDB; node.Nodes.Add(tnode); })); var collections = mongoDBTool.GetCollections(dataBaseName); this.Invoke(new Action(() => { ShowCollections(tnode, collections); })); } } LoadForm.HideLoading(); } catch (Exception ex) { LoadForm.HideLoading(); MessageBox.Show(ex.Message, "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Invoke(new Action(() => { node.Expand(); })); } }); }