资源描述
数据库课程设计
题 目 某电力企业收费管理信息系统
专业班级
姓 名
学 号
完成日期 -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. 每个同学完成一个题目,题目由老师安排。
四、 试验环境
VS,SQLsever
五、课程设计过程
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','-12-01','100');
Insert into 用电信息 values('00001','ABE','-12-01','220');
Insert into 用电信息 values('00002','ABC','-12-01','110');
Insert into 用电信息 values('00003','ABE','-11-01','125');
Insert into 用电信息 values('00003','ABE','-12-01','105');
Insert into 用电信息 values('00004','ABC','-12-01','200');
Insert into 用电信息 values('00005','ABC','-12-01','102');
Insert into 用电信息 values('00006','ABC','-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);
DataSet dss = new DataSet();
da1.Fill(dss);
展开阅读全文