实验八 访问数据库应用实例_第1页
实验八 访问数据库应用实例_第2页
实验八 访问数据库应用实例_第3页
实验八 访问数据库应用实例_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.实验八 访问数据库应用实例一. 实验目的:能够熟练运用某种高级语言进行数据库编程二. 实验内容:根据上次实验所使用的组件,编写一个访问数据库的程序,要求该程序具备查询、插入、删除、修改四个基本功能。三. 实验过程:(将程序的实验过程简单描述并将相关界面截图,关键代码附在文中)using System;using System.Data;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;namespace ConsoleApplication2 cla

2、ss Program static void Main(string args) string connstr = string.Format(server= LIUYANSQLEXPRESS;database=学生课程数据库;uid=login;pwd=123456); Console.WriteLine(-访问数据库系统-); Console.Write(-请输入操作:); int Operator = Convert.ToInt32(Console.ReadLine(); switch (Operator) case 1: Select(connstr); break; case 2:

3、Add(connstr); break; case 3: Delect(connstr); break; case 4: Update(connstr); break; default: break; static void Select(string s) SqlConnection conn = new SqlConnection(s); conn.Open(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; Console.Write(输入要查询的学生姓名:); string name = string.Forma

4、t(Console.ReadLine(); comm.CommandText = string.Format(select * from student where sname = 0,name); comm.CommandType = CommandType.Text; SqlDataReader sdr = comm.ExecuteReader(); while (sdr.Read() Console.WriteLine(学号; + sdr0.ToString(); Console.WriteLine(姓名; + sdr1.ToString(); Console.WriteLine(年龄;

5、 + sdr2.ToString(); Console.WriteLine(院系; + sdr3.ToString(); Console.WriteLine(生日; + sdr4.ToString(); conn.Dispose(); static void Add(string s) SqlConnection conn = new SqlConnection(s); conn.Open(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; Console.Write(输入要插入的学生学号:); string no =

6、string.Format(Console.ReadLine(); Console.Write(输入要插入的学生姓名:); string name = string.Format(Console.ReadLine(); Console.Write(输入要插入的学生年龄:); int age = Convert.ToInt32(Console.ReadLine(); Console.Write(输入要插入的学生系别:); string dept = string.Format(Console.ReadLine(); Console.Write(输入要插入的学生生日:); string birth

7、day = string.Format(Console.ReadLine(); comm.CommandText = string.Format(Insert Into student(sno,sname,sage,sdept,sbirthday) values(0,1,2,3,4),no,name,age,dept,birthday); comm.CommandType = CommandType.Text; if (comm.ExecuteNonQuery() 0) Console.WriteLine(成功添加学生信息!); else Console.WriteLine(添加学生信息失败!

8、); conn.Dispose(); static void Delect(string s) SqlConnection conn = new SqlConnection(s); conn.Open(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; Console.WriteLine(删除关于学号为95009的学生的记录); comm.CommandText = string.Format(delete from student where sno=95009); comm.CommandType = Command

9、Type.Text; if (comm.ExecuteNonQuery() 0) Console.WriteLine(成功删除学号为95009的学生记录!); conn.Dispose(); static void Update(string s) SqlConnection conn = new SqlConnection(s); conn.Open(); SqlCommand comm = new SqlCommand(); comm.Connection = conn; Console.WriteLine(修改张三学生的记录); comm.CommandText = string.Format(update student set sage=19 where sname=张三); comm.CommandType = CommandType.Text; if (comm.ExecuteNonQuery() 0) Console.WriteLin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论