资源描述
数据库课程设计
题 目 某电力公司收费管理信息系统
专业班级
姓 名
学 号
完毕日期 2023-12-18
一、 课程实验目的
1. 通过课程设计,使学生进一步巩固所学的知识,考察学生对数据库理论及知识的理解能力和综合运用能力;
2. 培养学生自主学习、独立思考的能力,学会查找资料并善于分析资料的能力;
3. 培养学生独立设计、独立调试程序的能力;
4. 培养学生初步的软件设计能力,形成良好的编程风格
二、 课程实验规定
(28)某电力公司收费管理信息系统
1. 实现客户信息、用电类型(类别号、类别名、电价)及业务员管理;
2. 实现客户用电信息管理(客户号、月份、用电类别号、用电度数);
3. 实现客户费用管理(客户号、月份、费用、收费标志),收费标志的默认值为‘未收’;
4. 实现收费登记(客户、月份、应收费用、实收费用、业务员),并自动修改收费标志(用触发器实现);
5. 创建触发器,实现收费时自动更加应收费用和实收费用,计算本次结余,然后修改客户信息表中的结余金额;
6. 创建存储过程记录指定月份应收费用和实收费用;
7. 创建存储过程查询指定月份未交费的用户信息,以便崔费;
8. 创建规则使得月份符合格式“××××年××月”,并邦定到表中相应字段;
9. 建立表间关系。
三、 课程设计规定
1. 在对数据库理论及知识理解的基础上;重点是针对具体的实际问题选择并设计合适的数据库表加以应用,并在此基础上完毕相关的算法与程序;
2. 给出系统的概要设计、具体设计;
3. 完毕数据流程图,E-R关系图,数据库表、程序流程图、功能模块图的设计、对功能编程加以实现;
4. 设计必要的视图、触发器、存储过程;
5. 使用相关的编程工具(C#.NET)和ORACLE编写代码实现设计的数据库系统;
6. 完毕规范化的课程设计报告的编写;
7. 每个同学完毕一个题目,题目由老师安排。
四、 实验环境
VS2023,SQLsever2023
五、课程设计过程
1. 根据题目写出关系模型如下:
① 客户(客户号、客户名、地址、联系方式)
② 用电类型(类别号、编号、类别名、电价)
③ 员工(员工号、姓名、性别、联系方式)
④ 用电信息(客户号、月份、类别号、用电度数)
⑤ 费用管理(客户号、月份、费用、收费标志)
⑥ 收费登记(客户号、月份、应收费用、实收费用、员工号)
⑦ 结余登记(客户号、月份、应收费用、实收费用、结余费用)
2、根据关系模型绘制出E-R模型为:
3、 创建表
1.客户表
Create table 客户
(
客户号 char(5) PRIMARY KEY,
客户名 char(4),
地址 varchar(50),
联系方式 char(10)
);
插入数据:
Insert into 客户 values('00001','张三','市南区','0000000');
Insert into 客户 values('00002','李四','黄岛区','0000002');
Insert into 客户 values('00003','王五','崂山区','0000003');
Insert into 客户 values('00004','赵兰','城阳区','0000004');
Insert into 客户 values('00005','李青','黄岛区','0000005');
Insert into 客户 values('00006','张倩','市南区','0000001');
2.用电类型表
Create table 用电类型
(
类别号 char(10) PRIMARY KEY,
类别名 varchar(50),
电价 money
);
插入数据:
Insert into 用电类型 values('ABC','家庭','1.00');
Insert into 用电类型 values('ABD','政府','2.00');
Insert into 用电类型 values('ABE','工厂','1.50');
Insert into 用电类型 values('ABF','学校','2.50');
Insert into 用电类型 values('ABG','医院','0.50');
3.员工表
Create table 员工
(
员工号 char(5) PRIMARY KEY,
姓名 char(20),
性别 char(10),
联系方式 char(20)
);
插入数据:
Insert into 员工 values('12345','李丽','女','1230000');
Insert into 员工 values('12346','王华','男','1230002');
Insert into 员工 values('12347','张悦','女','1230003');
4.用电信息表
Create table 用电信息
(
客户号 char(5) ,
类别号 char(10) ,
月份 date ,
用电度数 char(8),
primary key (客户号,类别号,月份),
foreign key (客户号) references 客户(客户号),
foreign key (类别号) references 用电类型(类别号)
);
Insert into 用电信息 values('00001','ABC','2023-12-01','100');
Insert into 用电信息 values('00001','ABE','2023-12-01','220');
Insert into 用电信息 values('00002','ABC','2023-12-01','110');
Insert into 用电信息 values('00003','ABE','2023-11-01','125');
Insert into 用电信息 values('00003','ABE','2023-12-01','105');
Insert into 用电信息 values('00004','ABC','2023-12-01','200');
Insert into 用电信息 values('00005','ABC','2023-12-01','102');
Insert into 用电信息 values('00006','ABC','2023-12-01','100');
5.费用管理
Create table 费用管理
(
客户号 char(5) ,
月份 date ,
费用 money,
收费标志 varchar(50),
PRIMARY KEY (客户号,月份),
foreign key (客户号) references 客户(客户号)
);
6.收费登记
Create table 收费登记
(
客户号 char(5),
月份 date ,
应收费用 money,
实收费用 money,
员工号 char(5),
PRIMARY KEY (客户号,月份),
foreign key (员工号) references 员工(员工号)
);
7.结余登记
Create table 结余登记
(
客户号 char(5),
月份 date ,
应收费用 money,
实收费用 money,
结余费用 money,
PRIMARY KEY (客户号,月份)
);
4.创建触发器
触发器1:计算费用
create trigger change_trigger1
on 用电信息
for insert
as
insert
into 费用管理 (客户号,月份,费用)
SELECT inserted.客户号,inserted.月份,inserted.用电度数*(select 电价 from 用电类型,inserted where 用电类型.类别号=inserted.类别号)
FROM inserted;
触发器2:未收标志
create trigger change_trigger on 费用管理
for insert
as
update 费用管理 set 收费标志='未收';
触发器3:已收标志
create trigger change_trigger3
on 收费登记
for update
as
update 费用管理 set 收费标志='已收'
from 费用管理,inserted
where 费用管理.客户号=inserted.客户号 and 费用管理.月份=inserted.月份;
触发器4:结余登记
create trigger change_trigger4
on 收费登记
for update
as
insert into 结余登记
select inserted.客户号, inserted.月份,inserted.应收费用,inserted.实收费用,inserted.实收费用-inserted.应收费用
from inserted;
5.建立存储
存储过程1:应收费用,实收费用
create procedure ch_procedure01
@month date
as
begin
select 月份,应收费用,实收费用
from 收费登记
where 收费登记.月份=@month
end
go
存储过程2:未收费
create procedure ch_procedure02
@month date
as
begin
select 客户号,月份,结余费用
from 结余登记
where 结余登记.月份=@month
order by 结余费用
end
go
6.表间关系
六、代码实现过程
1.显示窗体内的内容及表的内容
private void Form5_Load(object sender, EventArgs e)
{
string consqlserver = "Data Source=GXW-PC;Initial Catalog=liqiuyue0;Integrated Security=True";
//string consqlserver = "Data Source=CHEN-PC;Initial Catalog=rl;Integrated Security=True";
//定义连接数据源
string sql = "select * from 用电信息 ";
SqlConnection sqlcon = new SqlConnection(consqlserver);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, sqlcon);
DataSet ds = new DataSet();//shengming
try
{
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 1) //判断是否有符合条件的数据记录
{
//将获得数据源赋予数据库控件
dataGridView1.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
sqlcon.Close();
sqlcon.Dispose();
da.Dispose();
}
}
结果如图所示:
2.插入一行信息的代码
private void label3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string consqlserver = "Data Source=GXW-PC;Initial Catalog=liqiuyue0;Integrated Security=True";
// string sql = "select * from 客户 ";
SqlConnection sqlcon = new SqlConnection(consqlserver);
sqlcon.Open();
try
{
// 一方面判断输入信息是否完全
if (textBox1.Text == "")
{
MessageBox.Show("请输入完整的数据信息", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
DataSet ds = new DataSet();
// 在此判断保存反复记录问题
string strSqls;
strSqls = string.Format("SELECT * FROM 用电信息 where 客户号='" + textBox1.Text.Trim() + "'and 类别号='" + textBox2.Text.Trim() + "'and 月份='" + textBox3.Text.Trim() + "';");
//定义SQL Server连接对象
SqlDataAdapter da = new SqlDataAdapter(strSqls, sqlcon);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
MessageBox.Show("已经存在", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
strSqls = "insert into 用电信息 values ('" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "');";
//定义SQL Server连接对象
///////////////////
SqlConnection sqlcon1 = new SqlConnection(consqlserver);
SqlCommand cmd = new SqlCommand(strSqls, sqlcon1);
try
{
sqlcon1.Open();
cmd.ExecuteNonQuery();
}
catch { }
finally
{
sqlcon1.Close();
sqlcon1.Dispose();
cmd.Dispose();
}
MessageBox.Show("保存成功", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
//刷新数据
string sqls = "select * from 用电信息 ";
SqlDataAdapter da1 = new SqlDataAdapter(sqls, sqlcon);
da1.Fill(ds);
if (ds.Tables[0].Rows.Count > 1)
{
dataGridView1.DataSource = ds.Tables[0];
}
}
}
}
catch { }
}
结果如图所示:
3.修改一行信息的内容
private void button2_Click(object sender, EventArgs e)
{
string consqlserver = "Data Source=GXW-PC;Initial Catalog=liqiuyue0;Integrated Security=True";
// string sql = "select * from 客户 ";
SqlConnection sqlcon = new SqlConnection(consqlserver);
sqlcon.Open();
try
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入客户号,类别号,月份", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
DataSet ds = new DataSet();
string strSqls = string.Format("SELECT * FROM 用电信息 where 客户号='" + textBox1.Text.Trim() + "'and 类别号='" + textBox2.Text.Trim() + "'and 月份='" + textBox3.Text.Trim() + "';");
// string strSqls = string.Format("SELECT * FROM 客户 where 客户号='" + textBox1.Text.Trim() + "'");
// string strSqls = string.Format("update teacher set tname='" + textBox2.Text.Trim() + "'where tno='" + textBox1.Text.Trim() + "'");
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdapter da = new SqlDataAdapter(strSqls, con);
//定义SQL Server连接对象
da.Fill(ds);
int tnum = dataGridView1.CurrentRow.Index;//先选中某一行
string oldDate = dataGridView1.Rows[tnum].Cells["月份"].Value.ToString();
string oldLeibie = dataGridView1.Rows[tnum].Cells["类别号"].Value.ToString();
// strSqls = string.Format("update 客户 set 客户名='" + textBox2.Text.Trim() + "'where 客户号='" + textBox1.Text.Trim() + "'update 客户 set 地址='" + textBox3.Text.Trim() + "'where 客户号='" + textBox1.Text.Trim() + "'update 客户 set 联系方式='" + textBox4.Text.Trim() + "'where 客户号='" + textBox1.Text.Trim() + "");
strSqls = string.Format("update 用电信息 set 类别号='" + textBox2.Text.Trim() + "',月份='" + textBox3.Text.Trim() + "',用电度数='" + textBox4.Text.Trim() + "'where 客户号='" + textBox1.Text.Trim() + "'and 类别号='" + oldLeibie + "'and 月份='" + oldDate + "';");
SqlConnection cons = new SqlConnection(consqlserver);
SqlCommand cmd = new SqlCommand(strSqls, cons);
try
{
cons.Open();
cmd.ExecuteNonQuery();
}
catch { }
finally
{
cons.Close();
cons.Dispose();
cmd.Dispose();
}
MessageBox.Show("修改成功", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
//刷新数据
string sqls = "select * from 用电信息";
SqlDataAdapter da1 = new SqlDataAdapter(sqls, con);
DataSet dss = new DataSet();
da1.Fill(dss);
if (dss.Tables[0].Rows.Count > 1)
{
dataGridView1.DataSource = dss.Tables[0];
}
}
}
catch { }
}
运营结果如图所示:(注应当先选中,在修改,目的改主码)
5. 删除一行信息的内容
private void button3_Click(object sender, EventArgs e)
{
string consqlserver = "Data Source=GXW-PC;Initial Catalog=liqiuyue0;Integrated Security=True";
SqlConnection sqlcon = new SqlConnection(consqlserver);
sqlcon.Open();
try
{
if (textBox1.Text == "")
{
MessageBox.Show("请选择或输入要删除的信息", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
DataSet ds = new DataSet();
// 判断要删除的数据信息是否存在
string strSqls;
strSqls = string.Format("SELECT * FROM 用电信息 where 客户号='" + textBox1.Text.Trim() + "'and 类别号='" + textBox2.Text.Trim() + "'and 月份='" + textBox3.Text.Trim() + "';");
// strSqls = string.Format("SELECT * FROM 客户 where 客户号='" + textBox1.Text.Trim() + "'");
//定义SQL Server连接对象
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdapter da = new SqlDataAdapter(strSqls, con);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
if (MessageBox.Show("确认要删除吗?", "信息提醒", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
//定义删除数据信息的SQL语句
strSqls = string.Format("delete from 用电信息 where 客户号='" + textBox1.Text.Trim() + "'and 类别号='" + textBox2.Text.Trim() + "'and 月份='" + textBox3.Text.Trim() + "';");
//定义SQL Server连接对象
SqlConnection cons = new SqlConnection(consqlserver);
SqlCommand cmd = new SqlCommand(strSqls, cons);
try
{
cons.Open();
cmd.ExecuteNonQuery();
}
catch { }
finally
{
cons.Close();
cons.Dispose();
cmd.Dispose();
}
MessageBox.Show("信息删除成功", "信息提醒", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
//刷新数据
string sqls = "select * from 用电信息 ";
SqlDataAdapter da1 = new SqlDataAdapter(sqls, con);
Da
展开阅读全文