设计项目s9,创建如下图所示的WinForms应用程序。这个“病人管理系统”应接受病人的详细信息,并将病人的记录插入SqlServer或MySQL数据库中。该数据库的结构见下表。 窗体设计: 数据库设计: 在MySQL中建立patient数据库,所用软件为HeidiSQL,该数据库内含四张表:病人数据表P、医生数据表D、科室数据表K、类型数据表L。各表的属性如下图所示: SQL源码:
CREATE DATABASE IF NOT EXISTS `patient`
USE `patient`;
CREATE TABLE IF NOT EXISTS `d` (
`医生编号` varchar(5) NOT NULL,
`姓名` varchar(8) DEFAULT NULL,
`性别` varchar(4) DEFAULT '男',
`科室` varchar(4) DEFAULT NULL,
PRIMARY KEY (`医生编号`),
KEY `FK_d_k` (`科室`),
CONSTRAINT `FK_d_k` FOREIGN KEY (`科室`) REFERENCES `k` (`科室编号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='病人管理系统的医生数据表';
CREATE TABLE IF NOT EXISTS `k` (
`科室编号` varchar(4) NOT NULL,
`科室名称` varchar(10) DEFAULT NULL,
PRIMARY KEY (`科室编号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='病人管理系统的科室数据表';
CREATE TABLE IF NOT EXISTS `l` (
`类别编号` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'L1',
`类别名称` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '轻微',
PRIMARY KEY (`类别编号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='病人管理系统的类型数据表';
CREATE TABLE IF NOT EXISTS `p` (
`病人编号` varchar(50) NOT NULL,
`姓名` varchar(8) DEFAULT NULL,
`年龄` int DEFAULT NULL,
`性别` char(4) DEFAULT '男',
`医生` varchar(5) DEFAULT NULL,
`病症` tinytext,
`类别` varchar(5) DEFAULT NULL,
PRIMARY KEY (`病人编号`),
KEY `FK_p_d` (`医生`),
KEY `FK_p_l` (`类别`),
CONSTRAINT `FK_p_d` FOREIGN KEY (`医生`) REFERENCES `d` (`医生编号`),
CONSTRAINT `FK_p_l` FOREIGN KEY (`类别`) REFERENCES `l` (`类别编号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='病人管理系统的病人数据表';
MainForm.cs:
using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace s9
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
void Button1Click(object sender, EventArgs e)
{
string PatientNumber, PatientName, PatientSex, PatientDoctor, PatientSymptom, PatientType;
int PatientAge;
string Connection = "data source=localhost;database=patient;user id=你自己的MySQL用户名;password=你的MySQL密码;pooling=true;charset=utf8;";
using(MySqlConnection msc = new MySqlConnection(Connection))
{
PatientNumber = textBox1.Text;
PatientName = textBox2.Text;
PatientAge = int.Parse(textBox3.Text);
PatientSex = "";
if(radioButton1.Checked)
{
PatientSex = radioButton1.Text;
}
else if(radioButton2.Checked)
{
PatientSex = radioButton2.Text;
}
PatientDoctor = comboBox1.Text;
PatientType = comboBox2.Text;
PatientSymptom = textBox4.Text;
msc.Open();
string sql = "INSERT INTO p values('" + PatientNumber + "','" + PatientName + "','" + PatientAge + "','" + PatientSex + "','" + PatientDoctor + "','" + PatientSymptom + "','" + PatientType + "')";
MySqlCommand cmd = new MySqlCommand(sql, msc);
cmd.ExecuteNonQuery();
msc.Close();
MessageBox.Show("添加成功!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
}
}
void Button2Click(object sender, EventArgs e)
{
Close();
}
void MainFormLoad(object sender, EventArgs e)
{
string Connection = "data source=localhost;database=patient;user id=你自己的MySQL用户名;password=你的MySQL密码;pooling=true;charset=utf8;";
using(MySqlConnection msc = new MySqlConnection(Connection))
{
msc.Open();
string sql1 = "select 医生编号 from d";
MySqlCommand cmd1 = new MySqlCommand(sql1, msc);
MySqlDataReader reader1 = cmd1.ExecuteReader();
while(reader1.Read())
{
comboBox1.Items.Add((string)reader1["医生编号"]);
}
msc.Close();
msc.Open();
string sql2 = "select 类别编号 from l";
MySqlCommand cmd2 = new MySqlCommand(sql2, msc);
MySqlDataReader reader2 = cmd2.ExecuteReader();
while(reader2.Read())
{
comboBox2.Items.Add((string)reader2["类别编号"]);
}
msc.Close();
}
}
}
}
Program.cs:
using System;
using System.Windows.Forms;
namespace s9
{
internal sealed class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
测试结果: 运行程序,弹出如下界面: 输入病人编号为20211028_1234,姓名为小明,性别为男,年龄为25,医生为李华(D0007),类别为轻微(L1),病症为“感冒,头晕,流鼻涕”,按下“添加”按钮,自动弹出“添加成功”提示窗体,查看数据库中的P表,发现多了一行病人数据,即为刚刚插入的病人信息。 输入病人编号为20211028_8888,姓名为小红,性别为女,年龄为55,医生为李四(D0002),类别为普通(L2),病症为“腰肌劳损”,按下“添加”按钮,自动弹出“添加成功”提示窗体,查看数据库中的P表,发现多了一行病人数据,即为刚刚插入的病人信息。
|