private String getConnStr() { String host = txtIP.Text; String portStr = txtPort.Text; String userName = txtUserName.Text; String passWord = txtPassWord.Text; String connStr = MySqlUtil.getConnectionStr(host, portStr, userName, passWord); return(connStr); }
private void work() { //获取源数据 String host = txtSourceIP.Text.Trim(); String portStr = txtSourcePort.Text.Trim(); String userName = txtSourceUserName.Text.Trim(); String passWord = txtSourcePassWord.Text.Trim(); String dataBase = cboSourceDataBase.Text; String connectionString = MySqlUtil.getConnectionStr(host, portStr, userName, passWord, dataBase); connection = MySqlUtil.getConn(connectionString); Dictionary <String, DataTable> source = new Dictionary <string, DataTable>(); Dictionary <String, DataTable> sourceSetting = btnConnSource.Tag as Dictionary <String, DataTable>; Dictionary <String, DataTable> targetSetting = btnConnSource.Tag as Dictionary <String, DataTable>; DataTable sourceColumns = sourceSetting["columns"] as DataTable; DataTable targetColumns = targetSetting["columns"] as DataTable; lbState.Text = "开始获取源数据"; List <Dictionary <String, Dictionary <String, Object> > > sqlList = new List <Dictionary <String, Dictionary <String, Object> > >(); foreach (String tableName in clbTableInfo.CheckedItems) { bool createTable = radCreate.Checked && !radSkip.Checked; bool tableIsExist = checkTableExist(tableName, sourceColumns); if (createTable && tableIsExist) { String insertSql = getCreateSql(sourceColumns, tableName); } String sql = "select * from " + tableName; DataTable tempDT = MySqlUtil.getDateTable(connection, sql); DataTable tableInfo = sourceSetting[tableName]; StringBuilder fields = new StringBuilder(); var rows = sourceColumns.Select("TABLE_NAME = '" + tableName + "' and COLUMN_KEY <> 'PRI'").Distinct(row => row["COLUMN_NAME"]); foreach (var row in rows) { String colName = row["COLUMN_NAME"].ToString(); fields.Append(colName).Append(","); } } }
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 String getConnStr(String host, String portStr, String userName, String passWord) { String connStr = MySqlUtil.getConnectionStr(host, portStr, userName, passWord); return(connStr); }
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 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); } }