使用C#进行防疫物资管理系统设计,包括数据库设计,连接以及登录、注册、增删改查操作设计,完成寒假C#大作业,进行一下分享以及我的理解,希望对大家有帮助。下图为此系统不同人员下的不同操作权限。
一、数据库设计(SQL Server)
- 需求分析
对于疫情情况下的物资分配进行设计,结合实现某些功能,并进行可行性测试。
- 结构设计
- 数据表信息
数据表 | 属性名 |
|
admin表 | work_id、ad_name、pwd、phone |
|
users表 | user_id、user_name、ID、pwd、register_date |
|
deliver表 | deliver_id、deliver_name、phone、pwd |
|
mask表 | mask_type、m_name、remain_num、price |
|
info表 | order_id、user_id、user_name、allocate_num、phone、address、status、re_date | |
reserve表 | user_id、re_date、mask_type、ID、r_num、ex_date、phone、address |
|
allocate表 | work_id、order_id、allocate_time、deliver_id |
|
take表 | deliver_id、order_id、take_date、finish_date |
|
Admin表:
Users表:
Delivers表:
Item表:
Info表:
Reserve表:
Allocate表:
Take表:
2.2、数据需求
包括管理员账号数据信息、用户数据信息、快递员数据信息、防疫物资信息、订单信息、预约信息、分配信息以及快递信息。
管理员信息(Admin)
USE MySchool
CREATE table Admin(work_id varchar(32) primary key not null,
pwd varchar(32) not null,
ad_name varchar(32),
phone varchar(32));
用户数据信息(users)
CREATE table users(work_id varchar(32) primary key not null,
pwd varchar(32) not null,
ad_name varchar(32),
phone varchar(32)
Provincial_certificate_number varchar);
快递员数据信息(deliver)
CREATE table delivers(deliver_id varchar(32) primary key not null,
pwd varchar(32) not null,
deliver_name varchar(32) not null,
phone varchar(32));
防疫物资信息(item)
CREATE table reserve(user_id varchar(32) not null,
re_date datetime not null,
foreign key (user_id) references users(work_id),
primary key(user_id,re_date),
ID varchar(32) not null,
r_num int not null,
ex_date date not null,
phone varchar(32) not null,
address varchar(32) not null);
订单信息(info)、预约信息(reserve)、分配信息(allocate)、快递信息(take)(通过下图进行相关性设计)
数据库设计时,没有保存SQL语言部分。(下图为数据库表的关联信息)
二、C#源码介绍
- 界面设计(部分展示)
界面方面可以根据自己的想法设计,这里展示一部分具体以源代码中显示的为主,也可以自己进行界面美化。
//技巧,可以通过以下代码进行界面跳转,可以进行多界面设计;以打开form1为例
Form1 form1 = new Form1();
form1.Show();
1.1、登陆界面
1.2、信息管理界面
1.3、用户界面
- 操作
连接数据库(需要此窗体创建连接对象)
string strMyConnectoion = "Data Source="服务器名称";Initial Catalog=Myschool;Integrated Security=True";
SqlConnection myConnection;//数据库连接对象
查询操作(以Admin为例)
if (comboBox1.Text == "Admin")
{
try
{
//打开数据库
myConnection.Open();
//实例化命令对象
SqlCommand myCommand = new SqlCommand();
//把要操作的数据库传过来
myCommand.Connection = myConnection;
//操作类型为文本类型
myCommand.CommandType = CommandType.Text;
//命令格式,代表查询全部
myCommand.CommandText = @"select * from Admin";
//创建DataAdapter对象
SqlDataAdapter sda = new SqlDataAdapter(myCommand);
//创建DataSet对象
DataSet ds = new DataSet();
//将表填充到DataSet数据集中
sda.Fill(ds, "Admin");
//将表显示到控件上
dataGridView1.DataSource = ds.Tables["Admin"].DefaultView;
//关闭数据库
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
myConnection.Close();
}
登录操作(以Admin为例)
if (comboBox1.Text == "管理员")
{
try
{ myConnection.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
if (textBox1.Text == "")
{
MessageBox.Show("管理员不能为空", "管理员登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空", "管理员登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
string user_Id = textBox1.Text;
string password = textBox2.Text;
string sql = "SELECT COUNT(*) FROM Admin WHERE work_id='" + user_Id + "'AND pwd='" + password + "';";
SqlCommand cmd = new SqlCommand(sql, myConnection);//查询
if (Convert.ToInt32(cmd.ExecuteScalar()) == 1) // 转换 查找 找到为1
{
//进入管理员界面
MessageBox.Show("登入成功", "欢迎使用", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
else //登入成功则直接进入信息界面
{
MessageBox.Show("账号或者密码错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
myConnection.Close();
}
return;
}
注册设置(以Admin为例)
if (comboBox1.Text == "管理员")
{
string sql = "insert into Admin(work_id,pwd,ad_name,phone,regtime)values('{0}','{1}','{2}','{3}','{4}')";
sql = string.Format(sql, textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim(), dateTimePicker1.Text.Trim());
SqlCommand cmd = new SqlCommand(sql, myConnection);
try
{
myConnection.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("注册成功!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("连接数据库失败,具体为:" + ex.Message, "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
myConnection.Close();
}
}
5.增加操作(以Admin为例)
if (comboBox1.Text == "Admin")
{
string sql = "insert into Admin(work_id,pwd,ad_name,phone,regtime)values('{0}','{1}','{2}','{3}','{4}')";
sql = string.Format(sql, textBox1.Text.Trim(), textBox4.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), dateTimePicker1.Text.Trim());
SqlCommand cmd = new SqlCommand(sql, myConnection);
try
{
myConnection.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("插入数据成功!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("连接数据库失败,具体为:" + ex.Message, "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
myConnection.Close();
}
}
删除操作(以users为例)
if (comboBox1.Text == "users")
{
//删除语法
//delete from 表名 where 条件
try
{
//打开数据库
myConnection.Open();
//实例化命令对象
SqlCommand myCommand = new SqlCommand();
//把要操作的数据库传过来
myCommand.Connection = myConnection;
//操作类型为文本类型
myCommand.CommandType = CommandType.Text;
//命令格式,代表按姓名查询
myCommand.CommandText = @"delete from users where user_name =@user_name";
myCommand.Parameters.Add(new SqlParameter("@user_name", textBox13.Text));
//开始查询
int res = Convert.ToInt32(myCommand.ExecuteNonQuery());
//如果查询不到,报错
if (res == 0)
{
throw new Exception("查无此人");
}
MessageBox.Show("删除成功");
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
myConnection.Close();
}
}
修改只是在dataGridView控件中修改其属性enable改为TRUE即可。
三、总结
有些细节以源码为主,并未在本文提出,也可以给我一些建议,由于只是为了这次大作业完成的任务并没有进行美化操作。
源码自取。