报告
班级:
序号:
姓名:
地球科学学院地信系
2018-06-28
数据库课程设计1
目录2
一、概述3
1.1背景说明3
1.2开发环境3
二、需求分析3
2.1用户需求3
2.2主要功能3
2.3数据流图3
2.4数据字典3
三、数据库概念结构设计4
四、数据库逻辑结构设计2
4.1 关系表设计2
4.2 数据表关系图2
五、创建数据库及其对象2
5.1 建数据库和表2
5.2 查询实现3
五、软件功能设计5
六、界面设计6
七、应用程序6
7.1 站点查询6
7.2 线路查询7
7.3 站—站查询8
八、实验数据示例10
九、心得11
软件名称:公交线路查询系统
工程提出者:
工程开发者:
用户:广大需要乘坐公交车的人群
操作系统:Windows XP
软件配置:visual stutio2018,Microsoft SQLServer 2008 R2
数据库配置:Microsoft SQL Server 2008 R2在PC机中新建数据库,建立各个数据表、关系图。
城市的扩张使得公交线路越来越复杂,为了使得用户更加方便地能够获得最新公交线路,“公交线路查询工具”帮助人们解决这个问题。
本系统面向的对象是普通乘客,对于普通乘客来说,他们最关心的就是查询。系统也只对用户提供信息查询功能,并不对用户开放对数据的编辑权限。对于任何用户,只要浏览本系统的首页,就可以根据自己的需求进行查询。数据的修改、删除、及时更新工作只能由管理人员实现并对系统进行定期的维护,保证其运行的稳定性。
本系统从用户的需求出发,可实现以下功能:
1、线路具体情况查询:输入线路名称就可得到该线路的途经站点,发车和末班车时间,票价等相关信息。
2、经过此站点的所有路线查询:输入站点名称即可得出经过该站点的所有线路名称。
3、站点间的点到点路线查询:输入出发站点和目的站点即可输出可选择的线路,包括中间站的转乘。
1、数据项
名称 | 数据类型 | 长度 | 字段描述 |
name | varchar | 5 | 公交车的名称 |
start_time | time | 8 | 公交车每天的最早发车时间 |
end_time | time | 8 | 公交车每天的收班时间 |
buslength1 | int | 2 | 公交线路去程的站点数 |
buslength2 | int | 2 | 公交线路回程的站点数 |
pt_price | float | 3 | 普通车的票价 |
gd_price | float | 3 | 高等级车的票价 |
disprice_pt | float | 3 | 普通车刷卡后的价格 |
disprice_gd | float | 3 | 高等级车刷卡后的价格 |
companyID | int | 1 | 公司编号 |
company_name | varchar | 30 | 公司名称 |
station_id | int | 3 | 站点的编号 |
station_name | varchar | 30 | 站点的名称 |
orderid | int | 2 | 站点在一条线路中的位置 |
typeID | int | 2 | 线路类型编号,共12类 |
type_name | varchar | 5 | 线路类型名 |
direction | char | 4 | 公交行驶方向,“去程”或“回程” |
line | varchar | 500 | 公交行驶途径的站点 |
2、数据流
名称 | 说明 | 数据流来源 | 数据流去向 |
用户查询公交线路记录信息 | 记录公交车线路查询信息 | 线路+站点 | 查询结果输出 |
3、处理过程
名称 | 简述 | 输入的数据流 | 处理 | 输出的数据流 |
线路查询 | 根据公交线路查询该线路上的所有站点 | 公交车编号 | 根据编号查询 | 查询出该线路上的所有公交车站点 |
站点查询 | 根据站点查询出经过该站点的所有公交车编号 | 站点名称 | 根据站点查询公交车的编号 | 路过该站点的所有公交车编号 |
站站查询 | 根据两个站点查询出所有线路 | 两个不同的站点 | 根据站点查询出所有线路 | 输出查询出的线路 |
根据需求分析,可知本系统有以下E-R图:
1、公交站点E-R图 3、公交公司E-R图
2、公交车E-R图 4、公交线路类型E-R图
5、合并E-R图
根据ER模型,对本系统设计出5个关系表:
1、公交车表
bus<name,start_time,end_time,buslength1,buslength2,pt_price, dis_price,gd_price,company_id,type_id)
列名 | 数据类型 | NULL | 约束 | 说明 |
name | varchar(5> | not null | primary key | 公交车的名称 |
start_time | time | not null | 最早发车时间 | |
end_time | time | not null | 收班时间 | |
buslength1 | int | not null | 去程的站点数 | |
buslength2 | int | not null | 回程的站点数 | |
pt_price | float | null | 普通车的票价 | |
disprice_pt | float | null | 普通车刷卡 | |
gd_price | float | null | 高等级车的票价 | |
disprice_gd | float | null | 高等级车刷卡 | |
companyID | int | not null | foreignkeyreferencescompany(id>, | |
typeID | int | not null | Foreignkeyreferencesbus_type(id> | |
2、公交站点station<id,name)
列名 | 数据类型 | NULL | 约束 | 说明 |
id | int | not null | primary key | 站点的编号 |
name | varchar(30> | not null | 站点的名称 | |
3、线路与站点关联表bus_station<bus_name,station_id,orderid)
记录公交线路经过站点的信息,并且按顺序记录各站点,以此记录线路与站点的关联信息。
列名 | 数据类型 | NULL | 约束 | 说明 |
bus_name | varchar(5> | not null | primary key | 参照bus(name> |
station_id | int | not null | 参照station(id> | |
orderid | int | not null | 站点在一条线路中的位置 | |
4、公交公司表company(id,name>
列名 | 数据类型 | NULL | 约束 | 说明 |
id | int | Not null | primary key | 公司编号 |
name | Varchar(5> | Not null | 公司名称 | |
5、公交线路类型表bus_type(id,name>
列名 | 数据类型 | NULL | 约束 | 说明 |
id | int | Not null | primary key | 线路类型编号,共12类 |
name | Varchar(10> | Not null | 线路类型名 | |
6、公交线路表busline(bus_name,direction,line>
此表主要是为了方便依据线路来查询站点信息。
列名 | 数据类型 | NULL | 约束 | 说明 |
bus_name | varchar(4> | Not null | primary key | 公司编号 |
direction | varchar(4> | Not null | 公交行驶方向,“去程”或“回程” | |
line | varchar(500> | Not null | 公交行驶途径的站点 | |
下面是创建数据库和表的过程,数据通过手动输入。
1、创建数据库
createdatabasebus
on
(
name='bus_data1',
filename='C:\Documents and Settings\Administrator\桌面\bus\bus_data1.mdf'
>
logon
(
name='bus_log',
filename='C:\Documents and Settings\Administrator\桌面\bus\bus_log.ldf'
>
2、创建表
公交公司表 createtablecompany ( idintprimarykeynotnull, namevarchar(30>notnull > | 公交线路类型表 createtablebus_type ( idintprimarykeynotnull, namevarchar(5>notnull > |
公交站点 createtablestation ( idintprimarykeynotnull, namevarchar(15>notnull > | 公交线路表 createtablebusline (bus_namevarchar(5>, directionchar(2>notnull, linevarchar(200>notnull, primarykey(bus_name,direction>, foreignkey(bus_name>referencesbus(name>> |
公交车表 createtablebus (namevarchar(5>primarykeynotnull, start_timedatetimenotnull, end_timedatetimenotnull, buslengthintnotnull, pricefloatnotnull, companyIDintnotnull, typeIDintnotnull, foreignkey(companyID>referencescompany(id>, foreignkey(typeID>referencesbus_type(id>> | 线路与站点关联表 createtablebus_station ( bus_namevarchar(5>, station_idint, orderidintnotnull, primarykey(bus_name,station_id>, foreignkey(bus_name>referencesbus(name>, foreignkey(station_id>referencesstation(id> > |
这里只介绍线路查询和站站查询的存储过程实现,其它涉及到的功能通过SQLCommand或SqlDataAdapter实现,在后面的应用程序设计中实现。
1、线路具体情况查询:
ifexists(selectnamefromsysobjectswherename='busline_info'>
dropprocedurebusline_info
go
createprocedurebusline_info@inputvarchar(5>,@outputvarchar(700>output
as
begin
select@output=line
frombusline
wherebusline.bus_name=@input
end
go
2、站点间的点到点路线查询:
ifexists(selectnamefromsysobjectswherename='station_station'>
dropprocedurestation_station
go
createprocedurestation_station@input1varchar(30>,@input2varchar(30>,@output1varchar(5>output,@output2varchar(5>output,@output3varchar(5>output,@output4varchar(30>output,@output5varchar(30>output
as
begin
declare@stationID1int,@stationID2int--换乘过程中的中间站点
declare@id1int,@id2int--记录起点和终点的ID
select@id1=idfromstationwherename=@input1
select@id2=idfromstationwherename=@input2
--查找直达线路
select@output1=a.bus_name
from
(selectbus_namefrombus_stationwherestation_id=@id1>a,
(selectbus_namefrombus_stationwherestation_id=@id2>b
wherea.bus_name=b.bus_name
--若不存在直达线路,则搜寻一次换乘路线
ifnotexists(select*frombuswherename=@output1>
begin
--查找中间站点
select@stationID1=c.station_idfrom
--@input1能直达的站点集合c
(selectdistinctstation_id
frombus_station
wherebus_namein(selectbus_namefrombus_stationwherestation_id=@id1>>c,
--@input2能直达的站点集合d
(selectdistinctstation_id
frombus_station
wherebus_namein(selectbus_namefrombus_stationwherestation_id=@id2>>d
wherec.station_id=d.station_id
select@output1=a.bus_name
from
(selectbus_namefrombus_stationwherestation_id=@id1>a,
(selectbus_namefrombus_stationwherebus_station.station_id=@stationID1>b
wherea.bus_name=b.bus_name
select@output2=a.bus_name
from
(selectbus_namefrombus_stationwherestation_id=@id2>a,(selectbus_namefrombus_stationwherebus_station.station_id=@stationID1>b
wherea.bus_name=b.bus_name
end
--若不存在直达和一次换乘线路,则搜寻二次换乘路线
ifnotexists(select*frombuswherename=@output1>andnotexists(select*frombuswherename=@output2>
begin
--查找中间线路
select@output2=a.bus_name
from
(selectdistinctbus_name
frombus_station
wherestation_idin
(selectstation_idfrombus_station
wherebus_namein
(selectbus_namefrombus_stationwherestation_id=@id1>>>a,
(selectdistinctbus_namefrombus_station
wherestation_idin
(selectstation_idfrombus_stationwherebus_namein(selectbus_namefrombus_stationwherestation_id=@id2>>>b
wherea.bus_name=b.bus_name
--查找起点到中间线路的公交车
select@stationID1=a.station_id
from
(selectdistinctstation_idfrombus_stationwherebus_namein(selectbus_namefrombus_stationwherestation_id=@id1>>a,
(selectstation_idfrombus_stationwherebus_name=@output2>b
wherea.station_id=b.station_id
select@output1=c.bus_name
from
(selectbus_namefrombus_stationwherestation_id=@id1>c,
(selectbus_namefrombus_stationwherestation_id=@stationID1>d
wherec.bus_name=d.bus_name
--查找中间线路到终点的公交车
select@stationID2=a.station_id
from
(selectdistinctstation_idfrombus_stationwherebus_namein(selectbus_namefrombus_stationwherestation_id=@id2>>a,
(selectstation_idfrombus_stationwherebus_name=@output2>b
wherea.station_id=b.station_id
select@output3=c.bus_name
from
(selectbus_namefrombus_stationwherestation_id=@id2>c,
(selectbus_namefrombus_stationwherestation_id=@stationID2>d
wherec.bus_name=d.bus_name
end
select@output4=namefromstationwhereid=@stationID1
select@output5=namefromstationwhereid=@stationID2
end
软件共实现了3个大功能,即站点查询、线路查询、站站查询,其完成情况如下:
<一):检查用户是否输入了站点或线路信息,如果没有录入,则提示:请输入站点!或请输入线路名!
<二):检查用户是否输入的站点或线路是否存在,如果不存在,则提示:此站点不存在!或此线路不存在!
<三):如果信息都填写正确,则根据选定的查询条件进行查找,查找的具体实现为:
<1)站点查询:返回经过此站点的所有线路,并提供一条线路经过的所有站点。
如果没有结果,则提示:找不到此站点!
<2)线路查询:返回此线路的类型、最早和最晚时间、票价、经过站点等信息。
<3)站站查询:返回从起点到终点的可行的一条线路,此线路可直达,如果没有直达,则最多换乘两次,并提示在哪里下车换乘,以及所有车辆的站点信息。
//查询经过给定站点的公交线路
protectedvoid station_info(string input>
{
SqlConnection myConnection = newSqlConnection(connectionString>。
myConnection.Open(>。
SqlCommand cmd = newSqlCommand("select bus_station.bus_name as 公?交?线?路¡¤名?,busline.direction as 方¤?向¨°,line as 途ª?径?站?点Ì? from bus_station,station,busline where station.name = '" + input + "'and dbo.station.id = bus_station.station_id and bus_station.bus_name = busline.bus_name", myConnection>。
SqlDataAdapter da = newSqlDataAdapter(>。
da.SelectCommand = cmd。
System.Data.DataSet ds = new System.Data.DataSet(>。
da.Fill(ds>。
this.GridView1.DataSource = ds.Tables[0]。
GridView1.DataBind(>。
cmd = null。
myConnection.Close(>。
}
// 查询给定线路的公交车所经过站点
protectedstring busline_info1(string input>
{string connectionString = @"server=TXJM1FW3L1GYIC6。database = bus。Integrated Security = true"。
SqlConnection myConnection = newSqlConnection(connectionString>。
myConnection.Open(>。
SqlCommand myCommand = newSqlCommand("busline_info", myConnection>。
myCommand.CommandType = System.Data.CommandType.StoredProcedure。
//创建参数
SqlParameter bus_name = newSqlParameter("@input", System.Data.SqlDbType.VarChar, 5>。
bus_name.Direction = System.Data.ParameterDirection.Input。
bus_name.Value = input。
myCommand.Parameters.Add(bus_name>。
//添加输出参数
SqlParameter Result1 = newSqlParameter("@output", System.Data.SqlDbType.VarChar, 700>。
Result1.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result1>。
myCommand.ExecuteNonQuery(>。
string str = Result1.Value.ToString(>。
return str。
}
//查询给定起点和终点的公交车线路
protectedvoid station_station(string input1, string input2>
{
string connectionString = @"server=TXJM1FW3L1GYIC6。database = bus。Integrated Security = true"。
SqlConnection myConnection = newSqlConnection(connectionString>。
myConnection.Open(>。
//利用SQL中已有的存储过程实现站站查询
SqlCommand myCommand = newSqlCommand("station_station", myConnection>。
myCommand.CommandType = System.Data.CommandType.StoredProcedure。
//创建参数
SqlParameter station_name1 = newSqlParameter("@input1", System.Data.SqlDbType.VarChar, 30>。
station_name1.Direction = System.Data.ParameterDirection.Input。
station_name1.Value = input1。
myCommand.Parameters.Add(station_name1>。
SqlParameter station_name2 = newSqlParameter("@input2", System.Data.SqlDbType.VarChar, 30>。
station_name2.Direction = System.Data.ParameterDirection.Input。
station_name2.Value = input2。
myCommand.Parameters.Add(station_name2>。
//添加输出参数
SqlParameter Result1 = newSqlParameter("@output1", System.Data.SqlDbType.VarChar, 5>。
Result1.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result1>。
SqlParameter Result2 = newSqlParameter("@output2", System.Data.SqlDbType.VarChar, 5>。
Result2.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result2>。
SqlParameter Result3 = newSqlParameter("@output3", System.Data.SqlDbType.VarChar, 5>。
Result3.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result3>。
SqlParameter Result4 = newSqlParameter("@output4", System.Data.SqlDbType.VarChar, 30>。
Result4.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result4>。
SqlParameter Result5 = newSqlParameter("@output5", System.Data.SqlDbType.VarChar, 30>。
Result5.Direction = System.Data.ParameterDirection.Output。
myCommand.Parameters.Add(Result5>。
myCommand.ExecuteNonQuery(>。
string str1, str2, str3, str4, str5。
str1 = Result1.Value.ToString(>。
str2 = Result2.Value.ToString(>。
str3 = Result3.Value.ToString(>。
str4 = Result4.Value.ToString(>。
str5 = Result5.Value.ToString(>。
myConnection.Close(>。
//界面提示信息
if (str1 == "" && str2 == "" && str3 == "">
{
lab4_text.Text = "没有找到合适的路线"。
} else
if (str1 != "" && str2 != "" && str3 == "">
{
lab4_text.Text = "您可以先乘坐 " + str1 + " 在 " + str4 + " 下车," + " 再乘坐 " + str2。
lab5_name.Text = str1 + " 公交线路:"。
lab5_line.Text = busline_info1(str1>。
lab6_name.Text = str2 + " 公交线路:"。
lab6_line.Text = busline_info1(str2>。
}
else
if (str1 != "" && str2 == "" && str3 == "">
{
lab4_text.Text = "您可以直接乘坐 " + str1。
lab5_name.Text = str1 + " 公交线路:"。
lab5_line.Text = busline_info1(str1>。 }
else
if (str1 != "" && str2 != "" && str3 != "">
{
lab4_text.Text = "您可以先乘坐 " + str1 + " 在 " + str4 + " 下车," + " 再乘坐 " + str2 + " 在 " + str5 + " 下车," + " 最后再转乘 " + str3 + "即可"。
lab5_name.Text = str1 + " 公交线路:"。
lab5_line.Text = busline_info1(str1>。
lab6_name.Text = str2 + " 公交线路:"。
lab6_line.Text = busline_info1(str2>。
lab7_name.Text = str3 + " 公交线路:"。
lab7_line.Text = busline_info1(str3>。
}
}
1、bus表<存放公交车次有关信息数据)
2、bus_type表<存放公交车类型数据) 3、bus_type表<存放公交公司数据)
4、bus_station表<存放公交线路上的各个站点) 5、station表<存放公交站点数据)
6、bus_line表<存放公交线路,便于站点查询)
通过这次为期2个星期的数据库的课程设计实习,我从中学习到了一个数据库程序的设计的基本步骤,包括需求分析、概念结构设计、逻辑结构设计、物理结构设计等,同时加强了关系型数据库管理方面的部分知识。
我觉得需求分析是很重要的,只有需求分析做好了,后面的工作才会轻松。逻辑结构设计也很关键,需要考虑实体所应包含的所有属性,如果这个阶段不考虑清楚,等到后面才想起来的话,修改起来会比较麻烦。
在整个程序的设计时,我通过分析学习到了一个数据库程序应该考虑的方面,了解到关系型数据库关系处理方便的灵活性。同时在数据设计的过程中,让我知道了数据设计的重要性,在一定的选择判断中,设计出了数据库的整体结构。
通过在编程中嵌入SQL 语句访问数据库,处理返回数据,以及调用SQL中已经编写好的存储过程的过程中,巩固学习了SQL 语句的语法,也学会了在C#、ASP等编程语言中通过不同的方法使用数据库,自己收获还是很多的。
不足之处就在于,可能由于编程能力的原因,在如何让程序数据变得动态、多样这个方面还有待提高,程序的编写也应该更简洁一些才好。
这次实习给了我宝贵的机会,以充足的时间去编写一个基于数据库的小程序,让我对数据库和编写程序方面都得到了提高。
本文来源:https://www.2haoxitong.net/k/doc/b40bf6f9a55177232f60ddccda38376baf1fe0e0.html
文档为doc格式