C#实现学生模块的增删改查
本文实例为大家分享了C#实现学生模块的增删改查的具体代码,供大家参考,具体内容如下
文章图片
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace text3_CRUD{public partial class Form1 : Form{//把连接数据库的字符串提取出来,就不用每次都要写,增加代码复用性private string str = "data source=本地IP; initial catalog=数据库名; user ID=用户名; pwd=密码"; public Form1(){InitializeComponent(); }private void TextBox5_TextChanged(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){}private void Label10_Click(object sender, EventArgs e){}/// /// 添加学生信息档案/// /// /// private void ButAdd_Click(object sender, EventArgs e){//获取各个文本框的数据string name = txtname.Text; string sex = txtsex.Text; string college = txtcollege.Text; string id = txtid.Text; string grade = txtgrade.Text; string phone = txtphone.Text; string email = txtemail.Text; string qq = txtqq.Text; string room = txtroom.Text; using (var conn = new SqlConnection(this.str))//定义一个数据库连接实例{conn.Open(); //打开数据库Form1 f = new Form1(); //实例化Form1窗体对象if (f.Existence(id, conn))//检查数据库 存不存在此条记录,存在则插入{SqlParameter[] para = new SqlParameter[]//构建存储过程的输入参数{new SqlParameter("@name",name),new SqlParameter("@sex", sex),new SqlParameter("@college", college),new SqlParameter("@id", id),new SqlParameter("@grade", grade),new SqlParameter("@phone", phone),new SqlParameter("@email", email),new SqlParameter("@qq", qq),new SqlParameter("@room", room),}; string sql = "insert into Students values(@name, @sex, @college, @id, @grade, @phone, @email, @qq, @room); "; //定义一个数据库操作指令集SqlCommand com = new SqlCommand(sql, conn); //执行数据库操作指令com.Parameters.AddRange(para); //將参数和命令对象的参数集合绑定int result = (int)com.ExecuteNonQuery(); //针对Connection执行的SQL语句,返回受影响的行数,result > 0则表示SQL语句执行成功if (result > 0){MessageBox.Show("添加成功!"); //弹窗显示“添加成功” this.Form1_Load_1(null, null); //刷新数据}else{MessageBox.Show("添加失败!"); }}else{MessageBox.Show("数据已经存在!"); }conn.Close(); //关闭数据库//Application.Exit(); //关闭整个应用程序}}/// /// 根据ID值判断数据表Students中是否存在这个人,存在返回false,不存在返回true/// /// /// ///public bool Existence(string id, SqlConnection conn){string txtStr = string.Format( "select id from Students where id = '{0}' " ,id); //定义一个数据库操作指令集SqlDataAdapter sda = new SqlDataAdapter(txtStr, conn); //定义一个数据库适配器DataSet ds = new DataSet(); //定义数据集合sda.Fill(ds); //填充数据集合DataTable dt = ds.Tables[0]; //將数据集合中的第一张表赋值给DataTableif(dt.Rows.Count > 0)//count > 0表示有数据{return false; }else{return true; }}/// /// 对数据库进行的动态查询,不管用户掌握的信息有多少都可以查询/// /// /// private void BtnSelect_Click(object sender, EventArgs e){//获取各个文本框的数据string name = txtname.Text; string sex = txtsex.Text; string college = txtcollege.Text; string id = txtid.Text; string grade = txtgrade.Text; string phone = txtphone.Text; string email = txtemail.Text; string qq = txtqq.Text; string room = txtroom.Text; using(var conn = new SqlConnection(this.str))//定义一个数据库连接实例{conn.Open(); //打开数据库StringBuilder sb = new StringBuilder(); //创建一个字符串变量sb.Append("select name, sex, college, id, grade,phone, email, qq, room from Students where 1=1"); //判断用户有没有给出其它的查询条件,有则添加进sql语句if (name != ""){sb.AppendFormat(" and name = '{0}'", name); }if (sex != ""){sb.AppendFormat(" and sex = '{0}'", sex); }if (college != ""){sb.AppendFormat(" and college = '{0}'", college); }if (id != ""){sb.AppendFormat(" and id = '{0}'", id); }if (grade != ""){sb.AppendFormat(" and grade = '{0}'", grade); }if (phone != ""){sb.AppendFormat(" and phone = '{0}'", phone); }if (email != ""){sb.AppendFormat(" and email = '{0}'", email); }if (qq != ""){sb.AppendFormat(" and qq = '{0}'", qq); }if (room != ""){sb.AppendFormat(" and room = '{0}'", room); }string sql = sb.ToString(); SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); //定义一个数据集合adapter.Fill(ds); //填充数据集合dataGridView1.DataSource = ds.Tables[0]; //把数据集合绑定到dataGridView上,dataGridView会以表格的形式显示出来conn.Close(); //关闭数据库}}/// /// 修改学生信息/// /// /// private void BtnUpdate_Click(object sender, EventArgs e){//获取各个文本框的数据string name = txtname.Text; string sex = txtsex.Text; string college = txtcollege.Text; string id = txtid.Text; string grade = txtgrade.Text; string phone = txtphone.Text; string email = txtemail.Text; string qq = txtqq.Text; string room = txtroom.Text; //构建存储过程的输入参数SqlParameter[] para = new SqlParameter[]{new SqlParameter("@name",name),new SqlParameter("@sex", sex),new SqlParameter("@college", college),new SqlParameter("@id", id),new SqlParameter("@grade", grade),new SqlParameter("@phone", phone),new SqlParameter("@email", email),new SqlParameter("@qq", qq),new SqlParameter("@room", room)}; using(var conn = new SqlConnection(this.str)){conn.Open(); //打开数据库;string sql = "update Students set name = @name, sex = @sex, college = @college, id = @id, grade = @grade, phone = @phone, email = @email, qq = @qq, room = @room where id = @id"; SqlCommand com = new SqlCommand(sql, conn); //执行数据库操作指令com.Parameters.AddRange(para); //将参数和命令对象的参数集合绑定int result = (int)com.ExecuteNonQuery(); //查询返回的第一行第一列if(result > 0){MessageBox.Show("修改成功!"); this.Form1_Load_1(null, null); //修改完数据后,重新刷新属性Form1窗口,以查看变化的内容conn.Close(); //关闭数据库}}//SqlDataAdapter sda = new SqlDataAdapter(); }/// /// 刷新DataGridView里的数据/// /// /// private void Form1_Load_1(object sender, EventArgs e){using (var conn = new SqlConnection(this.str))//定义一个数据库连接实例{conn.Open(); //打开数据库string sql = "select * from Students"; //定义一个数据库操作指令SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //定义数据库适配器DataSet ds = new DataSet(); //定义数据集合sda.Fill(ds); //填充数据集合dataGridView1.DataSource = ds.Tables[0]; conn.Close(); //关闭数据库}}private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){}/// /// 选中DataGridView的行,这一行的数据返回到对应的文本框/// /// /// 【C#实现学生模块的增删改查】private void DataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e){//SelectedRows[0] 获取用户选定行的集合(选中的第一行就是0,一次类推)//Cells["name"] 获取用于填充行的单元格集合(就是列) .Value就是它的值,最后ToString转字符串txtname.Text = dataGridView1.SelectedRows[0].Cells["name"].Value.ToString(); txtsex.Text = dataGridView1.SelectedRows[0].Cells["sex"].Value.ToString(); txtcollege.Text = dataGridView1.SelectedRows[0].Cells["college"].Value.ToString(); txtid.Text = dataGridView1.SelectedRows[0].Cells["id"].Value.ToString(); txtgrade.Text = dataGridView1.SelectedRows[0].Cells["grade"].Value.ToString(); txtphone.Text = dataGridView1.SelectedRows[0].Cells["phone"].Value.ToString(); txtemail.Text = dataGridView1.SelectedRows[0].Cells["email"].Value.ToString(); txtqq.Text = dataGridView1.SelectedRows[0].Cells["qq"].Value.ToString(); txtroom.Text = dataGridView1.SelectedRows[0].Cells["room"].Value.ToString(); }/// /// 删除某个学生的所有数据/// /// /// private void BtnDelete_Click(object sender, EventArgs e){using(var conn = new SqlConnection(this.str))//创建一个数据库连接实例{conn.Open(); //连接数据库string sql = string.Format("delete from Students where id = '{0}'", txtid.Text); //往数据库操作指令中传值//如果传的值不是很多的话,就用这种方法;如果有很多就用SqlParameter[]SqlCommand com = new SqlCommand(sql, conn); //执行数据库删除指令int result = (int)com.ExecuteNonQuery(); //返回结果,result > 0则为修改成功if(result > 0){MessageBox.Show("删除成功!"); this.Form1_Load_1(null, null); //刷新数据conn.Close(); //关闭数据库}}}/// /// 对文本框进行清空处理,方便重新输入下一个学生信息/// /// /// private void BtnClear_Click(object sender, EventArgs e){txtname.Text = null; txtsex.Text = null; txtcollege.Text = null; txtid.Text = null; txtgrade.Text = null; txtphone.Text = null; txtemail.Text = null; txtqq.Text = null; txtroom.Text = null; }}}
Students表
文章图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- c#使用Unity粒子实现炮塔发射系统
- python如何实现网络测试,了解一下speedtest-cli...
- Android 使用Gallery组件实现图片播放预览
- Android使用Intent实现拨打电话的动作
- 投稿|RNG战队LPL春季赛夺冠,中国电竞产业未来如何实现“破与立”?
- android:Notification实现状态栏的通知
- vue实现实时上传文件进度条
- 用c语言实现一个电话薄(附完整代码)
- 如何在iOS 15中更改应用程序图标(实现方法和技巧指南)
- Python教程(time 与 datetime 模块使用方法)