C#实现学生模块的增删改查
程序员文章站
2022-06-10 09:17:58
本文实例为大家分享了c#实现学生模块的增删改查的具体代码,供大家参考,具体内容如下using system;using system.collections.generic;using system....
本文实例为大家分享了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) { } /// <summary> /// 添加学生信息档案 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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();//关闭整个应用程序 } } /// <summary> /// 根据id值判断数据表students中是否存在这个人,存在返回false,不存在返回true /// </summary> /// <param name="id"></param> /// <param name="conn"></param> /// <returns></returns> 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];//將数据集合中的第一张表赋值给datatable if(dt.rows.count > 0) //count > 0表示有数据 { return false; } else { return true; } } /// <summary> /// 对数据库进行的动态查询,不管用户掌握的信息有多少都可以查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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();//关闭数据库 } } /// <summary> /// 修改学生信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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(); } /// <summary> /// 刷新datagridview里的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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) { } /// <summary> /// 选中datagridview的行,这一行的数据返回到对应的文本框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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(); } /// <summary> /// 删除某个学生的所有数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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();//关闭数据库 } } } /// <summary> /// 对文本框进行清空处理,方便重新输入下一个学生信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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#基于DBContext(EF)实现通用增删改查的REST方法实例
-
Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
-
C#基于DBContext(EF)实现通用增删改查的REST方法实例
-
Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
-
C#如何实现对sql server数据库的增删改查
-
C#如何实现对sql server数据库的增删改查
-
C#实现XML文档的增删改查功能示例
-
关于学生对象的增删改查的“改”(网页界面,bootstrap)(续,使用模态窗的修改操作)第二种实现方式(JavaScript DOM操作)
-
C#实现学生模块的增删改查
-
C#实现XML文档的增删改查功能的示例代码分享