private void btnLogin_Click(object sender, EventArgs e) { try { if (testForm() && testConnection()) { DataTable dataBases = MySqlUtil.getSchema(connStr, "Databases"); DataRow[] rows = dataBases.Select("database_name in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { dataBases.Rows.Remove(row); } DataTable tables = MySqlUtil.getSchema(connStr, "Tables"); rows = tables.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { tables.Rows.Remove(row); } DataTable columns = MySqlUtil.getSchema(connStr, "Columns"); rows = columns.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { columns.Rows.Remove(row); } SessionUtil.Session.put("dataBases", dataBases); SessionUtil.Session.put("tables", tables); SessionUtil.Session.put("columns", columns); string passsword = txtPassWord.Text.Trim(); string ip = txtIP.Text.Trim(); String userName = txtUserName.Text.Trim(); string port = txtPort.Text.Trim(); String settingName = "@" + ip + ":" + port; DataTable setting = cboSetting.Tag as DataTable; DataRow[] result = setting.Select("password = '******'" + " AND userName = '******'" + " AND port = '" + port + "'" + " AND ip = '" + ip + "'"); if (result.Length == 0) { StringBuilder sbSql = new StringBuilder(); sbSql.Append(" INSERT INTO tb_database (dbName, password, ip, userName, port, settingName) VALUES ( "); sbSql.Append(" :dbName, :password, :ip, :userName, :port, :settingName "); sbSql.Append(" ) "); Dictionary <String, Object> parameters = new Dictionary <string, object>(); parameters.Add("dbName", ""); parameters.Add("password", txtPassWord.Text.Trim()); parameters.Add("ip", txtIP.Text.Trim()); parameters.Add("userName", txtUserName.Text.Trim()); parameters.Add("port", txtPort.Text.Trim()); parameters.Add("settingName", settingName); SqliteHelper.excuteNoQuery(sbSql.ToString(), parameters); } XMLUtil.setValue("Host", ip); XMLUtil.setValue("Port", port); XMLUtil.setValue("DBUserName", userName); XMLUtil.setValue("PassWord", passsword); XMLUtil.setValue("SelectedDbSetting", settingName); this.DialogResult = DialogResult.Yes; } else { this.DialogResult = DialogResult.No; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnConnTarget_Click(object sender, EventArgs e) { try { if (btnConnTarget.Text == "连接(&B)") { String host = txtTargetIP.Text.Trim(); String portStr = txtTargetPort.Text.Trim(); String userName = txtTargetUserName.Text.Trim(); String passWord = txtTargetPassWord.Text.Trim(); if (host.Length == 0) { MessageBox.Show("目标数据库IP地址不能为空!"); txtTargetIP.Focus(); return; } if (portStr.Length == 0) { MessageBox.Show("目标数据库端口不能为空!"); txtTargetPort.Focus(); return; } int port = 0; if (!Int32.TryParse(portStr, out port)) { MessageBox.Show("目标数据库端口必须为数字!"); txtTargetPort.Focus(); return; } if (userName.Length == 0) { MessageBox.Show("目标数据库用户名不能为空!"); txtTargetUserName.Focus(); return; } if (passWord.Length == 0) { MessageBox.Show("目标数据库密码不能为空!"); txtTargetPassWord.Focus(); return; } if (testConnection(host, portStr, userName, passWord)) { btnConnTarget.Text = "修改(&B)"; lbTargetState.Text = "目标数据库已连接"; txtTargetIP.ReadOnly = true; txtTargetPort.ReadOnly = true; txtTargetUserName.ReadOnly = true; txtTargetPassWord.ReadOnly = true; lbTargetState.ForeColor = Color.Green; lbTargetState.Tag = true; XMLUtil.setValue("targetIP", host); XMLUtil.setValue("targetPort", portStr); XMLUtil.setValue("targetUserName", userName); XMLUtil.setValue("targetPassWord", passWord); String connStr = MySqlUtil.getConnectionStr(host, portStr, userName, passWord); DataTable dataBases = MySqlUtil.getSchema(connStr, "Databases"); String selectedSorceDataBase = cboSourceDataBase.Text; DataRow[] rows = dataBases.Select("database_name in ('performance_schema','information_schema','mysql') AND database_name<>'" + selectedSorceDataBase + "'"); foreach (DataRow row in rows) { dataBases.Rows.Remove(row); } DataTable tables = MySqlUtil.getSchema(connStr, "Tables"); rows = tables.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { tables.Rows.Remove(row); } DataTable columns = MySqlUtil.getSchema(connStr, "Columns"); rows = columns.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { columns.Rows.Remove(row); } Dictionary <String, DataTable> target = new Dictionary <string, DataTable>(); target.Add("dataBases", dataBases); target.Add("tables", tables); target.Add("columns", columns); btnConnTarget.Tag = target; bindCbo(cboTargetDataBase, dataBases, "database_name"); cboTargetDataBase.Tag = dataBases; String selectedTargetDataBase = XMLUtil.getValue("selectedTargetDataBase"); if (selectedTargetDataBase.Length != 0 && dataBases.Rows.Count > 0) { int index = getRowIndex(dataBases, "database_name", selectedTargetDataBase); cboTargetDataBase.SelectedIndex = index; } } else { lbTargetState.Text = "目标数据库未连接"; lbTargetState.ForeColor = Color.Red; lbTargetState.Tag = null; MessageBox.Show("目标数据库链接失败"); } } else { txtTargetIP.ReadOnly = false; txtTargetPort.ReadOnly = false; txtTargetUserName.ReadOnly = false; txtTargetPassWord.ReadOnly = false; lbTargetState.ForeColor = Color.Red; lbTargetState.Tag = null; btnConnTarget.Text = "连接(&B)"; lbTargetState.Text = "目标数据库未连接"; cboTargetDataBase.Items.Clear(); cboTargetDataBase.Text = ""; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void CreateBeanForm_Load(object sender, EventArgs e) { String exportPath = XMLUtil.getValue("BeanPath"); txtPath.Text = exportPath.Trim(); try { raHibernate.Checked = isHibernate; raMybatis.Checked = !isHibernate; reloadSettingDT(); lbConnStr.Text = ""; lbState.Text = ""; String host = XMLUtil.getValue("Host"); String port = XMLUtil.getValue("Port"); String userName = XMLUtil.getValue("DBUserName"); String passWord = XMLUtil.getValue("PassWord"); if (host.Length != 0 && port.Length != 0 && userName.Length != 0 && passWord.Length != 0) { String connStr = MySqlUtil.getConnectionStr(host, port, userName, passWord); this.connStr = connStr; DataTable dataBases = MySqlUtil.getSchema(connStr, "Databases"); DataRow[] rows = dataBases.Select("database_name in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { dataBases.Rows.Remove(row); } DataTable tables = MySqlUtil.getSchema(connStr, "Tables"); rows = tables.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { tables.Rows.Remove(row); } DataTable columns = MySqlUtil.getSchema(connStr, "Columns"); rows = columns.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { columns.Rows.Remove(row); } Session.put("dataBases", dataBases); Session.put("tables", tables); Session.put("columns", columns); isConnected = true; } if (Session.check("dataBases")) { DataTable dataBases = Session.get("dataBases") as DataTable; cboDataBase.ComboBox.DataSource = dataBases; cboDataBase.ComboBox.DisplayMember = "database_name"; cboDataBase.ComboBox.ValueMember = "database_name"; lbConnStr.Text = connStr.Substring(0, connStr.LastIndexOf("password")); DataRow[] rows = dataBases.Select("database_name = '" + selectedDataBase + "'"); if (rows.Length > 0) { int index = dataBases.Rows.IndexOf(rows[0]); cboDataBase.SelectedIndex = index; } else if (dataBases.Rows.Count > 0) { cboDataBase.SelectedIndex = 0; } String schemaName = cboDataBase.Text; InitTree(schemaName, ""); } lbDbConnState.Text = isConnected ? "已连接" : "未连接"; lbDbConnState.ForeColor = isConnected ? Color.Green : Color.Red; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void MainForm_Load(object sender, EventArgs e) { try { lbConnStr.Text = ""; lbState.Text = ""; String host = XMLUtil.getValue("Host"); String port = XMLUtil.getValue("Port"); String userName = XMLUtil.getValue("DBUserName"); String passWord = XMLUtil.getValue("PassWord"); if (host.Length != 0 && port.Length != 0 && userName.Length != 0 && passWord.Length != 0) { String connStr = MySqlUtil.getConnectionStr(host, port, userName, passWord); this.connStr = connStr; DataTable dataBases = MySqlUtil.getSchema(connStr, "Databases"); DataRow[] rows = dataBases.Select("database_name in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { dataBases.Rows.Remove(row); } DataTable tables = MySqlUtil.getSchema(connStr, "Tables"); rows = tables.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { tables.Rows.Remove(row); } DataTable columns1 = new DataTable(); columns1.Columns.Add("database_name"); columns1.Columns.Add("TABLE_SCHEMA"); columns1.Columns.Add("column"); StringBuilder sqlBuilder = new StringBuilder(); foreach (DataRow table in tables.Rows) { String databaseName = table["TABLE_SCHEMA"].ToString(); String tableName = table["TABLE_NAME"].ToString(); String sql = "select * from " + databaseName + "." + tableName + " limit 1;"; sqlBuilder.Append(sql); } DataTable c = MySqlUtil.getDateTable(connStr, sqlBuilder.ToString()); DataTable columns = MySqlUtil.getSchema(connStr, "Columns"); rows = columns.Select("TABLE_SCHEMA in ('performance_schema','information_schema','mysql')"); foreach (DataRow row in rows) { columns.Rows.Remove(row); } Session.put("dataBases", dataBases); Session.put("tables", tables); Session.put("columns", columns); isConnected = true; } if (Session.check("dataBases")) { DataTable dataBases = Session.get("dataBases") as DataTable; cboDataBase.ComboBox.DataSource = dataBases; cboDataBase.ComboBox.DisplayMember = "database_name"; cboDataBase.ComboBox.ValueMember = "database_name"; lbConnStr.Text = connStr.Substring(0, connStr.LastIndexOf("password")); DataRow[] rows = dataBases.Select("database_name = '" + selectedDataBase + "'"); if (rows.Length > 0) { int index = dataBases.Rows.IndexOf(rows[0]); cboDataBase.SelectedIndex = index; } else if (dataBases.Rows.Count > 0) { cboDataBase.SelectedIndex = 0; } String schemaName = cboDataBase.Text; InitTree(schemaName, ""); } String SelectTable = XMLUtil.getValue("SelectTable"); lbDbConnState.Text = isConnected ? "已连接" : "未连接"; lbDbConnState.ForeColor = isConnected ? Color.Green : Color.Red; getExcludedFiledSetting(); radHibernate.Checked = XMLUtil.getValue("isHibernate").Equals("true"); radMybatis.Checked = !XMLUtil.getValue("isHibernate").Equals("true"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }