/// <summary> /// 删除选中数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelBtn_Click(object sender, EventArgs e) { //弹出删除提示框 DialogResult dr = MessageBox.Show("你确定要删除一条记录吗?", "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { dbName = DataBase.Text; //选择的数据库的名字 tbName = DataTable.Text; //选择的表的名字 if (dbName.Equals("请选择数据库") || tbName.Equals("选择表")) { MessageBox.Show("请选择数据库和表"); return; } string idName = dataView.Columns[0].HeaderText; //主键名 string id = dataView.CurrentRow.Cells[0].Value.ToString(); //主键值 try { //删除数据 MysqlUtil.DelById(dbName, tbName, idName, id); }catch (Exception ee) { MessageBox.Show(ee.Message); } DataTable dt = MysqlUtil.ExecuteAll(dbName, tbName); dataView.DataSource = dt; rows = dataView.RowCount; } }
/// <summary> /// 选中表时,执行查询全部语句 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataTable_SelectedIndexChanged(object sender, EventArgs e) { dbName = DataBase.Text; //选择的数据库的名字 tbName = DataTable.Text; //选择的表的名字 if (tbName.Equals("选择表")) { return; } DataTable dt = MysqlUtil.ExecuteAll(dbName, tbName); dataView.DataSource = dt; rows = dataView.RowCount; }
/// <summary> /// 当选中某一个数据库时,读取该数据库下的所有表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataBase_SelectedIndexChanged(object sender, EventArgs e) { dbName = DataBase.Text;//选择的数据库的名字 if (dbName.Equals("选择数据库")) { return; } List <string> tables = MysqlUtil.GetTablesByDB(dbName); DataTable.Items.Clear(); DataTable.Text = "选择表"; foreach (string table in tables) { DataTable.Items.Add(table); } }
/// <summary> /// 连接测试,并更新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ConnBtn_Click(object sender, EventArgs e) { string dataSource = dataSourceInput.Text; string port = portInput.Text; string user = userInput.Text; string pwd = pwdInput.Text; MysqlUtil.Config(dataSource, port, user, pwd); //配置基本信息 List <string> dataBases = MysqlUtil.GetDataBases(); //获取连接下所有的数据库名 DataBase.Items.Clear(); DataBase.Text = "选择数据库"; DataTable.Text = "选择表"; foreach (string db in dataBases) { DataBase.Items.Add(db); } MessageBox.Show("连接成功!请选择数据库"); }
//结束添加 private void EndAddBtn_Click(object sender, EventArgs e) { if (dbName.Equals("请选择数据库") || tbName.Equals("选择表")) { MessageBox.Show("请选择数据库和表"); return; } //如果不是添加状态,退出 if (!state.Equals("ADD")) { return; } else { for (int i = rows; i < dataView.RowCount; i++) { List <string> args = new List <string>();//所有的列值 for (int j = 0; j < dataView.ColumnCount; j++) { args.Add(Convert.ToString(dataView.Rows[i].Cells[j].Value)); } try { //插入 MysqlUtil.Insert(dbName, tbName, args); MessageBox.Show("插入成功!"); } catch (Exception ee) { MessageBox.Show("插入失败!\n" + ee.Message); } } } state = "FREE"; DataTable dt = MysqlUtil.ExecuteAll(dbName, tbName); dataView.DataSource = dt; rows = dataView.RowCount; }
private void EndUpdateBtn_Click(object sender, EventArgs e) { if (dbName.Equals("请选择数据库") || tbName.Equals("选择表")) { MessageBox.Show("请选择数据库和表"); return; } if (!state.Equals("UPDATE")) { return; } else { //将修改的行进行更新 foreach (int i in updateRows) { List <string> args = new List <string>();//所有的列值 for (int j = 0; j < dataView.ColumnCount; j++) { args.Add(Convert.ToString(dataView.Rows[i].Cells[j].Value)); } try { MysqlUtil.Update(dbName, tbName, args); MessageBox.Show("更新成功!"); } catch (Exception ee) { MessageBox.Show("更新失败!" + ee.Message); } } updateRows.Clear(); state = "FREE"; DataTable dt = MysqlUtil.ExecuteAll(dbName, tbName); dataView.DataSource = dt; } }