//改 public void updateData(MySqlData data) { if (this.OpenConection() == true) { string text = "update nanning_city set area_name='" + data.Area + "',name='" + data.Name + "',price=" + data.Price + " where id=" + data.Id + ";"; string text1 = "select count(*) from nanning_city where area_name = '" + data.Area + "' and name='" + data.Name + "' and price=" + data.Price + ";"; if (this.OpenConection()) { this.cmd = new MySqlCommand(text1, con); int count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { MessageBox.Show("数据已存在!"); } else { cmd = new MySqlCommand(text, con); cmd.ExecuteNonQuery(); this.CloseConnection(); MessageBox.Show("修改成功!"); } } } this.CloseConnection(); }
//增 public void addData(MySqlData data) { string text = "insert into nanning_city" + "(area_name,name,price) values('" + data.Area + "','" + data.Name + "','" + data.Price + "');"; string text1 = "select count(*) from nanning_city where area_name = '" + data.Area + "' and name='" + data.Name + "' and price=" + data.Price + ";"; if (this.OpenConection() == true) { //查询插入数据是否已经存在 this.cmd = new MySqlCommand(text1, con); int count = Convert.ToInt32(cmd.ExecuteScalar()); if (count > 0) { MessageBox.Show("数据已存在!"); } else { this.cmd = new MySqlCommand(text, con); this.cmd.ExecuteNonQuery(); this.CloseConnection(); MessageBox.Show("数据添加成功!"); } } }
public ModificationForm(MySqlData data) { InitializeComponent(); textBox4.Text = data.Area; textBox5.Text = data.Name; textBox6.Text = data.Price.ToString(); this.selected = data.Id; }
private void button1_Click(object sender, EventArgs e) { try { MySqlData data = new MySqlData(); data.Area = textBox1.Text; data.Name = textBox2.Text; data.Price = int.Parse(textBox3.Text); data.Id = this.selected; new MySqlDA().updateData(data); } catch { MessageBox.Show("Price必须为数字!请重新输入!"); } }
private void button3_Click(object sender, EventArgs e) { MySqlData data = new MySqlData(); if (listView1.SelectedItems.Count > 0) { if (MessageBox.Show( "请再次确定您是否要删除该数据!", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { data.Id = int.Parse(listView1.SelectedItems[0].SubItems[0].Text); new MySqlDA().deleteData(data.Id); refreshListView(); } } else { MessageBox.Show("请在表格中选择要删除的数据!"); } }
private void button4_Click(object sender, EventArgs e) { MySqlData data = new MySqlData(); if (listView1.SelectedItems.Count > 0) { //获得选中的数据 data.Id = int.Parse(listView1.SelectedItems[0].SubItems[0].Text); data.Area = listView1.SelectedItems[0].SubItems[1].Text; data.Name = listView1.SelectedItems[0].SubItems[2].Text; data.Price = float.Parse(listView1.SelectedItems[0].SubItems[3].Text); ModificationForm modification = new ModificationForm(data); modification.form1 = this; modification.Owner = this; modification.Show(); } else { MessageBox.Show("请先在表格中选择需要更改的数据!"); } }
//查 public List <MySqlData> queryData(string area) { List <MySqlData> list = new List <MySqlData>(); string text = "select * from nanning_city where area_name like '%" + area + "%';"; if (this.OpenConection() == true) { this.cmd = new MySqlCommand(text, con); MySqlDataReader mySqlDataReader = cmd.ExecuteReader(); while (mySqlDataReader.Read()) { MySqlData data = new MySqlData(); data.Id = int.Parse(mySqlDataReader["id"].ToString()); data.Area = mySqlDataReader["area_name"].ToString(); data.Name = mySqlDataReader["name"].ToString(); data.Price = float.Parse(mySqlDataReader["price"].ToString()); list.Add(data); } } this.CloseConnection(); return(list); }
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "") { try { MySqlData udata = new MySqlData(); udata.Area = textBox1.Text; udata.Name = textBox2.Text; udata.Price = int.Parse(textBox3.Text); new MySqlDA().addData(udata); } catch { MessageBox.Show("Price必须为数字!请重新输入!"); } } else { MessageBox.Show("输入不能为空!"); } }