From 24cbb2ff435169a0a1f2503a8af6889cd8fff952 Mon Sep 17 00:00:00 2001 From: Virink Date: Mon, 11 Dec 2017 19:29:39 +0800 Subject: [PATCH] update add db config ini and fix Exception Message --- .../MysqlMonitoring.Designer.cs | 231 ++++++++++++------ windows/vMysqlMonitoring/MysqlMonitoring.cs | 137 +++++++++-- windows/vMysqlMonitoring/MysqlMonitoring.resx | 3 + 3 files changed, 264 insertions(+), 107 deletions(-) diff --git a/windows/vMysqlMonitoring/MysqlMonitoring.Designer.cs b/windows/vMysqlMonitoring/MysqlMonitoring.Designer.cs index 3937ca5..623fd6e 100644 --- a/windows/vMysqlMonitoring/MysqlMonitoring.Designer.cs +++ b/windows/vMysqlMonitoring/MysqlMonitoring.Designer.cs @@ -25,86 +25,40 @@ private void InitializeComponent() this.components = new System.ComponentModel.Container(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MysqlMonitoring)); - this.txt_pass = new System.Windows.Forms.TextBox(); - this.label3 = new System.Windows.Forms.Label(); - this.txt_user = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); - this.txt_host = new System.Windows.Forms.TextBox(); - this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.CopyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.txt_time = new System.Windows.Forms.TextBox(); this.txt_count = new System.Windows.Forms.Label(); this.txt_break = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.txt_searchkey = new System.Windows.Forms.TextBox(); - this.txt_time = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); + this.txt_host = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.txt_user = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.txt_pass = new System.Windows.Forms.TextBox(); + this.label5 = new System.Windows.Forms.Label(); + this.txt_port = new System.Windows.Forms.TextBox(); + this.label6 = new System.Windows.Forms.Label(); + this.linkLabel1 = new System.Windows.Forms.LinkLabel(); + this.linkLabel2 = new System.Windows.Forms.LinkLabel(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // - // txt_pass - // - this.txt_pass.Location = new System.Drawing.Point(391, 12); - this.txt_pass.Name = "txt_pass"; - this.txt_pass.Size = new System.Drawing.Size(123, 21); - this.txt_pass.TabIndex = 7; - this.txt_pass.Text = "root"; - // - // label3 - // - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(337, 16); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(41, 12); - this.label3.TabIndex = 3; - this.label3.Text = "密码:"; - // - // txt_user - // - this.txt_user.Location = new System.Drawing.Point(243, 12); - this.txt_user.Name = "txt_user"; - this.txt_user.Size = new System.Drawing.Size(81, 21); - this.txt_user.TabIndex = 8; - this.txt_user.Text = "root"; - // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(189, 16); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(41, 12); - this.label2.TabIndex = 4; - this.label2.Text = "用户:"; - // - // txt_host - // - this.txt_host.Location = new System.Drawing.Point(69, 12); - this.txt_host.Name = "txt_host"; - this.txt_host.Size = new System.Drawing.Size(107, 21); - this.txt_host.TabIndex = 9; - this.txt_host.Text = "localhost"; - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(15, 16); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(41, 12); - this.label1.TabIndex = 5; - this.label1.Text = "主机:"; - // // button1 // this.button1.BackColor = System.Drawing.Color.White; - this.button1.Location = new System.Drawing.Point(527, 11); + this.button1.Location = new System.Drawing.Point(656, 11); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); - this.button1.TabIndex = 10; + this.button1.TabIndex = 4; this.button1.Text = "初始化"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); @@ -112,10 +66,10 @@ private void InitializeComponent() // button2 // this.button2.BackColor = System.Drawing.Color.White; - this.button2.Location = new System.Drawing.Point(615, 11); + this.button2.Location = new System.Drawing.Point(744, 11); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); - this.button2.TabIndex = 10; + this.button2.TabIndex = 5; this.button2.Text = "获 取"; this.button2.UseVisualStyleBackColor = false; this.button2.Click += new System.EventHandler(this.button2_Click); @@ -177,6 +131,13 @@ private void InitializeComponent() this.CopyToolStripMenuItem.Text = "复 制"; this.CopyToolStripMenuItem.Click += new System.EventHandler(this.CopyToolStripMenuItem_Click); // + // txt_time + // + this.txt_time.Location = new System.Drawing.Point(138, 37); + this.txt_time.Name = "txt_time"; + this.txt_time.Size = new System.Drawing.Size(100, 21); + this.txt_time.TabIndex = 18; + // // txt_count // this.txt_count.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -211,15 +172,113 @@ private void InitializeComponent() this.txt_searchkey.Location = new System.Drawing.Point(900, 12); this.txt_searchkey.Name = "txt_searchkey"; this.txt_searchkey.Size = new System.Drawing.Size(269, 21); - this.txt_searchkey.TabIndex = 14; + this.txt_searchkey.TabIndex = 6; this.txt_searchkey.TextChanged += new System.EventHandler(this.txt_searchkey_TextChanged); // - // txt_time + // label1 // - this.txt_time.Location = new System.Drawing.Point(138, 37); - this.txt_time.Name = "txt_time"; - this.txt_time.Size = new System.Drawing.Size(100, 21); - this.txt_time.TabIndex = 18; + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(15, 16); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(29, 12); + this.label1.TabIndex = 5; + this.label1.Text = "主机"; + // + // txt_host + // + this.txt_host.Location = new System.Drawing.Point(67, 12); + this.txt_host.Name = "txt_host"; + this.txt_host.Size = new System.Drawing.Size(82, 21); + this.txt_host.TabIndex = 0; + this.txt_host.Text = "localhost"; + this.txt_host.TextChanged += new System.EventHandler(this.txt_host_TextChanged); + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(329, 16); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(29, 12); + this.label2.TabIndex = 4; + this.label2.Text = "用户"; + // + // txt_user + // + this.txt_user.Location = new System.Drawing.Point(381, 12); + this.txt_user.Name = "txt_user"; + this.txt_user.Size = new System.Drawing.Size(82, 21); + this.txt_user.TabIndex = 2; + this.txt_user.Text = "root"; + this.txt_user.TextChanged += new System.EventHandler(this.txt_user_TextChanged); + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(486, 16); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(29, 12); + this.label3.TabIndex = 3; + this.label3.Text = "密码"; + // + // txt_pass + // + this.txt_pass.Location = new System.Drawing.Point(538, 12); + this.txt_pass.Name = "txt_pass"; + this.txt_pass.Size = new System.Drawing.Size(82, 21); + this.txt_pass.TabIndex = 3; + this.txt_pass.Text = "123456"; + this.txt_pass.TextChanged += new System.EventHandler(this.txt_pass_TextChanged); + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(172, 16); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(29, 12); + this.label5.TabIndex = 17; + this.label5.Text = "端口"; + // + // txt_port + // + this.txt_port.Location = new System.Drawing.Point(224, 12); + this.txt_port.Name = "txt_port"; + this.txt_port.Size = new System.Drawing.Size(82, 21); + this.txt_port.TabIndex = 1; + this.txt_port.Text = "3306"; + this.txt_port.TextChanged += new System.EventHandler(this.txt_port_TextChanged); + // + // label6 + // + this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.label6.AutoSize = true; + this.label6.Enabled = false; + this.label6.Location = new System.Drawing.Point(1020, 541); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(149, 12); + this.label6.TabIndex = 18; + this.label6.Text = "【借鉴Seay法师插件源码】"; + // + // linkLabel1 + // + this.linkLabel1.AutoSize = true; + this.linkLabel1.Location = new System.Drawing.Point(440, 541); + this.linkLabel1.Name = "linkLabel1"; + this.linkLabel1.Size = new System.Drawing.Size(173, 12); + this.linkLabel1.TabIndex = 22; + this.linkLabel1.TabStop = true; + this.linkLabel1.Text = "Blog : https://www.virzz.com"; + this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked); + // + // linkLabel2 + // + this.linkLabel2.AutoSize = true; + this.linkLabel2.Location = new System.Drawing.Point(619, 541); + this.linkLabel2.Name = "linkLabel2"; + this.linkLabel2.Size = new System.Drawing.Size(395, 12); + this.linkLabel2.TabIndex = 23; + this.linkLabel2.TabStop = true; + this.linkLabel2.Text = "Github : https://github.com/virzz general_log版 Ver 2.0 By Virink"; + this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked); // // MysqlMonitoring // @@ -227,25 +286,30 @@ private void InitializeComponent() this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(1184, 562); + this.Controls.Add(this.linkLabel2); + this.Controls.Add(this.linkLabel1); + this.Controls.Add(this.label6); + this.Controls.Add(this.txt_port); + this.Controls.Add(this.label5); + this.Controls.Add(this.txt_pass); + this.Controls.Add(this.label3); + this.Controls.Add(this.txt_user); + this.Controls.Add(this.label2); + this.Controls.Add(this.txt_host); + this.Controls.Add(this.label1); this.Controls.Add(this.txt_searchkey); this.Controls.Add(this.txt_break); this.Controls.Add(this.txt_count); this.Controls.Add(this.groupBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); - this.Controls.Add(this.txt_pass); this.Controls.Add(this.label4); - this.Controls.Add(this.label3); - this.Controls.Add(this.txt_user); - this.Controls.Add(this.label2); - this.Controls.Add(this.txt_host); - this.Controls.Add(this.label1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimumSize = new System.Drawing.Size(1200, 200); this.Name = "MysqlMonitoring"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "MysSQL 执行语句监测 general_log版本 By Virink 【借鉴Seay法师插件源码】"; + this.Text = "MySQL 执行语句监测工具"; this.Load += new System.EventHandler(this.F_MysqlMonitoring_Load); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); @@ -258,12 +322,6 @@ private void InitializeComponent() #endregion - private System.Windows.Forms.TextBox txt_pass; - private System.Windows.Forms.Label label3; - private System.Windows.Forms.TextBox txt_user; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox txt_host; - private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.GroupBox groupBox1; @@ -275,5 +333,16 @@ private void InitializeComponent() private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txt_searchkey; private System.Windows.Forms.TextBox txt_time; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox txt_host; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox txt_user; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox txt_pass; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.TextBox txt_port; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.LinkLabel linkLabel1; + private System.Windows.Forms.LinkLabel linkLabel2; } } \ No newline at end of file diff --git a/windows/vMysqlMonitoring/MysqlMonitoring.cs b/windows/vMysqlMonitoring/MysqlMonitoring.cs index 19b32c0..ecc272f 100644 --- a/windows/vMysqlMonitoring/MysqlMonitoring.cs +++ b/windows/vMysqlMonitoring/MysqlMonitoring.cs @@ -1,26 +1,37 @@ using System; +using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; +using System.Runtime.InteropServices; using MySql.Data.MySqlClient; namespace MysqlMonitoringMain { public partial class MysqlMonitoring : Form { + string config_path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+Path.DirectorySeparatorChar+"vmysqlmotitoring.conf"; + //断点时间 + string var_datatime = ""; + + // 数据库配置 + string M_str_sqlcon = ""; + + BindingSource Bs = null; + public MysqlMonitoring() { InitializeComponent(); } - //断点时间 - string var_datatime = ""; - Timer myTimer = new System.Windows.Forms.Timer(); - BindingSource Bs = null; + [DllImport("kernel32")] + private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); + [DllImport("kernel32")] + private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); #region 建立MySql数据库连接 /// @@ -29,8 +40,6 @@ public MysqlMonitoring() /// 返回MySqlConnection对象 public MySqlConnection func_getmysqlcon() { - string M_str_sqlcon = "server=" + txt_host.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; - MySqlConnection myCon = new MySqlConnection(M_str_sqlcon); return myCon; } @@ -65,41 +74,75 @@ public DataSet func_getmysqlread(string M_str_sqlstr) { MySqlConnection mysqlcon = this.func_getmysqlcon(); mysqlcon.Open(); - MySqlDataAdapter sda = new MySqlDataAdapter(M_str_sqlstr, mysqlcon); - //MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); - DataSet ds = new DataSet(); sda.Fill(ds); - //MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection); return ds; } #endregion - private void init() + #region 读取INI文件 + /// + /// 读取INI文件 + /// + /// 项目名称(如 [section] ) + /// 键 + /// 路径 + public string IniReadValue(string section, string skey, string path, string _default = "") + { + StringBuilder temp = new StringBuilder(500); + int i = GetPrivateProfileString(section, skey, _default, temp, 500, path); + return temp.ToString(); + } + #endregion + + #region 写入INI文件 + /// + /// 写入ini文件 + /// + /// 项目名称 + /// 键 + /// 值 + /// 路径 + public void IniWrite(string section, string key, string value, string path) + { + WritePrivateProfileString(section, key, value, path); + } + #endregion + + private void F_MysqlMonitoring_Load(object sender, EventArgs e) + { + db_config_str(); + init_general_log(); + } + + private void init_general_log() { - var_datatime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); - txt_break.Text = "断点:" + var_datatime; try { - func_getmysqlcom("set global general_log=on;SET GLOBAL log_output='table';"); + func_getmysqlcom("set global general_log=off;truncate table general_log;set global general_log=on;SET GLOBAL log_output='table';"); } - catch (Exception) + catch (Exception ee) { - MessageBox.Show("数据库出错,请检查连接信息以及确认mysql版本在5.1.6以上", "提示"); + MessageBox.Show(ee.Message, "提示"); } } + private void setTimeNow() + { + var_datatime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + txt_break.Text = "断点:" + var_datatime; + } + private void button1_Click(object sender, EventArgs e) { - init(); + setTimeNow(); } private void button2_Click(object sender, EventArgs e) { try { - //func_getmysqlcom("set global general_log=on;SET GLOBAL log_output='table';"); string sql = "SELECT event_time,argument FROM mysql.general_log WHERE (command_type = 'Query' OR command_type = 'Execute') AND argument NOT LIKE '%general_log%' AND argument NOT LIKE '%select event_time,argument from%' AND argument NOT LIKE '%SHOW%' AND argument NOT LIKE '%SELECT STATE%' AND argument NOT LIKE '%SET NAMES%' AND argument NOT LIKE '%SET PROFILING%' AND argument NOT LIKE '%SELECT QUERY_ID%' AND event_time>'" + var_datatime + "'"; DataSet ds = func_getmysqlread(sql); DataTableCollection tables = ds.Tables; @@ -113,20 +156,18 @@ private void button2_Click(object sender, EventArgs e) dataGridView1.Columns[1].HeaderText = "语句"; dataGridView1.Columns[0].Width = 150; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1; + if (this.dataGridView1.Rows.Count > 0) + { + dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1; + } } - catch (Exception) + catch (Exception ee) { - MessageBox.Show("数据库出错,请检查连接信息以及确认mysql版本在5.1.6以上", "提示"); + MessageBox.Show(ee.Message, "提示"); } } - private void F_MysqlMonitoring_Load(object sender, EventArgs e) - { - init(); - } - private void CopyToolStripMenuItem_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) @@ -151,5 +192,49 @@ private void txt_searchkey_TextChanged(object sender, EventArgs e) } } } + + private void db_config_str() + { + txt_host.Text = IniReadValue("Config", "host", config_path, "localhost"); + txt_port.Text = IniReadValue("Config", "port", config_path, "3306"); + txt_user.Text = IniReadValue("Config", "user", config_path, "root"); + txt_pass.Text = IniReadValue("Config", "pass", config_path, "123456"); + M_str_sqlcon = "server=" + txt_host.Text + ";port=" + txt_port.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; + } + + private void txt_host_TextChanged(object sender, EventArgs e) + { + IniWrite("Config", "host", txt_host.Text, config_path); + M_str_sqlcon = "server=" + txt_host.Text + ";port=" + txt_port.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; + } + + private void txt_port_TextChanged(object sender, EventArgs e) + { + IniWrite("Config", "port", txt_port.Text, config_path); + M_str_sqlcon = "server=" + txt_host.Text + ";port=" + txt_port.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; + } + + private void txt_user_TextChanged(object sender, EventArgs e) + { + IniWrite("Config", "user", txt_user.Text, config_path); + M_str_sqlcon = "server=" + txt_host.Text + ";port=" + txt_port.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; + } + + private void txt_pass_TextChanged(object sender, EventArgs e) + { + IniWrite("Config", "pass", txt_pass.Text, config_path); + M_str_sqlcon = "server=" + txt_host.Text + ";port=" + txt_port.Text + ";user id=" + txt_user.Text + ";password=" + txt_pass.Text + ";database=mysql"; + } + + private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + { + System.Diagnostics.Process.Start("https://www.virzz.com"); + } + + private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) + { + System.Diagnostics.Process.Start("https://github.com/virink/vMysqlMonitoring"); + } + } } diff --git a/windows/vMysqlMonitoring/MysqlMonitoring.resx b/windows/vMysqlMonitoring/MysqlMonitoring.resx index cc9a591..0b47dd4 100644 --- a/windows/vMysqlMonitoring/MysqlMonitoring.resx +++ b/windows/vMysqlMonitoring/MysqlMonitoring.resx @@ -120,6 +120,9 @@ 17, 17 + + 50 +