数据库课程设计某电力公司收费系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数据库课程设计某电力公司收费系统
数据库课程设计
题目某电力公司收费管理信息系统专业班级
姓名
学号
完成日期 2013-12-18
地址 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','2013-12-01','100'); Insert into 用电信息 values('00001','ABE','2013-12-01','220'); Insert into 用电信息 values('00002','ABC','2013-12-01','110'); Insert into 用电信息 values('00003','ABE','2013-11-01','125'); Insert into 用电信息 values('00003','ABE','2013-12-01','105'); Insert into 用电信息 values('00004','ABC','2013-12-01','200'); Insert into 用电信息 values('00005','ABC','2013-12-01','102'); Insert into 用电信息 values('00006','ABC','2013-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,
rmation);
//刷新数据
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,
rmation);
//刷新数据
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 { }
}
运行结果如图所示:(注应该先选中,在修改,目的改主码)
1.删除一行信息的内容
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, rmation);
//刷新数据
string sqls = "select * from 用电信息 "; SqlDataAdapter da1 = new SqlDataAdapter(sqls, con);
DataSet dss = new DataSet();
da1.Fill(dss);
if (dss.Tables[0].Rows.Count > 0)
{
dataGridView1.DataSource = dss.Tables[0];
}
}
}
}
}
catch { }
}
}
}
七、运行结果展示:
触发器实现自动修改和计算:
每当添加一个新值,收费标志自动改成未收
存储过程的实现,实现存储指定日期的应收费用和实收费用
八、总结与心得体会
通过几月的课程设计,还是简单地完成了“某电力公司收费管理信息系统”题目的相关要求。

在设计时创建了表table、存储过程procedure触发器trigger、规则rule来实现相关的功能和写了SQL语句(create,select等语句)来创建、查询表中的相关信息,这次课程设计也让我熟悉了使用VS2010和SQLsever2008,同时把sql语句强加练习了一遍,还学会了创建规则和存储过程还有触发器,并且熟悉的使用了C#,但也遇到了许多问题,比如:1.由于对C#不熟悉,刚开始插手很难,后来慢慢熟悉了才掌握了一些编程技巧。

2.在修改主码时,由于查询是根据主码的,所以不好实现,通过查资料,可以把就的值提取出来并保存在一个字符串里,这样就轻易解决了,代码如:
inttnum=dataGridView1.CurrentRow.Index;//先选中某一行
string oldDate = dataGridView1.Rows[tnum].Cells["月份
"].Value.ToString();
string oldLeibie = dataGridView1.Rows[tnum].Cells["类别号
"].Value.ToString();这样就可以修改主码了。

3.创建触发器来实现相关功能时由于自身知识的不够,写的不完整,导致不能级联,经过反复修改才能实现。

4.存储过程实现时由于不会调用,不知道怎样传参,做了好长时间,最后经过向别人请教才实现了存储过程。

代码如下:
comm.Parameters.Add("@month", SqlDbType.Date);
comm.Parameters["@month"].Value = textBox2.Text;
但不管怎么说这次课程设计还是收获不少的,这不仅是对我们一个学期以来所学知识内容的综合考验,也是我们回顾知识、牢记知识、复习知识的一次大好时机,同时也是我们从中看到自己的不足的一次作业;我觉得我们应该认真对待、虚心学习!。

相关文档
最新文档