SCUT2017机试数据库

题目 

 

SCUT2017机试数据库_第1张图片

SCUT2017机试数据库_第2张图片SCUT2017机试数据库_第3张图片

SCUT2017机试数据库_第4张图片

由于听说软院考的只有查询,所以这些计院的增删改都只是象征性的做了一下。

实现

建立数据库

create table Patient(
PID varchar(10) primary key,
PName varchar(6) not null,
Date date,
GroupNo int
)
create table [Group](
GroupNo int primary key,
GroupName varchar(10),
DeptNo int,
Month int,
Number int 
check(Number<81) 
)
create table Department(
DeptNo int primary key,
DeptName varchar(10)
)
alter table Patient add foreign key(GroupNo) references [Group](GroupNo) on delete cascade;
alter table [Group] add foreign key(DeptNo) references Department(DeptNo);

insert into Department values(111,'内科');
insert into Department values(222,'外科');
insert into Department values(333,'中医科');

insert into [Group] values(11,'心内科',111,3,32);
insert into [Group] values(12,'呼吸内科',111,4,45);
insert into [Group] values(13,'中医保健科',333,3,68);
insert into [Group] values(14,'骨科',222,4,28);
insert into [Group] values(15,'心外科',222,4,56);

insert into Patient values('880101','李一','1980-6-1',11);
insert into Patient values('880102','王二','1978-2-3',15);
insert into Patient values('880103','张三','1969-11-2',13);
insert into Patient values('880104','李四','1975-7-20',13);
insert into Patient values('880105','赵五','1986-5-16',14);
insert into Patient values('880106','杨六','1998-4-23',13);
insert into Patient values('880107','赵七','2000-3-1',12);
insert into Patient values('880108','周八','1992-7-5',11);

C#实现

SCUT2017机试数据库_第5张图片

SCUT2017机试数据库_第6张图片

其他的都是一个dataGridView 这里就不贴了

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 DC2017SCUT
{
    class DBUtils
    {
        private DBUtils() { }
        static String strCon = @"data source=.\sqlexpress; initial catalog=hospital2017; integrated security =true";
        static SqlConnection con = new SqlConnection(strCon);

        public static SqlConnection getCon()
        {
            if (con.State == ConnectionState.Closed) con.Open();
            return con;
        }
    }
}

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 DC2017SCUT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        String sql = "";
        private void button1_Click(object sender, EventArgs e)
        {
            String DeptName = textBox1.Text.Trim();
            String GroupName = textBox2.Text.Trim();
            String PName = textBox3.Text.Trim();
            sql = "select DeptName as 大科室名称,GroupName as 小科室名称,PName as 病人姓名 from Patient inner join [Group] on Patient.GroupNo=[Group].GroupNo inner join Department on [Group].DeptNo=Department.DeptNo where 1=1 ";
            if (!"".Equals(DeptName))
            {
                sql += "and Department.DeptName='"+DeptName+"' ";
            }
            if (!"".Equals(GroupName))
            {
                sql += " and [Group].GroupName='" + GroupName + "'";
            }
            if (!"".Equals(PName))
            {
                sql += " and PName like '%" + PName + "%'";
            }
            sql += " order by DeptName desc,GroupName asc,PName desc";
            dataGridView1.DataSource= execute(sql);
        }
        //执行sql 语句并将结果返回
        private DataTable execute(string sql)
        {
            SqlConnection con = DBUtils.getCon();
            SqlCommand com = new SqlCommand(sql, con);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(ds,"hospital2017");
            DataTable table= ds.Tables["hospital2017"];
            con.Close();
            return table;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage2)
            {
                listView1.Clear();
                sql = "select PID as 身份证号,PName as 病人姓名,Date as 出生日期,GroupNo as 小科室编号 from Patient";
                DataTable table = execute(sql);

                for (int i = 0; i < table.Columns.Count; i++)
                {
                    ColumnHeader h = new ColumnHeader();
                    h.Text = table.Columns[i].ToString();
                    h.Width = 100;
                    listView1.Columns.Add(h);
                }

                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());
                    }
                    listView1.Items.Add(item);
                }
                listView1.View = View.Details;
                listView1.GridLines = true;
                listView1.FullRowSelect = true;
            }
            else if (tabControl1.SelectedTab == tabPage3)
            {
                sql = "select DeptName as 大科室名称,SUM(Number) as 病人人数 from [Group] inner join Department on [Group].DeptNo=Department.DeptNo group by Department.DeptNo,DeptName";
                dataGridView2.DataSource = execute(sql);
            }
            else if (tabControl1.SelectedTab == tabPage4)
            {
                sql = "select Month as 统计月份,sum(Number) as 病人数 from [Group] group by Month";
                dataGridView3.DataSource = execute(sql);
            }
            else if (tabControl1.SelectedTab == tabPage5)
            {
                sql = "select [Group].GroupNo as 小科室编号,GroupName as 小科室名称,DeptNo as 所属大科室编号,Month as 统计月份,Number as 病人人数 from [Group] where Number=(select MAX(Number) from [Group]) or Number=(select MIN(Number) from [Group])";
                dataGridView4.DataSource = execute(sql);
            }
        }
    }
}

运行结果

第一题:

SCUT2017机试数据库_第7张图片

第二题:  这里的出生日期在数据库里面选择了Date类型,所以在查询的时候成了这种格式,建议还是用varchar类型

SCUT2017机试数据库_第8张图片

第三题

SCUT2017机试数据库_第9张图片

SCUT2017机试数据库_第10张图片

第四题  直接实现了找出最多病人数和最少病人数记录

SCUT2017机试数据库_第11张图片

你可能感兴趣的:(考研路上)