实例介绍
【实例简介】 含数据库文件 附加即可
【实例简介】
【核心代码】
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 System.Data.SqlClient; namespace 增删改查数据窗体 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LoadData(); } public void LoadData() { List<Employ> list = new List<Employ>(); string constr = "data source=.;initial catalog=myfirstdatabase;integrated security=true"; using (SqlConnection con = new SqlConnection(constr)) { string sql = "select * from 员工表"; using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); using (SqlDataReader Reader = cmd.ExecuteReader()) { if (Reader.HasRows) { while (Reader.Read()) { Employ newemploy = new Employ(); newemploy.ID = Reader.GetInt32(0); newemploy.Name = Reader.IsDBNull(1) ? null : Reader.GetString(1); newemploy.Gender = Reader.IsDBNull(2) ? null : Reader.GetString(2); newemploy.Age = Reader.GetInt32(3); newemploy.School = Reader.IsDBNull(4) ? null : Reader.GetString(4); newemploy.Address = Reader.IsDBNull(5) ? null : Reader.GetString(5); newemploy.Salary = Reader.GetDecimal(6); list.Add(newemploy); //把一条条记录封装成对象,添加到list集合中 } } else { Console.WriteLine("没读到任何数据!!!"); } } } } //数据绑定只认属性,不认字段。内部通过反射来实现 DataGridView1.DataSource = list; } //插入一条新的记录 private void button1_Click(object sender, EventArgs e) { #region 版本一 显示插入数据成功 //bool flag; ////采集数据 //string name = txtname.Text.Trim(); //string gender = txtgender.Text.Trim(); //string age = txtage.Text.Trim(); //string school = txtschool.Text.Trim(); //string address = txtaddress.Text.Trim(); //string salary = txtsalary.Text.Trim(); //string constr = "data source=.;initial catalog=myfirstdatabase;integrated security=true"; //using (SqlConnection con = new SqlConnection(constr)) //{ // string sql = string.Format("insert into 员工表 values('{0}','{1}','{2}','{3}','{4}','{5}')", name, gender, age, school, address, salary); // using (SqlCommand cmd = new SqlCommand(sql, con)) // { // con.Open(); // int r = cmd.ExecuteNonQuery(); // if (r > 0) // { // flag = true; // } // else // { // flag = false; // } // } //} //if (flag) //{ // MessageBox.Show("插入成功,编号为");//放到后面 不会阻塞进程,阻碍using 的进行 // LoadData(); //刷新,重新加载 //} //else //{ // MessageBox.Show("插入失败"); //} #endregion #region 版本二 显示插入成功并返回插入记录的编号 int id; //采集数据 string name = txtname.Text.Trim(); string gender = txtgender.Text.Trim(); string age = txtage.Text.Trim(); string school = txtschool.Text.Trim(); string address = txtaddress.Text.Trim(); string salary = txtsalary.Text.Trim(); string constr = "data source=.;initial catalog=myfirstdatabase;integrated security=true"; using (SqlConnection con = new SqlConnection(constr)) { string sql = string.Format("insert into 员工表 output inserted.员工编号 values('{0}','{1}','{2}','{3}','{4}','{5}')", name, gender, age, school, address, salary); using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); id = (int)cmd.ExecuteScalar(); } } MessageBox.Show(string.Format("插入成功,插入的编号是{0}", id));//放到后面 不会阻塞进程,阻碍using 的进行 LoadData(); //刷新,重新加载 #endregion } //删除数据 private void button3_Click(object sender, EventArgs e) { bool flag; //采集用户输入 Employ model = new Employ(); model.ID = Convert.ToInt32(IDlabel.Text); string constr = "data source=.;initial catalog=myfirstdatabase;integrated security=true"; using (SqlConnection con = new SqlConnection(constr)) { string sql = string.Format("delete from 员工表 where 员工编号={0}", model.ID); //只有字符串类型两边需要加单引号 using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); int r = cmd.ExecuteNonQuery(); if (r > 0) { flag = true; } else { flag = false; } } } if (flag) { MessageBox.Show("删除数据成功!"); LoadData(); } else { MessageBox.Show("删除数据失败!"); } } //更新数据 private void button2_Click(object sender, EventArgs e) { bool flag; //采集用户输入 Employ model = new Employ(); model.ID = Convert.ToInt32(IDlabel.Text); model.Name = txtname2.Text; model.Gender = txtgender2.Text; model.Age = txtage2.Text.Trim().Length == 0 ? null : (int?)Convert.ToInt32(txtage2.Text.Trim()); model.School = txtschool2.Text.Trim().Length == 0 ? null : txtschool2.Text; model.Address = txtaddress2.Text; model.Salary = Convert.ToDecimal(txtsalary2.Text); string constr = "data source=.;initial catalog=myfirstdatabase;integrated security=true"; using (SqlConnection con = new SqlConnection(constr)) { string sql = string.Format("update 员工表 set 姓名='{0}',性别='{1}',年龄='{2}',毕业院校='{3}',家庭住址='{4}',工资='{5}'where 员工编号={6}", model.Name, model.Gender, model.Age, model.School, model.Address, model.Salary, model.ID); using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); int r = cmd.ExecuteNonQuery(); if (r > 0) { flag = true; } else { flag = false; } } } if (flag) { MessageBox.Show("更新记录成功!"); LoadData(); } else { MessageBox.Show("更新记录失败!"); } } //行获取焦点事件 private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { //获取当前选中的行对象 DataGridViewRow currentRow = this.DataGridView1.Rows[e.RowIndex]; //获取当前行中绑定的employ数据对象,即每一行就是employ类的一个对象 Employ model = currentRow.DataBoundItem as Employ; if (model != null)//转换成功 { IDlabel.Text = model.ID.ToString(); txtname2.Text = model.Name; txtgender2.Text = model.Gender; txtage2.Text = model.Age.ToString(); txtschool2.Text = model.School; txtaddress2.Text = model.Address; txtsalary2.Text = model.Salary.ToString(); } } } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论