本文实例为大家分享了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=数据库名;ur id=用户名;pwd=密码"; public form1() { initializecomponent(); } private void textbox5_textchanged(object nder, eventargs e) { } private void form1_load(object nder, eventargs e) { } private void label10_click(object nder, eventargs e) { } /// <summary> /// 添加学生信息档案 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void butadd_click(object nder, eventargs e) { //获取各个文本框的数据 string name = txtname.text; string x = txtx.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("@x", x), 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 = "inrt into students values(@name, @x, @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);//刷新数据 } el { messagebox.show("添加失败!"); } } el { messagebox.show("数据已经存在!"); } conn.clo();//关闭数据库 //application.exit();//关闭整个应用程序 } } /// <summary> /// 根据id值判断数据表students中是否存在这个人,存在返回fal,不存在返回true /// </summary> /// <param name="id"></param> /// <param name="conn"></param> /// <returns></returns> public bool existence(string id, sqlconnection conn) { string txtstr = string.format( "lect id from students where id = '{0}' " ,id);//定义一个数据库操作指令集 sqldataadapter sda = new sqldataadapter(txtstr, conn);//定义一个数据库适配器 datat ds = new datat();//定义数据集合 sda.fill(ds);//填充数据集合 datatable dt = ds.tables[0];//將数据集合中的第一张表赋值给datatable if(dt.rows.count > 0) //count > 0表示有数据 { return fal; } el { return true; } } /// <summary> /// 对数据库进行的动态查询,不管用户掌握的信息有多少都可以查询 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void btnlect_click(object nder, eventargs e) { //获取各个文本框的数据 string name = txtname.text; string x = txtx.text; string college = txtcollege.text; string id = txtid.text; string grade = txtgrade.text; string phone = txtphone.tadvertiment可数吗ext; 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("lect name, x, college, id, grade,phone, email, qq, room from students where 1=1"); //判断用户有没有给出其它的查询条件,有则添加进sql语句 if (name != "") { sb.appendformat(" and name = '{0}'", name); } if (x != "") { sb.appendformat(" and x = '{0}'", x); } 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); datat ds = new datat();//定义一个数据集合 adapter.fill(ds);//填充数据集合 datagridview1.datasource = ds.tables[0];//把数据集合绑定到datagridview上,datagridview会以表格的形式显示出来 conn.clo();//关闭数据库 } } /// <summary> 会计事务所实习心得 /// 修改学生信息 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void btnupdate_click(object nder, eventargs e) { //获取各个文本框的数据 string name = txtname.text; string x = txtx.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("@x", x), 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 t name = @name, x = @x, 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.clo();//关闭数据库 } } //sqldataadapter sda = new sqldataadapter(); } /// <summary> /// 刷新datagridview里的数据 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void form1_load_1(object nder, eventargs e) { using (var conn = new sqlconnection(this.str))//定义一个数据库连接实例 { conn.open();//打开数据库 string sql = "lect * from students";//定义一个数据库操作指令 sqldataadapter sda = new sqldataadapter(sql, conn);//定义数据库适配器 datat ds = new datat();//定义数据集合 sda.fill(ds);//填充数据集合 datagridview1.datasource = ds.tables[0]; conn.clo();//关闭数据库 } } private void datagridview1_cellclick(object nder, datagridviewcelleventargs e) { } /// <summary> /// 选中datagridview的行,这一行的数据返回到对应的文本框 /// </summary>大专以上学历; /// <param name="nder"></param> /// <param name="e"></param> private void datagridview1_cellclick_1(object nder, datagridviewcelleventargs e) { //lectedrows[0] 获取用户选定行的集合(选中的第一行就是0,一次类推) //cells["name"] 获取用于填充行的单元格集合(就是列) .value就是它的值,最后tostring转字符串 txtname.text = datagridview1.lectedrows[0].cells["name"].value.tostring(); txtx.text = datagridview1.lectedrows[0].cells["x"].value.tostring(); txtcollege.text = datagridview1.lectedrows[0].cells["college"].value.tostring(); txtid.text = datagri中国四大名校dview1.lectedrows[0].cells["id"].value.tostring(); txtgrade.text = datagridview1.lectedrows[0].cells["grade"].value.tostring(); txtphone.text = datagridview1.lectedrows[0].cells["phone"].value.tostring(); txtemail.text = datagridview1.lectedrows[0].cells["email"].value.tostring(); txtqq.text = datagridview1.lectedrows[0].cells["qq"].value.tostring(); txtroom.text = datagridview1.lectedrows[0].ce如何画正方体lls["room"].value.tostring(); } /// <summary> /// 删除某个学生的所有数据 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void btndelete_click(object nder, 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.clo();//关闭数据库 } } } /// <summary> /// 对文本框进行清空处理,方便重新输入下一个学生信息 /// </summary> /// <param name="nder"></param> /// <param name="e"></param> private void btnclear_click(object nder, eventargs e) { txtname.text = null; txtx.text = null; txtcollege.text = null; txtid.text = null; txtgrade.text = null; txtphone.text = null; txtemail.text = null; txtqq.text = null; txtroom.text = null; } }}
students表
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 18:07:59,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/75248daed226b277dc6e87410a5d40c8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#实现学生模块的增删改查.doc
本文 PDF 下载地址:C#实现学生模块的增删改查.pdf
留言与评论(共有 0 条评论) |