华南理工2015复试数据库(C#)

题目内容借鉴博文:【C#】华南理工大学计算机考研复试题目

【原题】

注意事项:

1、创建文件夹“d:\研究生复试\[你的中文姓名]\” 例: 张三,应创建“d:\研究生复试\张三”文件夹。 所有文档和答案都放在这个文件夹中。

2、在文件夹中建立一个readme文件(.txt或.doc均可),以说明所用的软件工具。

3、设计文档,文件名为Info.doc,数据库连接说明:如用户名,密码,ODBC/JDBC等数据源配置等(数据库连接方式及配置参数)如果必要,可以说明运行方式和相关参数 如果功能不能通过运行,则给出相应的源代码。

4、在你的目录中建立SOURCE目录,系统源文件放在该目录下

5、考完后请不要关机,人离开就可以了。
 

一、数据库设计

建立数据库,并建立以下各表, 一个员工可以到多个不同公司上班。

要求:1、按照需求描述,建立表格,设置主键、外键约束。

            2、将数据输入相应的表中。

            3、数据库进行备份,备份文件存放在你的目录,文件名为backupInfo。

员工关系表EMPLOYEE(员工号EmpNo,员工姓名EmpName,性别EmpSex,年龄EmpAge)

华南理工2015复试数据库(C#)_第1张图片

工作关系表WORKS(EmpNo员工号,CmpNo公司号,Salary薪水)

华南理工2015复试数据库(C#)_第2张图片

公司关系表COMPANY(CmpNo公司号,CmpName公司名)

 

二、数据库编程

基于上述数据库,请使用sql server2005+vs2008或vs2010完成员工信息管理系统,并生成相应的可运行文件(文件名为你的名字),具体要求如下:

1. 要求程序与数据库能进行有效连接,并具有完善的人机交互界面, 要求有参数输入界面和执行按钮,在界面上有结果输出展现区, 要求不要把所有操作全部集中在一个菜单内。

2.完成对员工关系表的添加,删除,修改和浏览四项功能。老师的性别要求用单选按钮实现。(15分)

3.统计和查询:

(1)根据员工号或员工名查找员工所在的公司名和工资,员工号或员工名不能文本输入,要求使用下拉菜单实现,并与数据库中现有信息一致(10分)

(2)统计年龄至少为40岁员工的总工资,工资按从大到小顺序排列;与数据库中现有信息一致(10分)

(3)查询至少具有两份工作员工的姓名和其公司名。(10分)

4. 具有数据完整性校验功能,当出现数据异常和操作异常时,程序应给出清楚完整的异常提示信息。(10分)
 

【数据库操作】

1.建表

--雇员
create table Employee(
	EmpNo varchar(5) primary key,
	EmpName nvarchar(4) not null,
	EmpSex varchar(2) not null check(EmpSex='男' or EmpSex='女'),
	EmpAge int not null checked(EmpAge>0)
);

--公司
create table Company(
	CmpNo varchar(10) primary key,
	CmpName nvarchar(20) not null
);

--雇员与公司的工作关系
create table Works(
	EmpNo varchar(5),
	CmpNo varchar(10),
	Salary int check(Salary>0),
	primary key(EmpNo, CmpNo),
	foreign key(EmpNo) references Employee(EmpNo) on update cascade on delete cascade,
	foreign key(CmpNo) references Company(CmpNo) on update cascade on delete cascade,
);

2.插入数据

insert into Employee values('E01', '张三', '女',32 );
insert into Employee values('E02', '李四', '男', 28);
insert into Employee values('E03', '王五', '女', 42);
insert into Employee values('E04', '赵六', '男', 37);
insert into Employee values('E05', '陈七', '男', 51);


insert into Company values('C01', '阳光科技');
insert into Company values('C02', '晨光科技');
insert into Company values('C03', '未来科技');


insert into Works values('E01', 'C01', 3000);
insert into Works values('E01', 'C02', 4000);
insert into Works values('E02', 'C02', 5000);
insert into Works values('E02', 'C03', 2500);
insert into Works values('E03', 'C01', 3500);
insert into Works values('E04', 'C02', 3000);
insert into Works values('E05', 'C03', 2000);

3.查询操作

(1)根据员工号或员工名查找员工所在的公司名和工资,员工号或员工名不能文本输入,要求使用下拉菜单实现,并与数据库中现有信息一致(10分)

select [employee].EmpNo, [employee].EmpName, [company].CmpName, [works].Salary
from employee, company, works
where [employee].EmpNo = [works].EmpNo and [company].CmpNo = [works].CmpNo;

(2)统计年龄至少为40岁员工的总工资,工资按从大到小顺序排列;与数据库中现有信息一致(10分)

select [employee].EmpNo, [employee].EmpName, [employee].EmpAge, sum([works].Salary) as salary
from [employee], [works]
where employee.EmpNo = works.EmpNo and employee.EmpAge >= 40
group by employee.EmpNo, employee.EmpName, employee.EmpAge
order by salary desc;

(3)查询至少具有两份工作员工的姓名和其公司名。(10分)

两种实现:

select employee.empNo, employee.empName, company.cmpName
from employee, company, (
	select works.empNo, works.cmpNo
	from works
		where works.empNo in(
		select works.empNo
		from works
		group by works.empNo
		having count(works.empNo)>1
		)
	) as t1
where employee.empNo = t1.empNo and company.cmpNo = t1.cmpNo
select employee.empname, company.cmpname
from employee, company, works, (
	select empno from works 
	group by empno having count(empno)>1
) as w1 
where employee.empno=works.empno and company.cmpno=works.cmpno and works.empno=w1.empno

【C#实现】

1.新建windows窗体项目

2.添加DB.cs类,用于数据库基本操作

3.如果有必要,添加Employee, Works,  Company等类

4.窗体基本实现:

4.1 为了不要把所有操作全部集中在一个菜单内,拖一个tabControl,添加至4个选项卡

4.2 表格数据显示均使用datagridview,方便绑定数据和操作;某项数据显示采用comboBox;绑定数据用DataSource属性实现。

#数据库操作类 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using System.Windows.Forms;


namespace SCUT2017
{
    class DB: IDisposable
    {
        private SqlConnection sqlConnection;

        public DB()
        {
            sqlConnection = new SqlConnection("server=.; uid=pc; pwd=123456; database=SCUT2017");
            sqlConnection.Open();
        }


        /******************查询******************/
        public DataTable getBySql(String sql)
        {//无参数查询
            SqlDataAdapter sda = new SqlDataAdapter(new SqlCommand(sql, sqlConnection));
            DataTable dataTable = new DataTable();
            sda.Fill(dataTable);
            return dataTable;
        }

        public DataTable getBySql(String sql, Object[] param)
        {//参数化查询
            sql = String.Format(sql, param);    //用字符串参数替换的形式防止sql注入
            //trest
            MessageBox.Show(sql);

            SqlDataAdapter sda = new SqlDataAdapter(new SqlCommand(sql, sqlConnection));
            DataTable dataTable = new DataTable();
            //判空
            if (dataTable == null)
            {
                MessageBox.Show("无符合条件的查询结果!");
                return null;
            }
            sda.Fill(dataTable);
            return dataTable;
        }


        /******************增、删、改******************/
        public void setBySql(String sql)
        {//无参数、无查询结果修改
            new SqlCommand(sql, sqlConnection).ExecuteNonQuery();
        }

        public void setBySql(String sql, Object[] param)
        {//参数化、无查询结果修改
            sql = String.Format(sql, param);
            //trest
            MessageBox.Show(sql);
            new SqlCommand(sql, sqlConnection).ExecuteNonQuery();
        }

        public void Dispose()
        {//关闭数据库连接,不能在析构函数中实现
            sqlConnection.Close();
        }


    }
}

#用于主窗体与副窗体间传值的员工类 

    class Employee  //省略类的修饰符时默认为intenal
    {
        //省略类成员修饰符时默认为private
        //静态成员必须使用类名引用
        public static string empNo;
        public static string empName;
        public static string empSex;
        public static int empAge;


        //添加、修改
        public static bool flag;    //flag=true表示添加,flag=false表示修改

        //public String EmpNo{get; set;}
        //public String EmpName { get; set; }
        //public String EmpSex { get; set; }  //C#枚举类
        //public int EmpAge { get; set; }
    }

 #主窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SCUT
{
    public partial class Form3 : Form
    {

        private DB db;


        public Form3()
        {
            InitializeComponent();
            db = DB.getInstance();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

            TabPage1_Click();
            
        }

        //选项卡改变时触发事件
        private void TabControl1_SelectedIndexChanged(Object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Name == "tabPage1")
            {
                TabPage1_Click();

            }
            else if (tabControl1.SelectedTab.Name == "tabPage2")
            {
                TabPage2_Click();
            }
            else if (tabControl1.SelectedTab.Name == "tabPage3")
            {
                TabPage3_Click();
            }
            else if (tabControl1.SelectedTab.Name == "tabPage4")
            {
                TabPage4_Click();
            }
        }

        //显示出所有员工基本信息
        private void TabPage1_Click()
        {
            //DataGridView绑定数据源
            dataGridView1.DataSource = db.getBySql(@"select * from [EMPLOYEE]"); ;

            //改列名称
            dataGridView1.Columns[0].HeaderText = "员工号"; 
            dataGridView1.Columns[1].HeaderText = "姓名";
            dataGridView1.Columns[2].HeaderText = "性别"; 
            dataGridView1.Columns[3].HeaderText = "年龄";
        }

        //将员工号和员工名分别放入两个combox中
        private void TabPage2_Click()
        {
            DataTable table = db.getBySql(@"select * from [EMPLOYEE]");

            comboBox1.DataSource = table ;
            comboBox1.DisplayMember = "EmpNo";  //设置显示列'员工号'
            comboBox1.ValueMember = "EmpNo";    //设置列的实际值

            comboBox2.DataSource = table;
            comboBox2.DisplayMember = "EmpName";  //设置显示列‘员工名’
            comboBox2.ValueMember = "EmpName";    //设置列的实际值
        }

        //统计年龄至少为40岁员工的总工资,工资按从大到小顺序排列
        private void TabPage3_Click()
        {
            //DataGridView绑定数据源
            dataGridView3.DataSource = db.getBySql(@"select [employee].EmpNo, [employee].EmpName, [employee].EmpAge, sum([works].Salary) as salary
                    from [employee], [works]
                    where employee.EmpNo = works.EmpNo and employee.EmpAge >= 40
                    group by employee.EmpNo, employee.EmpName, employee.EmpAge
                    order by salary desc"); ;

            //改列名称
            dataGridView3.Columns[0].HeaderText = "员工号";
            dataGridView3.Columns[1].HeaderText = "姓名";
            dataGridView3.Columns[2].HeaderText = "年龄";
            dataGridView3.Columns[3].HeaderText = "总工资";

        }


        //查询至少具有两份工作员工的姓名和其公司名:
        private void TabPage4_Click()
        {
            //DataGridView绑定数据源
            dataGridView4.DataSource = db.getBySql(@"select employee.empNo, employee.empName, company.cmpName
                from employee, company, (
	                select works.empNo, works.cmpNo
	                from works
		            where works.empNo in(
		                select works.empNo
		                from works
		                group by works.empNo
		                having count(works.empNo)>1
		                )
	                ) as t1
                where employee.empNo = t1.empNo and company.cmpNo = t1.cmpNo"); ;

            //改列名称
            dataGridView4.Columns[0].HeaderText = "员工号";
            dataGridView4.Columns[1].HeaderText = "姓名";
            dataGridView4.Columns[2].HeaderText = "公司名";
        }



        /*****************************员工表增删改****************************/
        //添加新员工
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            Employee.flag = true;   //设置为添加员工
            if (form2.ShowDialog() == DialogResult.OK)
            {
                bool canAdd = true; //能够添加新员工

                //判断员工在列表中是否已存在
                foreach (DataGridViewRow dgRow in dataGridView1.Rows)
                {
                    if ((Employee.empNo).Equals(dgRow.Cells[0].Value))
                    {
                        canAdd = false;
                    }
                }

                //添加员工
                if (canAdd)
                {
                    string[] strArray = { Employee.empNo, Employee.empName, Employee.empSex, (Employee.empAge).ToString() };
                    ((DataTable)dataGridView1.DataSource).Rows.Add(strArray);  
                    //db.setBySql(@"insert into employee values('" + Employee.empNo + "', '" + Employee.empName 
                     //   + "', '" + Employee.empSex + "', " + Employee.empAge + ")");
                    //用字符串参数替换的形式防止注入
                    db.setBySql(@"insert into Employee values('{0}','{1}','{2}','{3}')", new object[] { Employee.empNo, Employee.empName, Employee.empSex, Employee.empAge });
                    MessageBox.Show("添加成功!", "信息提示");
                }
                else
                {
                    MessageBox.Show("添加失败!该员工号已存在!","信息提示");
                }
            }
        }

        //修改员工信息
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            Employee.flag = false;   //设置为修改员工信息
            Employee.empNo = (string)dataGridView1.SelectedRows[0].Cells[0].Value;
            Employee.empName = (string)dataGridView1.SelectedRows[0].Cells[1].Value;
            Employee.empSex = (string)dataGridView1.SelectedRows[0].Cells[2].Value;
            Employee.empAge = (int)dataGridView1.SelectedRows[0].Cells[3].Value;
            
            if (form2.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.SelectedRows[0].Cells[0].Value = Employee.empNo;
                dataGridView1.SelectedRows[0].Cells[1].Value = Employee.empName;
                dataGridView1.SelectedRows[0].Cells[2].Value = Employee.empSex;
                dataGridView1.SelectedRows[0].Cells[3].Value = Employee.empAge;
                    
                //db.setBySql(@"update employee set empNo='" + Employee.empNo + "', empName='"
                //        + Employee.empName + "', empSex='" + Employee.empSex + "', empAge=" + Employee.empAge + " where empNo='" + Employee.empNo + "'");
                //用字符串替换参数的形式防止sql注入
                db.setBySql(@"update Employee set EmpNo='{0}', EmpName='{1}', EmpSex='{2}', EmpAge='{3}' where EmpNo='{0}'",
                    new object[] { Employee.empNo, Employee.empName, Employee.empSex, Employee.empAge });
                
                MessageBox.Show("修改成功!", "信息提示");
                
            }
        }

        //删除员工
        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除?", "信息提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string empNo = (string)dataGridView1.SelectedRows[0].Cells[0].Value;
                db.setBySql(@"delete from employee where employee.empNo = '" + empNo + "'");    //从数据库中删除
                dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);       //从控件中删除
                MessageBox.Show("删除成功!", "信息提示");
            }
        }



        /*************************查询3-1***************************/
        private void button4_Click(object sender, EventArgs e)
        {//根据员工号查找员工所在的公司名和工资

            //DataGridView绑定数据源
            dataGridView2.DataSource = db.getBySql(@"select [employee].EmpNo, [employee].EmpName, [company].CmpName, [works].Salary
                from employee, company, works
                where [employee].EmpNo='" + comboBox1.Text + "' and [employee].EmpNo = [works].EmpNo and [company].CmpNo = [works].CmpNo");;

            //改列名称
            dataGridView2.Columns[0].HeaderText = "员工号";
            dataGridView2.Columns[1].HeaderText = "姓名";
            dataGridView2.Columns[2].HeaderText = "公司名";
            dataGridView2.Columns[3].HeaderText = "薪水";
        }

        private void button5_Click(object sender, EventArgs e)
        {//根据员工名字查找员工所在的公司名和工资

            //DataGridView绑定数据源
            dataGridView2.DataSource = db.getBySql(@"select [employee].EmpNo, [employee].EmpName, [company].CmpName, [works].Salary
                from employee, company, works
                where [employee].EmpName='" + comboBox2.Text + "' and [employee].EmpNo = [works].EmpNo and [company].CmpNo = [works].CmpNo"); ;

            //改列名称
            dataGridView2.Columns[0].HeaderText = "员工号";
            dataGridView2.Columns[1].HeaderText = "姓名";
            dataGridView2.Columns[2].HeaderText = "公司名";
            dataGridView2.Columns[3].HeaderText = "薪水";
        }


    }
}

#副窗体 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SCUT
{
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {
            if (Employee.flag)  //flag为true表示添加员工
            {
                radioButton1.Checked = true;
                this.textBox3.Focus();//设置焦点停留在textBox1中 
            }
            else    //flag为false表示修改员工
            {
                textBox1.Text = Employee.empNo;
                textBox2.Text = Employee.empName;
                if ((Employee.empSex).Equals("男"))
                {
                    radioButton1.Checked = true;
                }
                else {
                    radioButton2.Checked = true;
                }
                textBox3.Text = Employee.empAge + "";
                textBox1.Focus();//设置焦点停留在textBox1中   
                textBox1.SelectAll();//要先有焦点才能全选  
            } 
        }
 
        //确定
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || (!radioButton1.Checked && !radioButton2.Checked))
            {
                MessageBox.Show("任意一项没有完成填写!", this.Text);
            }
            else
            {
                //关闭form2之前,将要传给form1的值传入Employee对象中   
                Employee.empNo = (textBox1.Text).Trim();    //员工号
                Employee.empName = (textBox2.Text).Trim();  //员工姓名
                if(radioButton1.Checked){                   //员工性别
                    Employee.empSex = "男";
                }else{
                    Employee.empSex = "女";
                }
                int parseResult = 0;    //用于保存员工年龄
                if (int.TryParse((textBox3.Text).Trim(), out parseResult) && parseResult>0)
                {
                    Employee.empAge = parseResult;
                    this.DialogResult = DialogResult.OK;//同时设置返回值为OK,不设置的话,默认返回Cancel
                }
                else
                {
                    MessageBox.Show("请输入有效的年龄!", "信息提示");
                } 

            } 

        }
 
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();  
        }
    }
}

 

你可能感兴趣的:(复试,机试,华南理工)