private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (this.comboBox1.SelectedItem is DBConfig) { DBConfig config = (DBConfig)this.comboBox1.SelectedItem; this.textBoxPort.Text = config.port + ""; this.textBoxPwd.Text = config.password; this.textBoxUN.Text = config.user; } }
private void buttonConn_Click(object sender, EventArgs e) { MySqlConnectionStringBuilder str = new MySqlConnectionStringBuilder(); try { DBConfig config = null; string connStr = ""; if (comboBox1.SelectedItem is DBConfig) { config = (DBConfig)comboBox1.SelectedItem; } if (config == null) { string text = comboBox1.Text; Match match = Regex.Match(text, "(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})"); if (match.Success) { connStr = match.Groups[0].ToString(); } } else { connStr = config.server; } str.Password = textBoxPwd.Text; str.UserID = textBoxUN.Text; str.Server = connStr; str.Port = uint.Parse(textBoxPort.Text); str.ConnectionTimeout = 2; str.Pooling = true; if (conn != null) { conn.Close(); } conn = new MySqlConnection(str.ToString()); conn.Open(); writeLog(str.Server + ":" + str.Port + " 连接已建立!"); comboBox2.Items.Clear(); mycmd = new MySqlCommand("show databases;", conn); IDataReader dr = mycmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr[0]); } dr.Close(); if (config != null) { comboBox2.SelectedItem = config.defaultDB; } else { //str.Database = comboBox2.Text; } toolStrip1.Enabled = true; } catch (MySqlException mexc) { writeLog("连接数据库失败!", LOG_ERROR); writeLog(mexc.Message, LOG_ERROR); //writeLog("连接字符串:" + str.ToString(), LOG_ERROR); } catch (Exception exc) { writeLog(exc.Message, LOG_ERROR); } }