public static void ShowLoading(Form parent) { int i = 0; loadForm = new LoadForm(); Task.Factory.StartNew(() => { while (loadForm != null) { try { if (loadForm.IsHandleCreated) { loadForm.Invoke(new Action(() => { i++; if (loadForm.Visible) { loadForm.SetTime(i); } else { i = 0; } })); } } catch { loadForm = null; i = 0; } Thread.Sleep(1000); } }); Task.Factory.StartNew(() => { parent.Invoke(new Action(() => { loadForm.ShowDialog(parent); })); }); }
///快速查询 private void searchTextBox_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { var text = searchTextBox.Text; LoadForm.ShowLoading(this); var nodes = Treeview.Nodes; if (nodes != null && nodes.Count > 0) { for (int i = 0; i < nodes.Count; i++) { ChangeByKeyword(text, nodes[i]); } } LoadForm.HideLoading(this);; } }
public static void ShowLoading(Form parent) { _parent = parent; _parent.Invoke(new Action(() => { if (_loadForm == null) { _loadForm = new LoadForm(); } })); _loadForm.ShowDialogWithLoopAsync(_parent, (seconds) => { if (int.TryParse(seconds, out int os) && (os > 10 && !_loadForm.CancelLink.Visible)) { _loadForm.CancelLink.Visible = true; } _loadForm.SetTime(seconds); }); }
public void RunSql() { var sql = AutoTextBox.TextBox.Text; stopwatch.Restart(); LoadForm.ShowLoading(this); Task.Factory.StartNew(() => { WEF.DbDAL.IDbObject dbObject = null; if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer.ToString())) { dbObject = new WEF.DbDAL.SQL2000.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer9.ToString())) { dbObject = new WEF.DbDAL.SQL2005.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MsAccess.ToString())) { dbObject = new WEF.DbDAL.OleDb.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Oracle.ToString())) { dbObject = new WEF.DbDAL.Oracle.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Sqlite3.ToString())) { dbObject = new WEF.DbDAL.SQLite.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MySql.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MariaDB.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.PostgreSQL.ToString())) { dbObject = new WEF.DbDAL.PostgreSQL.DbObject(ConnectionModel.ConnectionString); } else { LoadForm.HideLoading(1); this.Invoke(new Action(() => { MessageBox.Show(this, "不支持的数据库类型!"); })); return; } if (string.IsNullOrEmpty(sql)) { LoadForm.HideLoading(1); this.Invoke(new Action(() => { MessageBox.Show(this, "sql内容不能为空!"); })); return; } this.Invoke(new Action(() => { if (!string.IsNullOrWhiteSpace(AutoTextBox.TextBox.SelectedText)) { sql = AutoTextBox.TextBox.SelectedText.Trim(); if (string.IsNullOrEmpty(sql)) { LoadForm.HideLoading(1); MessageBox.Show(this, "sql内容不能为空!"); return; } } dataGridView1.DataSource = null; })); if (sql.IndexOf("select", StringComparison.InvariantCultureIgnoreCase) >= 0) { try { int max = 50; var ds = dbObject.Query(ConnectionModel.Database, sql); if (ds != null && ds.Tables != null) { var dt = ds.Tables[0]; if (dt != null && dt.Rows.Count > 0) { var count = dt.Rows.Count; if (count > max) { for (int i = max; i < count; i++) { dt.Rows.RemoveAt(max); } } var dList = new List <int>(); for (int i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].DataType == typeof(DateTime)) { dList.Add(i); } } dataGridView1.Invoke(new Action(() => { dataGridView1.DataSource = dt; if (dList.Any()) { foreach (var item in dList) { dataGridView1.Columns[item].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss.fff"; } } lbl_execute.Text = $"当前显示{(max > count ? count : max)}行,影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalSeconds} 秒"; })); } } LoadForm.HideLoading(); } catch (Exception ex) { LoadForm.HideLoading(); this.Invoke(new Action(() => { MessageBox.Show(this, $"查询发生异常,ex:" + ex.Message); })); } } else { try { var count = dbObject.ExecuteSql(ConnectionModel.Database, sql); lbl_execute.Invoke(new Action(() => { lbl_execute.Text = $"影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalMilliseconds} 毫秒"; })); LoadForm.HideLoading(); } catch (Exception ex) { LoadForm.HideLoading(); this.Invoke(new Action(() => { MessageBox.Show(this, $"操作发生异常,ex:" + ex.Message); })); } } }); }
/// <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(); })); } }); }
/// <summary> /// 加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ContentForm_Load(object sender, EventArgs e) { LoadForm.ShowLoading(this); if (ConnectionModel != null) { this.DatabaseName = ConnectionModel.Database; this.TableName = ConnectionModel.TableName; this.IsView = ConnectionModel.IsView; } Task.Factory.StartNew(() => { WEF.DbDAL.IDbObject dbObject = null; if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer.ToString())) { dbObject = new WEF.DbDAL.SQL2000.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer9.ToString())) { dbObject = new WEF.DbDAL.SQL2005.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MsAccess.ToString())) { dbObject = new WEF.DbDAL.OleDb.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Oracle.ToString())) { dbObject = new WEF.DbDAL.Oracle.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Sqlite3.ToString())) { dbObject = new WEF.DbDAL.SQLite.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MySql.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MongoDB.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.PostgreSQL.ToString())) { dbObject = new WEF.DbDAL.PostgreSQL.DbObject(ConnectionModel.ConnectionString); } else { LoadForm.HideLoading(1); MessageBox.Show("未知数据库类型!"); return; } DataTable primarykeydt = dbObject.GetKeyName(DatabaseName, TableName); if (primarykeydt.Rows.Count == 0) { MessageBox.Show("当前表找不到任何主键,请选择上面的列表行来添加主键"); } this.Invoke(new Action(() => { cnnTxt.Text = ConnectionModel.ConnectionString; #region structure columnsdt = dbObject.GetColumnInfoList(DatabaseName, TableName); gridColumns.DataSource = columnsdt; #endregion cbPrimarykey.Items.Clear(); if (null != primarykeydt && primarykeydt.Rows.Count > 0) { foreach (DataRow dr in primarykeydt.Rows) { cbPrimarykey.Items.Add(dr["ColumnName"].ToString()); } cbPrimarykey.SelectedIndex = 0; } txtClassName.Text = TableName.Trim().Replace(' ', '_'); txtnamespace.Text = UtilsHelper.ReadNamespace(); })); LoadForm.HideLoading(1); }); }
/// <summary> /// 执行sql /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void autoTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { string sql = ((TextBox)sender).Text.Trim(); ShortcutKeyHelper.Run(sender, e, () => { stopwatch.Restart(); LoadForm.ShowLoading(this); Task.Factory.StartNew(() => { WEF.DbDAL.IDbObject dbObject = null; if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer.ToString())) { dbObject = new WEF.DbDAL.SQL2000.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.SqlServer9.ToString())) { dbObject = new WEF.DbDAL.SQL2005.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MsAccess.ToString())) { dbObject = new WEF.DbDAL.OleDb.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Oracle.ToString())) { dbObject = new WEF.DbDAL.Oracle.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.Sqlite3.ToString())) { dbObject = new WEF.DbDAL.SQLite.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(DatabaseType.MySql.ToString())) { dbObject = new WEF.DbDAL.MySql.DbObject(ConnectionModel.ConnectionString); } else { LoadForm.HideLoading(1); MessageBox.Show("不支持的数据库类型!"); return; } if (string.IsNullOrEmpty(sql)) { MessageBox.Show("sql内容不能为空!"); return; } this.Invoke(new Action(() => { if (!string.IsNullOrWhiteSpace(((TextBox)sender).SelectedText)) { sql = ((TextBox)sender).SelectedText.Trim(); if (string.IsNullOrEmpty(sql)) { MessageBox.Show("sql内容不能为空!"); return; } } dataGridView1.DataSource = null; })); if (string.Compare(sql, "select", true) >= 0) { try { int max = 50; var dt = dbObject.GetTabData(ConnectionModel.Database, sql, max); if (dt != null && dt.Rows.Count > 0) { var count = dt.Rows.Count; if (count > max) { for (int i = max; i < count; i++) { dt.Rows.RemoveAt(max); } } var dList = new List <int>(); for (int i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].DataType == typeof(DateTime)) { dList.Add(i); } } dataGridView1.Invoke(new Action(() => { dataGridView1.DataSource = dt; if (dList.Any()) { foreach (var item in dList) { dataGridView1.Columns[item].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss.fff"; } } lbl_execute.Text = $"当前显示{(max > count ? count : max)}行,影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalSeconds} 秒"; })); } } catch (Exception ex) { this.Invoke(new Action(() => { MessageBox.Show(this, $"查询发生异常,ex:" + ex.Message); })); } } else { try { var count = dbObject.ExecuteSql(ConnectionModel.Database, sql); lbl_execute.Invoke(new Action(() => { lbl_execute.Text = $"影响数据行数:{count} 耗时:{stopwatch.Elapsed.TotalMilliseconds} 毫秒"; })); } catch (Exception ex) { this.Invoke(new Action(() => { MessageBox.Show(this, $"操作发生异常,ex:" + ex.Message); })); } } LoadForm.HideLoading(); }); }); ShortcutKeyHelper.AllSelect(sender, e); }