实验八_访问数据库应用实例答案.doc
文本预览下载声明
实验八 访问数据库应用实例
实验目的:能够熟练运用某种高级语言进行数据库编程
实验内容:根据上次实验所使用的组件,编写一个访问数据库的程序,要求该程序具备查询、插入、删除、修改四个基本功能。
实验过程:(将程序的实验过程简单描述并将相关界面截图,关键代码附在文中)
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string connstr = string.Format(server= LIUYAN\\SQLEXPRESS;database=学生课程数据库;uid=login;pwd=123456);
Console.WriteLine(----------访问数据库系统-----------);
Console.Write(-----------请输入操作:);
int Operator = Convert.ToInt32(Console.ReadLine());
switch (Operator)
{
case 1:
Select(connstr); break;
case 2:
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.Format(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(学号; + sdr[0].ToString());
Console.WriteLine(姓名; + sdr[1].ToString());
Console.WriteLine(年龄; + sdr[2].ToString());
Console.WriteLine(院系; + sdr[3].ToString());
Console.WriteLine(生日; + sdr[4].ToString());
}
conn.Dispose();
}
static void Add(string s)
显示全部