-
数据库建立 建立TestSchool数据库并在此数据库下建立表格TblClassId -
建立Windows窗体应用Form1 -
创建TblClassIdl类,用以绑定数据
public class TblClassId
{
public int Id { get; set; }
public string Name { get; set; }
public string Desc { get; set; }
}
- 主窗体程序
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
List<TblClassId> list = new List<TblClassId>();
string constr = @"server=DESKTOP-FAVDBP3\MSSQLSERVER2014;user=123;pwd=123;database=TestSchool";
SqlConnection con = new SqlConnection(constr);
string sql = "select * from TblClassId";
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
TblClassId model = new TblClassId();
model.Id = reader.GetInt32(0);
model.Name = reader.GetString(1);
model.Desc = reader.IsDBNull(2) ? null : reader.GetString(2);
list.Add(model);
}
}
this.dgvClassId.DataSource = list;
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string cName = CName.Text.Trim();
string cDesc = CDesc.Text.Trim();
string constr = @"server=DESKTOP-FAVDBP3\MSSQLSERVER2014;user=123;pwd=123;database=TestSchool";
SqlConnection con = new SqlConnection(constr);
string sql = string.Format("insert into TblClassId values('{0}','{1}')", cName, cDesc);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int r = cmd.ExecuteNonQuery();
if (r > 0)
{
this.Text = "插入成功!";
LoadData();
}
else
{
this.Text = "插入了" + r + "行。!";
}
con.Close();
}
private void dgvClassId_RowEnter(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow currentRow = this.dgvClassId.Rows[e.RowIndex];
TblClassId model = currentRow.DataBoundItem as TblClassId;
if(model!=null)
{
CId.Text = model.Id.ToString();
EditName.Text = model.Name;
EditDesc.Text = model.Desc;
}
}
private void button2_Click(object sender, EventArgs e)
{
TblClassId model = new TblClassId();
model.Id = Convert.ToInt32(CId.Text);
model.Name =EditName.Text.Trim();
model.Desc =EditDesc.Text.Trim();
string constr = @"server=DESKTOP-FAVDBP3\MSSQLSERVER2014;user=123;pwd=123;database=TestSchool";
SqlConnection con = new SqlConnection(constr);
string sql = string.Format("update TblClassId set Name='{0}',[Desc]='{1}' where Id={2}",
model.Name,model.Desc,model.Id);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int r = cmd.ExecuteNonQuery();
this.Text = "更新了" + r + "行。";
LoadData();
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
DialogResult result= MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if(result==System.Windows.Forms.DialogResult.OK)
{
int Id = Convert.ToInt32(CId.Text);
string constr = @"server=DESKTOP-FAVDBP3\MSSQLSERVER2014;user=123;pwd=123;database=TestSchool";
SqlConnection con = new SqlConnection(constr);
string sql = string.Format("delete from TblClassId where Id={0}", Id);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int r = cmd.ExecuteNonQuery();
this.Text = "删除了" + r + "条数据。";
LoadData();
con.Close();
}
}
- 调试
|