找传奇、传世资源到传世资源站!

C#窗体增删改查(入门级数据库操作示例)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

含数据库文件 附加即可
from clipboard
from clipboard

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(); } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复