2013 年华南理工大学计算机学院硕士研究生
入学复试数据库上机题
考试时间:3 月 25 日下午 14:00-16:30
姓名 报考专业 得分
注意事项: 10’
1.创建文件夹“d:\研究生复试\[你的中文姓名]\”
例: 张三,应创建“d:\研究生复试\张三”文件夹。
所有文档和答案都放在这个文件夹中。
2.在文件夹中建立一个 readme 文件(.txt 或.doc 均可),以说明所
用的软件工具。
3.设计文档,文件名为 orders.doc
数据库连接说明:如用户名,密码,ODBC/ JDBC 等数据源配置等
(数据库连接方式及配置参数)
如果必要,可以说明运行方式和相关参数
如果功能不能通过运行,则给出相应的源代码。
4.在你的目录中建立 SOURCE 目录,系统源文件放在该目录下
5.考完后请不要关机,人离开就可以了。
一、 数据库设计 30’
建立数据库,并建立以下各表
一个教师可以教授多门课,一门课可由多个教师教授
TEACHERS(教师号,教师名,教师性别)
T01 张三 男
T02 李四 女
T03 王五 男
T04 赵六 女
WORKS(教师号,课程号,年份,学期)
T01 C01 2010 春
T01 C02 2010 秋
T02 C01 2011 春
T03 C01 2011 秋
T04 C02 2011 春
T04 C03 2010 春
T04 C04 2012 秋
COURSES(课程号,课程名,学时)
C01 数据结构 64
C02 操作系统 64
C03 网络原理 48
C04 数据库 48
1、在数据库中根据上述表的定义创建上述三张表,同时需建立主键
和外键关系
2、将上面的数据输入到数据库中相应的表中
3、将数据库备份到你的文件夹下,命名为 backupInfo
二、数据库编程 60’
基于上述数据库,请使用 sql server2005+vs2008 完成教师信息管理系
统,并生成相应的可运行文件(文件名为你的名字),具体要求如下:
1. 要求程序与数据库能进行有效连接,并具有完善的人机交互界面,
要求有参数输入界面和执行按钮,在界面上有结果输出展现区,
要求不要把所有操作全部集中在一个菜单内。
2. 完成对 TEACHERS 表的添加,删除,修改和浏览四项功能。老师的
性别要求用单选按钮实现。(15 分)
3. 统计和查询:
(1)根据老师号或老师名查找老师的课程,老师号或老师名要求
不能文本输入,要求使用下拉菜单实现,并与数据库中现有信息
一致(15 分)
(2)通过年份查询,显示该年份老师的教授课程,学时和年份;
年份要求使用下拉菜单实现,并与数据库中现有信息一致(这个
好像没有,不太清楚了)(10 分)
(3)查询所有授课课程至少两门以上的老师的名字和课程名字。(10 分)
4. 具有数据完整性校验功能,当出现数据异常和操作异常时,程序
应给出清楚完整的异常提示信息。(10 分)
DBUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace SCUT2013Demo
{
class DBUtils
{
private DBUtils()
{
}
//饿汉式
static String strCon = @"data source=.\sqlexpress;initial catalog=Teacher2013;integrated security=true";
static SqlConnection con = new SqlConnection(strCon);
public static SqlConnection getCon()
{
if(con.State==ConnectionState.Closed) con.Open();
return con;
}
}
}
Teachers.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SCUT2013Demo
{
public class Teacher
{
private String TNo = "";
private String TName = "";
private String TSex = "";
public void setTNo(String TNo)
{
this.TNo = TNo;
}
public String getTNo()
{
return TNo;
}
public void setTName(String TName)
{
this.TName = TName;
}
public String getTName()
{
return TName;
}
public void setTSex(String TSex)
{
this.TSex = TSex;
}
public String getTSex()
{
return TSex;
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SCUT2013Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.GridLines = true;
dataGridView1.ReadOnly = true;
dataGridView2.ReadOnly = true;
dataGridView3.ReadOnly = true;
}
String sql = "";
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage1) //点击教师表选项时
{
Form1_Load(null, null);
}
else if (tabControl1.SelectedTab == tabPage2)
{
comboBox1.Items.Clear();
comboBox2.Items.Clear();
sql = "select TNo,TName from TEACHERS";
DataTable tables=execute(sql, new Teacher());
//把信息添加到combox中去
for (int i = 0; i < tables.Rows.Count; i++)
{
comboBox1.Items.Add(tables.Rows[i][0]);
comboBox2.Items.Add(tables.Rows[i][1]);
}
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
}
else if (tabControl1.SelectedTab == tabPage3)
{
comboBox3.Items.Clear();
sql = "select distinct year from works";
DataTable tables = execute(sql, new Teacher());
//把信息添加到combox中去
for (int i = 0; i < tables.Rows.Count; i++)
{
comboBox3.Items.Add(tables.Rows[i][0]);
}
comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox3.SelectedIndex = 0;
}
else if (tabControl1.SelectedTab == tabPage4)
{
sql = "select TName as 教师名, CName as 课程名 from teachers inner join works on teachers.TNo=works.TNo inner join courses on works.CNo=courses.CNo inner join (select TNo from works group by TNo having COUNT(TNo)>1) as t1 on teachers.TNo=t1.TNo";
DataTable tables = execute(sql, new Teacher());
dataGridView3.DataSource = tables;
}
}
//初始化显示所有的教师信息
private void Form1_Load(object sender, EventArgs e)
{
sql = "select TNo as 教师编号,TName as 教师姓名,TSex as 性别 from teachers";
DataTable table= execute(sql,new Teacher());
updateListView(table, listView1);
}
private DataTable execute(string sql,Teacher teacher)
{
SqlParameter spTNo = null;
SqlParameter spTName = null;
SqlParameter spTSex = null;
if (!"".Equals(teacher.getTNo()))
{
sql += " and TNo=@TNo ";
spTNo = new SqlParameter("@TNo", teacher.getTNo());
}
if (!"".Equals(teacher.getTName()))
{
sql += " and TName=@TName ";
spTName = new SqlParameter("@TName", teacher.getTName());
}
if (!"".Equals(teacher.getTSex()))
{
sql += " and TSex=@TSex ";
spTSex = new SqlParameter("@TSex", teacher.getTSex());
}
//执行sql语句
SqlConnection con = DBUtils.getCon();
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
//添加参数
if (spTNo != null)
sda.SelectCommand.Parameters.Add(spTNo);
if (spTName != null)
sda.SelectCommand.Parameters.Add(spTName);
if (spTSex != null)
sda.SelectCommand.Parameters.Add(spTSex);
sda.Fill(ds, "Teacher2013");
DataTable table= ds.Tables["Teacher2013"];
return table;
}
private void updateListView(DataTable table, ListView lv)
{
lv.Clear();
//把结果显示到屏幕
for (int i = 0; i < table.Columns.Count; i++)
{
ColumnHeader header = new ColumnHeader();
header.Text = table.Columns[i].ToString();
header.Width = 100;
listView1.Columns.Add(header);
}
for (int i = 0; i < table.Rows.Count; i++)
{
ListViewItem item = new ListViewItem(table.Rows[i][0].ToString());
for (int j = 1; j < table.Columns.Count; j++)
{
item.SubItems.Add(table.Rows[i][j].ToString());
}
lv.Items.Add(item);
}
}
//查询
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2("查询教师信息");
DialogResult dr= f2.ShowDialog();
if (dr == DialogResult.OK)
{
sql = "select TNo as 教师编号,TName as 教师姓名,TSex as 性别 from teachers where 1=1 ";
updateListView(execute(sql, f2.teacher), listView1);
}
}
//修改 未完全实现功能
private void button3_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
listView1.SelectedItems.Clear();
}
else
{
MessageBox.Show("请选择一行数据进行修改!");
}
}
private void button5_Click(object sender, EventArgs e)
{
String TNo = comboBox1.SelectedItem.ToString();
sql = "select teachers.TNo as 教师号,TName as 教师名, CName as 课程名 from teachers inner join works on teachers.TNo=works.TNo inner join courses on works.CNo=courses.CNo where teachers.TNo='"+TNo+"'";
dataGridView1.DataSource= execute(sql, new Teacher());
}
private void button6_Click(object sender, EventArgs e)
{
String TName = comboBox2.SelectedItem.ToString();
sql = "select teachers.TNo as 教师号,TName as 教师名, CName as 课程名 from teachers inner join works on teachers.TNo=works.TNo inner join courses on works.CNo=courses.CNo where teachers.TName='" + TName + "'";
dataGridView1.DataSource = execute(sql, new Teacher());
}
private void button7_Click(object sender, EventArgs e)
{
sql = "select TName as 教师名, CName as 课程名,CTime as 学时 from teachers inner join works on teachers.TNo=works.TNo inner join courses on works.CNo=courses.CNo where works.year='" + comboBox3.SelectedItem.ToString() + "'";
dataGridView2.DataSource = execute(sql, new Teacher());
}
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SCUT2013Demo
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string p)
{
InitializeComponent();
this.Text = p;
radioButton1.Checked = true;
}
public Teacher teacher;
private void button1_Click(object sender, EventArgs e)
{
String TNo = textBox1.Text;
String TName = textBox2.Text;
String TSex = radioButton1.Checked ? "男" : "女";
teacher = new Teacher();
teacher.setTNo(TNo);
teacher.setTName(TName);
teacher.setTSex(TSex);
this.DialogResult = DialogResult.OK;
this.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Dispose();
}
}
}
运行结果