欢迎来到天天文库
浏览记录
ID:57099000
大小:171.00 KB
页数:5页
时间:2020-08-02
《实验八 访问数据库应用实例.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验八访问数据库应用实例一.实验目的:能够熟练运用某种高级语言进行数据库编程二.实验内容:根据上次实验所使用的组件,编写一个访问数据库的程序,要求该程序具备查询、插入、删除、修改四个基本功能。三.实验过程:(将程序的实验过程简单描述并将相关界面截图,关键代码附在文中)usingSystem;usingSystem.Data;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;namespaceConsoleApplication2{clas
2、sProgram{staticvoidMain(string[]args){stringconnstr=string.Format("server=LIUYAN\SQLEXPRESS;database=学生课程数据库;uid=login;pwd=123456");Console.WriteLine("----------访问数据库系统-----------");Console.Write("-----------请输入操作:");intOperator=Convert.ToInt32(Console.ReadLine());switch(Operator){case1:
3、Select(connstr);break;case2:Add(connstr);break;case3:Delect(connstr);break;case4:Update(connstr);break;default:break;}}staticvoidSelect(strings){SqlConnectionconn=newSqlConnection(s);conn.Open();SqlCommandcomm=newSqlCommand();comm.Connection=conn;Console.Write("输入要查询的学生姓名:");stringname=st
4、ring.Format(Console.ReadLine());comm.CommandText=string.Format("select*fromstudentwheresname='{0}'",name);comm.CommandType=CommandType.Text;SqlDataReadersdr=comm.ExecuteReader();while(sdr.Read()){Console.WriteLine("学号;"+sdr[0].ToString());Console.WriteLine("姓名;"+sdr[1].ToString());Console
5、.WriteLine("年龄;"+sdr[2].ToString());Console.WriteLine("院系;"+sdr[3].ToString());Console.WriteLine("生日;"+sdr[4].ToString());}conn.Dispose();}staticvoidAdd(strings){SqlConnectionconn=newSqlConnection(s);conn.Open();SqlCommandcomm=newSqlCommand();comm.Connection=conn;Console.Write("输入要插入的学生学号
6、:");stringno=string.Format(Console.ReadLine());Console.Write("输入要插入的学生姓名:");stringname=string.Format(Console.ReadLine());Console.Write("输入要插入的学生年龄:");intage=Convert.ToInt32(Console.ReadLine());Console.Write("输入要插入的学生系别:");stringdept=string.Format(Console.ReadLine());Console.Write("输入要插入的学
7、生生日:");stringbirthday=string.Format(Console.ReadLine());comm.CommandText=string.Format("InsertIntostudent(sno,sname,sage,sdept,sbirthday)values({0},'{1}',{2},'{3}',{4})",no,name,age,dept,birthday);comm.CommandType=CommandType.Text;if(comm.ExecuteNonQuery()>0){Consol
此文档下载收益归作者所有