/// <summary> /// 测试连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(cbbServer.Text)) { MessageBox.Show("服务器不能为空!"); return; } if (string.IsNullOrEmpty(txtUserName.Text)) { MessageBox.Show("登陆名不能为空!"); return; } try { WEF.DbDAL.IDbObject dbObejct = new WEF.DbDAL.MySql.DbObject(false, cbbServer.Text, txtUserName.Text, txtPassword.Text, txtport.Text); DataTable DBNameTable = dbObejct.GetDBList(); cbbDatabase.Items.Clear(); cbbDatabase.Items.Add("全部"); foreach (DataRow dr in DBNameTable.Rows) { cbbDatabase.Items.Add(dr[0].ToString()); } cbbDatabase.Enabled = true; cbbDatabase.SelectedIndex = 0; MessageBox.Show("连接成功!"); button2.Enabled = true; } catch (Exception ex) { MessageBox.Show("连接失败!\n\r" + ex.Message); cbbDatabase.Enabled = false; } }
/// <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(); })); } }); }