1.界面
2.创建类
class TbClass
{
public int ClassID { get; set; }
public string ClassName { get; set; }
public string ClassDesc{get;set;}
}
3.窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MDI
{
public partial class ClassManger : Form
{
public ClassManger()
{
InitializeComponent();
}
private void ClassManger_Load(object sender, EventArgs e)
{
//将TbClass表中的数据读取到一个List<T>中
LoadData();
}
private void LoadData()
{
List<TbClass> list = new List<TbClass>();
string constr = "Database=ticket;Data Source=localhost;Port=3306;UserId=root;Password=root;Charset=utf8;pooling=true;";
using (MySqlConnection con = new MySqlConnection(constr))
{
string sql = "select * from TbClass";
using (MySqlCommand cmd = new MySqlCommand(sql,con))
{
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
//判断是否查询了数据
if (reader.HasRows)
{
//一条一条的读取数据
while(reader.Read())
{
TbClass model = new TbClass();
model.ClassID = reader.GetInt32(0);
model.ClassName = reader.IsDBNull(1) ? null : reader.GetString(1);
model.ClassDesc =reader.IsDBNull(2) ? null: reader.GetString(2);
list.Add(model);//把model对象加到list集合中
}
}
}
}
}
//数据绑定需要注意的一点:
//数据绑定的时候,只认"属性”,不认”字段”。内部通过反射来实现。
this.dataGridView1.DataSource = list;//数据绑定
}
//增加一条数据
private void btnAdd_Click(object sender, EventArgs e)
{
//1.采集用户的输入
string className = txtClassName.Text.Trim();
string classDesc = txtDesc.Text.Trim();
//执行插入操作
string constr = "Database=ticket;Data Source=localhost;Port=3306;UserId=root;Password=root;Charset=utf8;pooling=true;";
using (MySqlConnection con = new MySqlConnection(constr))
{
string sql = string.Format("insert into tbclass(className,classDesc) values(N'{0}',N'{1}')",className,classDesc);
using (MySqlCommand cmd = new MySqlCommand(sql, con))
{
con.Open();
int r=cmd.ExecuteNonQuery();
if (r > 0)
{
this.Text = "插入成功";
LoadData();
}
else
{
this.Text = "操作失败";
}
}
}
{
}
}
//行获取焦点事件
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
//获取当前选中行
DataGridViewRow currentRow = this.dataGridView1.Rows[e.RowIndex];
//获取当前选中行绑定的TbClass数据对象
TbClass model=currentRow.DataBoundItem as TbClass;
if (model != null)
{
lblID.Text = model.ClassID.ToString();
txtEditClass.Text = model.ClassName;
txtEditDesc.Text = model.ClassDesc;
}
}
//编辑
private void btnEdit_Click(object sender, EventArgs e)
{
//采集用户输入
TbClass model = new TbClass();
model.ClassID = Convert.ToInt32(lblID.Text);
model.ClassName = txtEditClass.Text.Trim();
model.ClassDesc = txtEditDesc.Text.Trim();
//连接数据库,执行编辑操作
string constr = "Database=ticket;Data Source=localhost;Port=3306;UserId=root;Password=root;Charset=utf8;pooling=true;";
using (MySqlConnection con = new MySqlConnection(constr))
{
string sql = string.Format("update tbclass set className='{0}',classDesc='{1}' where classId='{2}' ",model.ClassName,model.ClassDesc,model.ClassID);
using(MySqlCommand cmd=new MySqlCommand(sql,con)){
con.Open();
int r = cmd.ExecuteNonQuery();
this.Text="更新了"+r+"行。";
//重新加载,绑定DataGridView
LoadData();
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
MessageBox.Show("确定要删除嘛?","操作提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
int classID = Convert.ToInt32(lblID.Text);
string constr = "Database=ticket;Data Source=localhost;Port=3306;UserId=root;Password=root;Charset=utf8;pooling=true;";
using(MySqlConnection con=new MySqlConnection(constr))
{
string sql = string.Format("delete from tbclass where classId='{0}'",classID);
using (MySqlCommand cmd = new MySqlCommand(sql,con))
{
con.Open();
int r= cmd.ExecuteNonQuery();
this.Text = "操作成功";
//重新加载数据
LoadData();
}
}
}
}
}