private void button1_Click(object sender, EventArgs e) { if (checkBox1.Checked) { connectStr = textBox5.Text; } else { connectStr = $"mongodb://{textBox3.Text}:{textBox4.Text}@${textBox1.Text}/{textBox6.Text}?authSource=admin"; } LoadForm.ShowLoading(this); Task.Factory.StartNew(() => { try { MongoDBTool.Connect(connectStr).GetDataBases(); this.Invoke(new Action(() => { button2.Enabled = true; LoadForm.HideLoading(); })); } catch (Exception ex) { LoadForm.HideLoading(); this.Invoke(new Action(() => { MessageBox.Show(this, "连接失败!\n\r" + ex.Message); })); } }); }
private void button2_Click(object sender, EventArgs e) { Task.Factory.StartNew(() => { LoadForm.ShowLoading(this); try { var mongoTool = MongoDBTool.Connect(connectStr); var dataBaseNames = mongoTool.GetDataBases(); var dataBaseName = ""; if (dataBaseNames != null && dataBaseNames.Any()) { if (System.Linq.Enumerable.Count(dataBaseNames) == 1) { dataBaseName = dataBaseNames.First(); } else { dataBaseName = "all"; } } this.Invoke(new Action(() => { Connection connectionModel = new Connection(); connectionModel.Database = dataBaseName; connectionModel.ID = Guid.NewGuid(); connectionModel.Name = mongoTool.ServerInfo + "(MongoDB)[" + connectionModel.Database + "]"; connectionModel.DbType = DatabaseType.MongoDB.ToString(); connectionModel.ConnectionString = connectStr; UtilsHelper.AddConnection(connectionModel); this.DialogResult = DialogResult.OK; this.Close(); })); } catch (Exception ex) { MessageBox.Show("连接失败!\n\r" + ex.Message); } LoadForm.HideLoading(); }); }
/// <summary> /// 双击treeview /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tview_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode node = Treeview.SelectedNode; if (node == null) { return; } if (node.Level == 0) { node.Nodes.Clear(); getServers(); Treeview.ExpandAll(); } else if (node.Level == 1) { node.Nodes.Clear(); getDatabaseinfo(node); } else if (node.Level == 4) { if (null != newcontentForm) { ConnectionModel conModel = _ConnectList.Find(delegate(ConnectionModel con) { return(con.ID.ToString().Equals(Treeview.SelectedNode.Parent.Parent.Parent.Tag.ToString())); }); if (conModel.DbType.Equals(DatabaseType.MongoDB.ToString())) { //todo MongoDBTool.Connect(conModel.ConnectionString).GetList(node.Parent.Parent.Text, node.Text, "{\"find\":\"Account\", limit:20, sort:{AddTime:-1}}"); } else { conConnectionString = Treeview.SelectedNode.Parent.Parent.Tag.ToString(); try { conModel.TableName = Treeview.SelectedNode.Text; conModel.Database = Treeview.SelectedNode.Parent.Parent.Text; conModel.IsView = Treeview.SelectedNode.Tag.ToString().Equals("V"); newcontentForm?.Invoke(conModel); } catch { } } } } }
/// <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(); })); } }); }