數據庫系統開發——旅游公司機票查詢及預訂系統


摘   要

      本系統是主要針對機票查詢及預定功能開發的小型系統,通過SQL Server 2008數據庫和Visual Studio 2010的連接,在Visual Studio 2010中創建winform文件進行c#的程序編寫,實現對SQL Server2008數據庫中錄入的數據信息及表格信息的調用,從而完成機票的查詢與預訂。Visual Studio 2010調用數據庫中的信息后,通過可視化界面將航班信息顯示出來。普通用戶可進行用戶的注冊及密碼的修改,在登錄系統后選擇出發地、目的地后可查詢相應航班信息並進行預訂,若要取消行程可刪除訂單,實現了用戶預訂機票的基本功能,管理員可對航班信息和用戶信息進行管理操作。

 

關鍵詞:機票預定,機票查詢,c#,Visual Studio 2010,SQL Server 2008

  

目錄

第一章 需求分析

       1.1 功能需求

          1.1.1  系統功能模塊

           1.1.2預訂模塊

       1.2 數據流圖

       1.3 功能模塊圖

第二章 概念結構設計

       2.1 局部 E-R圖

       2.2 整體E-R圖

第三章 邏輯結構設計

       3.1 關系模式

       3.2 數據關系圖

第四章 物理結構設計

       4.1 存儲記錄結構設計

第五章 數據庫的實施

       5.1 建立數據庫

       5.2 數據表的建立

       5.3 存入數據

       5.4 存儲過程

       5.5 創建觸發器

第六章 系統實現

       6.1 系統核心代碼及運行截屏

第七章 總結

        7.1 總結及心得體會

附:程序使用說明

  

 

第一章 需求分析

1.1 功能需求

     隨着時代的發展,人們旅游出行的次數越來越多,旅游業越來越發達,針對旅游公司幫助客人訂機票的需求,建立此系統。旅游公司的訂票及預訂系統主要功能是查詢某一城市到另一城市的航班,並幫助游客預訂,在行程取消時可將訂單刪除。該系統能夠讓普通用戶查詢機票的航班號、航空公司、出發時間、到達時間和票價,並選擇預訂數量和出發日期,提供正確的身份信息后可成功預訂機票。系統管理員可以對航班信息和用戶信息進行管理操作,以滿足不同航班和不同用戶的信息更改等操作。

  1.1.1  系統功能模塊

 

      機票查詢及預定系統主要包括用戶登錄,航班信息的查詢、機票預訂功能、機票信息管理和用戶信息管理。

            (1)用戶登錄管理

      不同用戶設置不同的賬號及密碼,用賬號和密碼登錄后才能夠進行機票的查詢和預訂,用戶可以注冊新的賬號,修改已有賬號的密碼。

            (2)機票查詢管理

      選擇不同的出發地和目的地,點擊查詢,可查出不同的可預訂航班。

            (3)機票預定管理

      查詢出相應的航班后,可查看剩余票數,用戶自行選擇出發時間和預訂數量,點擊預訂,即可預訂成功。

            (4)  訂單管理

      訂單預訂成功后,若行程取消或改變,可根據訂單編號刪除原有訂單,重新進行預訂。

            (5)機票信息管理

      當航班信息發生改變或有新的航班時,管理員可登錄系統,對航班信息進行修改、刪除、添加。

            (6)用戶信息管理

      當系統中需要新的管理員時,原管理員可登錄系統,添加新的管理員賬號,也可刪除已有管理員。

  1.1.2預訂模塊

      本機票查詢及預定系統的主要功能是機票查詢和預訂:

            (1) 查詢所需航班。

            (2) 修改出發地、目的地、預訂數量和出發日期。

            (3) 增加和刪除預定信息及訂單。

            (4)管理員對機票信息和用戶信息進行增刪改的操作。

1.2 數據流圖

 

圖 1-1 數據流圖

 

1-2 管理員數據流圖

 

1.3 功能模塊圖

  

圖 1-3 功能模塊圖

 

 

第二章 概念結構設計

2.1 局部 E-R圖

  

圖 2-1 用戶E-R圖

 

  

 

圖 2-2  城市E-R圖

  

圖 2-3  航空公司E-R圖

 

  

圖 2-4 訂單E-R圖

 

  

圖 2-5 航班信息E-R圖

 

  

2.2 整體E-R圖

  

圖 2-6 整體E-R圖

 

 

第三章 邏輯結構設計

 3.1 關系模式

      該系統的關系模式主要有以下幾種:

            用    戶   表:usertable( account ,password

            管 理 員 表:managertable (account1,password1)

            乘   客   表:passneger (name,idcard)

            城市信息表:Cityinfo ( id , cityname )

            航空公司表:Airwaysinfo ( id , airways )

            航班信息表:Flightinfo ( flightno , airwaysid , leavecity , destination , leavetime , landtime , price )

            訂   單    表:Orderinfo ( orderinfo , flightno , leavedate , number )

3.2 數據關系圖

 

圖 3-1 數據關系圖

 

 

第四章 物理結構設計

4.1 存儲記錄結構設計

      在物理結構中,數據的基本存取單位是存儲記錄。有邏輯結構就可以設計存儲記錄結構,一個存儲記錄可以與多個邏輯記錄相對應。存儲記錄就包括記錄的組成,數據項的類型和長度等。

            (1)用戶表包括account和password

列名

字段類型

約束條件

account

Char(50)

主碼

password

Char(50)

主碼

             (2)Cityinfo 包括id和cityname

列名

字段類型

約束條件

Id

int

主碼

cityname

nvarchar(50)

非空

 

            (3)Airwaysinfo包括id和airways

列名

字段類型

約束條件

Id

int

主碼

airways

nvarchar(50)

非空

 

            (4)  Flightinfo包括flightno,airwaysid,leavecity,destination,leavetime,landtime,price,total

列名

字段類型

約束條件

flightno

Nvarchar(50)

主碼

airwaysid

int

非空

leavecity

int

非空

destination

int

非空

leavetime

nvarchar(50)

非空

landtime

nvarchar(50)

非空

price

money

非空

total

int

非空

 

            (5)Orderinfo包括orderinfo,flightno,leavedate,number

列名

字段類型

約束條件

orderinfo

int

主碼

flightno

nvarchar(50)

非空

leavedate

nvarchar(50)

非空

number

int

非空

(6)  Managertable包括 account1和password1

列名

字段類型

約束條件

account1

char(50)

主碼

password1

char(50)

主碼

 

(6)  passenger包括name和idcard

列名

字段類型

約束條件

name

char(50)

主碼

idcard

char(50)

主碼

 

 

 

 

第五章 數據庫的實施

5.1 建立數據庫

 create database MyTicket 

5.2 數據表的建立

 

 1 create table 用戶表 (
 2 account  char(50) not null,
 3 password  char(50) not null,
 4 primary key(帳號,密碼))
 5 
 6 create table cityinfo (
 7 id int   primary key,
 8 cityname nvarchar(50) not null)
 9 
10 create table airwaysinfo (
11 id int  primary key,
12 airways nvarchar(50) not null)
13 
14 create table flightinfo(
15 flightno nvarchar(50) primary key,
16 airwaysid int not null,
17 leavecity int not null,
18 destination int not null,
19 leavetime nvarchar(50) not null,
20 landtime nvarchar(50) not null,
21 price money not null,
22 total int not null,
23 foreign key(airwaysid) references airwaysinfo(id),
24 foreign key(leavecity) references cityinfo(id),
25 foreign key(destination) references airwaysinfo(id))
26 
27 create table orderinfo(
28 orderid int primary key,
29 flightno  nvarchar(50) not null,
30 leavedate nvarchar(50) not null,
31 number int not null,
32 foreign key(flightno) references flightinfo(flightno))
33 
34 create table  managertable(
35 account1 char(50),
36 password1 char(50),
37 primary key(account1,password1))
38 
39 create table passneger(
40 name char(50),
41 idcard char(50),
42 primary key(name,idcard))

5.3 存入數據

 1 insert into airwaysinfo values('1','中國國航')
 2 insert into airwaysinfo values('2','上海航空')
 3 insert into airwaysinfo values('3','山東航空')
 4 insert into airwaysinfo values('4','海南航空')
 5 insert into airwaysinfo values('5','東方航空')
 6 insert into airwaysinfo values('6','北京航空')
 7 
 8 insert into cityinfo values('1','上海')
 9 insert into cityinfo values('2','北京')
10 insert into cityinfo values('3','三亞')
11 insert into cityinfo values('4','沈陽')
12 insert into cityinfo values('5','杭州')
13 insert into cityinfo values('6','深圳')
14 
15 insert into flightinfo values('CA137','1','2','5','08:30','11:30','1200','100')
16 insert into flightinfo values('CA148','4','1','2','13:30','16:30','1450','100')
17 insert into flightinfo values('CA356','2','2','4','21:40','02:30','950','100')
18 insert into flightinfo values('CA888','3','4','6','06:50','10:20','1100','100')
19 insert into flightinfo values('CZ1437','5','3','1','22:30','03:30','1000','100')
20 insert into flightinfo values('CZ8137','2','6','5','11:30','14:30','800','100')
21 
22 insert into managertable values('yuanyuan','123456')

 

 

5.4 存儲過程

           1.城市表的存儲過程

1 create procedure pro_cityinfo
2 @id int,@cityname nvarchar(50)
3 as
4 insert into cityinfo (id,cityname)
5 values (@id,@cityname)

           2.航空公司表的存儲過程

1 create procedure pro_airwaysinfo
2 @id int,@airways nvarchar(50)
3 as
4 insert into airwaysinfo (id,airways)
5 values (@id,@airways)

           3.航班信息表的存儲過程

1 create procedure pro_flightinfo
2 @flightno nvarchar(50),@airwaysid int,@leavecity int,@destination int,
3 @leavetime nvarchar(50),@landtime nvarchar(50),@price money
4 as
5 insert into flightinfo (flightno,airwaysid,leavecity,
6 destination,leavetime,landtime,price)
7 values (@flightno,@airwaysid,@leavecity,
8 @destination,@leavetime,@landtime,@price)

           4.訂單表的存儲過程

1 create procedure pro_orderinfo
2 @orderinfo int,@flightno  nvarchar(50),@leavedate nvarchar(50),@number int
3 as
4 insert into orderinfo (orderinfo,flightno,leavedate,number)
5 values (@orderinfo,@flightno,@leavedate,@number)

           5.用戶表的存儲過程

1 create procedure pro_user
2 @account char(30),@password char(30)
3 as
4 insert into usertable (account,password)
5 values (@account,@password)

           6.管理員表的存儲過程

1 create procedure pro_managertable
2 @account1 char(30),@password1 char(30)
3 as
4 insert into usertable (account1,password1)
5 values (@account1,@password1)

          7.乘客表的存儲過程

1 create procedure pro_passneger
2 @name char(50),@idcard char(50)
3 as
4 insert into usertable (name,idcard)
5 values (@name,@idcard)

 

5.5 創建觸發器

        當在航班信息表中插入一條新的信息時,如果插入信息的城市名和航空公司未出現過,那么在航班公司表中插入新的航空公司名,在城市表中插入新的城市名。

           1.創建觸發器使cityinfo表里增加新的城市

           (1) 

1 create trigger tr_fli1 
2 on flightinfo for after insert
3 as
4 insert into cityinfo 
5 where id =(select leavecity from  inserted)

           (2)

1 create trigger tr_fli2
2 on flightinfo for after insert
3 as
4 insert into cityinfo 
5 where id =(select destination from  inserted)

           2.創建觸發器使航空公司表里增加新的航空公司id

1 create trigger tr_fli3 
2 on flightinfo for after insert
3 as
4 insert into airwaysinfo 
5 where id =(select airwaysid from  inserted)

          3.創建觸發器當orderinfo插入新數據時,flightinfo中的total即剩余機票數量應發生相應的更改。

1 create trigger up1 on orderinfo
2 after insert
3 as
4 update flightinfo set total = total - 
5 (select  number from orderinfo
6 where number = (select number from inserted))
7  where flightno =(
8 select flightno from inserted)

          4.創建觸發器當游客刪除訂單時,flightinfo中的total發生相應的更改。

 

1 create trigger up1 on orderinfo
2 after insert
3 as
4 update flightinfo set total = total - 
5 (select  number from orderinfo
6 where number = (select number from inserted))
7  where flightno =(
8 select flightno from inserted)

                  預訂成功界面:

 

 

 

                  預訂后剩余票數變化:

 

 

                   刪除訂單后剩余票數變化:

 

 

 

第六章 系統實現

6.1 系統核心代碼及運行結果

        (1)登錄界面代碼及運行結果

 1        private void button1_Click(object sender, EventArgs e)
 2         {
 3             string userno = txtUserNo.Text.Trim();//取出賬號
 4             string pw = textPassword.Text.Trim();//取出密碼
 5             //連接數據庫
 6             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
 7             SqlConnection mycon = new SqlConnection(constr);//實例化連接對象
 8             mycon.Open();
 9             //確定賬號和密碼
10             SqlCommand mycom = mycon.CreateCommand();       //創建SQL命令執行對象
11             string s1 = "select account,password from usertable where account='" + userno + "' and password='" + pw + "'";
12             //編寫SQL命令
13             mycom.CommandText = s1;                           //執行SQL命令
14             SqlDataAdapter myDA = new SqlDataAdapter();       //實例化數據適配器
15             myDA.SelectCommand = mycom;                       //讓適配器執行SELECT命令
16             DataSet myDS = new DataSet();                     //實例化結果數據集
17             int n = myDA.Fill(myDS, "register");         //將結果放入數據適配器,返回元祖個數
18             if (n != 0)
19             {
20                 MessageBox.Show("登錄成功!", "提示");
21                 this.Hide();
22                 FrmBookTicket frm = new FrmBookTicket();
23                 frm.Show();
24             }
25             else
26             {
27                 MessageBox.Show("用戶名或密碼錯誤,請重試!","提示");
28                 txtUserNo.Text = "";
29                 textPassword.Text = "";
30             }
31         }
登錄界面

 

 

 

 圖6-1 登錄界面

 

       (2) 注冊界面代碼及運行結果

 1 namespace BookTicket
 2 {
 3     public partial class Frmregister : Form
 4     {
 5         public Frmregister()
 6         {
 7             InitializeComponent();
 8         }
 9  
10  
11         private void Frmregister_Load_1(object sender, EventArgs e)
12         {
13             lbluserno.BackColor = Color.Transparent;
14             lbluserno.Parent = pictureBox1;
15  
16             lblpassword.BackColor = Color.Transparent;
17             lblpassword.Parent = pictureBox1;
18  
19             copassword.BackColor = Color.Transparent;
20             copassword.Parent = pictureBox1;
21         }
22  
23         private void button2_Click_1(object sender, EventArgs e)
24         {
25             this.Hide();
26             FrmLogin f1 = new FrmLogin();
27             f1.Show();
28         }
29  
30         private void button1_Click(object sender, EventArgs e)
31         {
32             //檢查是否已經存在
33             string userID = userno.Text.Trim();  //取出賬號
34             string password = userPw.Text.Trim();  //取出密碼
35             //連接數據庫
36             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
37             SqlConnection mycon = new SqlConnection(constr);                  //實例化連接對象
38             mycon.Open();
39  
40             //查詢新注冊的用戶是否存在
41             SqlCommand checkCmd = mycon.CreateCommand();       //創建SQL命令執行對象
42             string s = "select account from usertable where account='" + userID + "'";
43             checkCmd.CommandText = s;
44             SqlDataAdapter check = new SqlDataAdapter();       //實例化數據適配器
45             check.SelectCommand = checkCmd;                    //讓適配器執行SELECT命令
46             DataSet checkData = new DataSet();                 //實例化結果數據集
47             int n = check.Fill(checkData, "register");         //將結果放入數據適配器,返回元祖個數
48             if (n != 0)
49             {
50                 MessageBox.Show("用戶名存在");
51             }
52  
53             else if (userno.Text == "" || userPw.Text == "" || ensurePw.Text == "")
54             {
55                 MessageBox.Show("賬號或密碼不能為空!", "提示");
56             }
57             else if (userPw.Text != ensurePw.Text)
58             {
59                 MessageBox.Show("兩次密碼不一致!", "提示");
60  
61             }
62             else
63             {
64                 MessageBox.Show("注冊成功!", "提示");
65  
66                 StringBuilder sql = new StringBuilder();
67                 sql.Append("insert into usertable ([account],[password])");
68                 sql.AppendFormat(" Values ('{0}','{1}')  ", userID, password);
69  
70                 DBHelper helper = new DBHelper();
71  
72                 try
73                 {
74                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
75                     helper.OpenConnection();
76                     //執行增刪改
77                     int result = command.ExecuteNonQuery();
78                 }
79                 catch (Exception ex)
80                 {
81                     // Console.WriteLine(ex.Message);
82                     MessageBox.Show("發生錯誤,請聯系管理員" + ex.Message);
83                 }
84                 finally
85                 {
86                     helper.CloseConnection();
87                 }
88                 this.Hide();
89                 FrmLogin f1 = new FrmLogin();
90                 f1.Show();
91             }
92  
93         }
94    }
95 }
注冊界面

圖6-2 注冊界面

 

       (3) 修改密碼界面代碼及運行結果

 1 namespace BookTicket
 2 {
 3     public partial class Frmchange : Form
 4     {
 5         public Frmchange()
 6         {
 7             InitializeComponent();
 8         }
 9  
10         private void button2_Click(object sender, EventArgs e)
11         {
12             this.Hide();
13             FrmLogin f1 = new FrmLogin();
14             f1.Show();
15         }
16  
17         private void button1_Click(object sender, EventArgs e)
18         {
19             string userno = txtuserid.Text.Trim();//取出賬號
20             string pw = txtoldpw.Text.Trim();//取出密碼
21             string npw = txtnewpw.Text.Trim();//取出新密碼
22             //連接數據庫
23             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
24             SqlConnection mycon = new SqlConnection(constr);//實例化連接對象
25             mycon.Open();
26             //確定賬號和密碼
27             SqlCommand mycom = mycon.CreateCommand();       //創建SQL命令執行對象
28             string s1 = "select account,password from usertable where account='" + userno + "' and password='" + pw + "'";
29             //編寫SQL命令
30             mycom.CommandText = s1;                           //執行SQL命令
31             SqlDataAdapter myDA = new SqlDataAdapter();       //實例化數據適配器
32             myDA.SelectCommand = mycom;                       //讓適配器執行SELECT命令
33             DataSet myDS = new DataSet();                     //實例化結果數據集
34             int n = myDA.Fill(myDS, "register");              //將結果放入數據適配器,返回元祖個數
35             if (txtuserid.Text == "" || txtoldpw.Text == "" || txtnewpw.Text == "" || txtensurepw.Text == "")
36             {
37                 MessageBox.Show("賬號或密碼不能為空!", "提示");
38             }
39             else if (n == 0)
40             {
41                 MessageBox.Show("原密碼輸入錯誤!請重試", "提示");
42             }
43             else if (txtnewpw.Text != txtensurepw.Text)
44             {
45                 MessageBox.Show("兩次密碼不一致!", "提示");
46             }
47             else
48             {
49                 MessageBox.Show("修改密碼成功!", "提示");
50                 StringBuilder sql = new StringBuilder();
51                 sql.Append("update usertable set password = " + "'" + npw + "'" + " where account = " + "'" + userno+ "'" );
52                 DBHelper helper = new DBHelper(); 
53  
54                 try
55                 {
56                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
57                     helper.OpenConnection();
58                     //執行增刪改
59                     int result = command.ExecuteNonQuery();
60                 }
61                 catch (Exception ex)
62                 {
63                     // Console.WriteLine(ex.Message);
64                     MessageBox.Show("發生錯誤,請聯系管理員" + ex.Message);
65                 }
66                 finally
67                 {
68                     helper.CloseConnection();
69                 }
70                 this.Hide();
71                 FrmLogin f1 = new FrmLogin();
72                 f1.Show();
73             }
74  
75         }
76  
77         private void Frmchange_Load(object sender, EventArgs e)
78         {
79             label4.BackColor = Color.Transparent;
80             label4.Parent = pictureBox1;
81  
82             label1.BackColor = Color.Transparent;
83             label1.Parent = pictureBox1;
84  
85             label2.BackColor = Color.Transparent;
86             label2.Parent = pictureBox1;
87  
88             label3.BackColor = Color.Transparent;
89             label3.Parent = pictureBox1;
90         }
91     }
92 }
修改密碼界面

 

圖6-3 修改密碼界面

 

       (4) 機票查詢及預訂界面代碼及運行結果

 

  1   public partial class FrmBookTicket : Form
  2     {
  3         DataSet ds;  // 數據集   
  4  
  5         public FrmBookTicket()
  6         {
  7             InitializeComponent();
  8         }
  9  
 10         // 填充數據集,綁定到ComboBox顯示
 11         private void BookTicketForm_Load(object sender, EventArgs e)
 12         {
 13             grpFlightInfo.BackColor = Color.Transparent;
 14             grpFlightInfo.Parent = pictureBox1;//將pictureBox1設為標簽的父控件
 15             //pictureBox1.Controls.Add(label1);
 16             grpFlightInfo.Location = new Point(80, 80);
 17  
 18             grpOrderInfo.BackColor = Color.Transparent;
 19             grpOrderInfo.Parent = pictureBox1;
 20  
 21              
 22              
 23             // 查詢城市數據,填充數據集
 24             DBHelper helper = new DBHelper();
 25             string sqlCity = "SELECT * FROM CityInfo";
 26             SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCity, helper.Connection);
 27  
 28             ds = new DataSet();
 29  
 30             dataAdapter.Fill(ds, "CityInfo");
 31             DataRow row = ds.Tables["CityInfo"].NewRow();
 32             row[0] = "-1";
 33             row[1] = "請選擇";
 34             ds.Tables["CityInfo"].Rows.InsertAt(row, 0);
 35  
 36             // 獲得座位數據源的視圖
 37             DataView dvLeaveCity = new DataView(ds.Tables["CityInfo"]);
 38             DataView dvDestination = new DataView(ds.Tables["CityInfo"]);
 39              
 40             // 綁定數據源
 41             this.cboLeaveCity.DataSource = dvLeaveCity;
 42             this.cboLeaveCity.ValueMember = "Id";
 43             this.cboLeaveCity.DisplayMember = "CityName";
 44  
 45             this.cboDestination.DataSource = dvDestination;
 46             this.cboDestination.ValueMember = "Id";
 47             this.cboDestination.DisplayMember = "CityName";
 48  
 49             // 查詢航班信息,填充到數據集
 50             StringBuilder sqlFlight = new StringBuilder();
 51             sqlFlight.Append("SELECT F.[FlightNO],A.[Airways],F.[LeaveCity],F.[LeaveTime],F.[Destination],F.[LandTime],F.[Price]");
 52             sqlFlight.Append(" FROM FlightInfo AS F,AirwaysInfo AS A");
 53             sqlFlight.Append(" WHERE F.[AirwaysId] = A.[Id]");
 54             SqlDataAdapter adapter = new SqlDataAdapter(sqlFlight.ToString(), helper.Connection);            
 55             adapter.Fill(ds, "FlightInfo");
 56         }
 57  
 58         // 根據查詢條件,重新填充數據集
 59         private void btnSearch_Click(object sender, EventArgs e)
 60         {
 61             // 根據查詢條件顯示查詢結果
 62             ShowResult();
 63  
 64             // 清空預定部分的內容
 65             foreach (Control c in grpOrderInfo.Controls)
 66             {
 67                 if (c is TextBox)
 68                 {
 69                     ((TextBox)c).Text = string.Empty;
 70                 }
 71             }        
 72         }
 73  
 74         /// <summary>
 75         /// 綁定DataGridView顯示查詢結果
 76         /// </summary>
 77         private void ShowResult()
 78         {
 79             if (cboLeaveCity.Text == "請選擇" || cboDestination.Text == "請選擇")
 80             {
 81                 MessageBox.Show("請選擇出發地和目的地!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 82             }
 83             else
 84             {
 85                 DataView dvFlight = new DataView(ds.Tables["FlightInfo"]);
 86                 dvFlight.RowFilter = string.Format("LeaveCity={0} AND Destination={1}",
 87                     Convert.ToInt32(cboLeaveCity.SelectedValue),Convert.ToInt32(cboDestination.SelectedValue));
 88                 this.dgvTicketInfo.DataSource = dvFlight;
 89             }
 90         }
 91  
 92         // 根據用戶的選擇,顯示航班的詳細信息
 93         private void dgvTicketInfo_MouseClick(object sender, MouseEventArgs e)
 94         {
 95             if (dgvTicketInfo.Rows.Count > 0)
 96             {
 97                 // 顯示選中的航班信息
 98                 this.txtFlightNO.Text = dgvTicketInfo.CurrentRow.Cells["FlightNO"].Value.ToString();
 99                 this.txtAirways.Text = dgvTicketInfo.CurrentRow.Cells["Airways"].Value.ToString();
100                 this.txtFrom.Text = this.cboLeaveCity.Text;
101                 this.txtTo.Text = this.cboDestination.Text;
102                 this.txtLeaveTime.Text = dgvTicketInfo.CurrentRow.Cells["LeaveTime"].Value.ToString();
103                 this.txtLandTime.Text = dgvTicketInfo.CurrentRow.Cells["LandTime"].Value.ToString();
104                 this.txtPrice.Text = dgvTicketInfo.CurrentRow.Cells["Price"].Value.ToString();
105             }            
106         }
107  
108         // 單擊“預訂”按鈕,將預訂信息寫入數據庫       
109         private void btnOrder_Click(object sender, EventArgs e)
110         {
111             if (ValidateInput())
112             {
113                 // 獲得要插入的數據
114                 Random random = new Random();
115                 int orderId = random.Next(100000, 999999);  // 訂單編號
116                 string flightNo = this.txtFlightNO.Text;    // 預定的航班號
117                 string leaveDate = this.dtpDate.Value.ToShortDateString();// 出發日期
118                 int number = (int)this.numNumber.Value;
119  
120                 // 插入數據到數據庫
121                 StringBuilder sql = new StringBuilder();
122                 sql.Append("INSERT INTO OrderInfo ([OrderId],[FlightNo],[LeaveDate],[Number])");
123                 sql.AppendFormat(" Values ({0},'{1}','{2}',{3})  ", orderId, flightNo, leaveDate, number);
124  
125                 DBHelper helper = new DBHelper();
126  
127                 try
128                 {
129                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
130                     helper.OpenConnection();
131                     //執行增刪改
132                     int result = command.ExecuteNonQuery();
133                     if (result > 0)
134                     {
135                         MessageBox.Show("預定成功!訂單編號為" + orderId.ToString(),"提示");
136                        // MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
137                     }
138                     else
139                     {
140                         MessageBox.Show("預訂失敗,請重試!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
141                     }
142                 }
143                 catch (Exception ex)
144                 {
145                    // Console.WriteLine(ex.Message);
146                     MessageBox.Show("發生錯誤,請聯系管理員" );
147                 }
148                 finally
149                 {
150                     helper.CloseConnection();
151                 }
152                 /*this.Hide();
153                 Frmchange f1 = new Frmchange();
154                 f1.Show();*/
155             }
156         } 
157         /// <summary>
158         /// 驗證預訂部分的用戶輸入
159         /// </summary>
160         /// <returns>驗證成功返回True,失敗返回False</returns>
161         private bool ValidateInput()
162         {
163             if (txtFlightNO.Text == string.Empty)
164             {
165                 MessageBox.Show("請選擇一個航班!", "輸入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
166                 return false;
167             }
168             if (dtpDate.Value < DateTime.Now)
169             {
170                 MessageBox.Show("請選擇正確的出發日期!", "輸入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
171                 dtpDate.Focus();
172                 return false;
173             }
174             else
175             {
176                 return true;
177             }
178         } 
機票查詢及預訂

 

 

 

 

圖6-4 查詢與預訂機票界面

 

       (5)  新增乘客界面代碼及運行結果

 1 private void button1_Click_1(object sender, EventArgs e)
 2         {
 3             string id = textBox2.Text;
 4             //連接數據庫
 5             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
 6             SqlConnection mycon = new SqlConnection(constr);                  //實例化連接對象
 7             mycon.Open();
 8  
 9             if (textBox1.Text == "" || textBox2.Text == "")
10             {
11                 MessageBox.Show("姓名或身份證號不能為空!", "提示");
12             }
13             else if (id.Length < 18)
14             {
15                 MessageBox.Show("身份證號少於18位!","提示");
16             }
17             else
18             {
19                 MessageBox.Show("添加成功!", "提示");
20  
21                 StringBuilder sql = new StringBuilder();
22                 sql.Append("insert into passneger ([name],[idcard])");
23                 sql.AppendFormat(" Values ('{0}','{1}')  ", textBox1.Text, textBox2.Text);
24  
25                 DBHelper helper = new DBHelper();
26  
27                 try
28                 {
29                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
30                     helper.OpenConnection();
31                     //執行增刪改
32                     int result = command.ExecuteNonQuery();
33                 }
34                 catch (Exception ex)
35                 {
36                     // Console.WriteLine(ex.Message);
37                     MessageBox.Show("發生錯誤,請聯系管理員" + ex.Message);
38                 }
39                 finally
40                 {
41                     helper.CloseConnection();
42                 }
43                 this.Hide();
44                 FrmBookTicket f1 = new FrmBookTicket();
45                 f1.Show();
46             }
47         }
新增乘客

 

 

 圖6-5 新增乘客界面

 (6) 刪除訂單界面代碼及運行結果

 

 1 namespace BookTicket
 2 {
 3     public partial class Frmdelete : Form
 4     {
 5         public Frmdelete()
 6         {
 7             InitializeComponent();
 8         }
 9  
10         private void button1_Click(object sender, EventArgs e)
11         {
12             string orderid = textBox1.Text.Trim();
13             //連接數據庫
14             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
15             SqlConnection mycon = new SqlConnection(constr);//實例化連接對象
16             mycon.Open();
17             //確定訂單編號
18             SqlCommand checkCmd = mycon.CreateCommand();       //創建SQL命令執行對象
19             string s = "select orderid from orderinfo where orderid = '" + orderid + "'";
20             checkCmd.CommandText = s;
21             SqlDataAdapter check = new SqlDataAdapter();       //實例化數據適配器
22             check.SelectCommand = checkCmd; //執行select命令
23             DataSet checkData = new DataSet();                 //實例化結果數據集
24             int n = check.Fill(checkData, "register");         //將結果放入數據適配器,返回元祖個數
25             if (n != 0)
26             {
27                 StringBuilder sql = new StringBuilder();
28                 sql.Append("delete from OrderInfo where orderid = '" + orderid + "'");
29                  DBHelper helper = new DBHelper();
30  
31                 try
32                 {
33                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
34                     helper.OpenConnection();
35                     //執行增刪改
36                     int result = command.ExecuteNonQuery();
37                     if (result > 0)
38                     {
39                         MessageBox.Show("刪除成功!" ,"提示");
40                     }
41                     else
42                     {
43                         MessageBox.Show("刪除失敗,請重試!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
44                     }
45                 }
46                 catch (Exception ex)
47                 {
48                     // Console.WriteLine(ex.Message);
49                     MessageBox.Show("發生錯誤,請聯系管理員");
50                 }
51                 finally
52                 {
53                     helper.CloseConnection();
54                 }
55                 FrmBookTicket f1 = new FrmBookTicket();
56                 f1.Show();
57                 this.Close();
58             }
59         }
60         private void button2_Click(object sender, EventArgs e)
61         {
62             FrmBookTicket f1 = new FrmBookTicket();
63             f1.Show();
64             this.Close();
65         }
66  
67         private void Frmdelete_Load(object sender, EventArgs e)
68         {
69             label1.BackColor = Color.Transparent;
70             label1.Parent = pictureBox1;
71         }
72  
73     }
74 }
刪除訂單界面

 

圖6-6 刪除訂單界面

 

     

 (7)  管理員用戶主界面代碼及運行結果

 1  private void button2_Click(object sender, EventArgs e)
 2         {
 3             this.Hide();
 4             Frmuser f2 = new Frmuser();
 5             f2.Show();
 6         }
 7  
 8         private void button1_Click_1(object sender, EventArgs e)
 9         {
10             this.Hide();
11             Frmflight f1 = new Frmflight();
12             f1.Show();
13         }
管理員用戶主界面

 

 

 

 圖6-7 管理員用戶主界面

(8)  航班信息管理代碼及界面

 1 private void button2_Click(object sender, EventArgs e)
 2         {
 3             string flightno = txtflyno.Text.Trim();//取出賬號
 4             string airwayid = txtairway.Text.Trim();
 5             string leavecity = txtleave.Text.Trim();
 6             string destination = txtarrive.Text.Trim();
 7             string leavetime = txtltime.Text.Trim();
 8             string landtime = txtatime.Text.Trim();
 9             string price = txtprice.Text.Trim();
10             string total = txtto.Text.Trim();
11             //連接數據庫
12             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
13             SqlConnection mycon = new SqlConnection(constr);//實例化連接對象
14             mycon.Open();
15             //確定賬號和密碼
16             SqlCommand mycom = mycon.CreateCommand();       //創建SQL命令執行對象
17             string s1 = "select flightno from flightinfo where flightno='" + flightno + "'";
18             //編寫SQL命令
19             mycom.CommandText = s1;                           //執行SQL命令
20             SqlDataAdapter myDA = new SqlDataAdapter();       //實例化數據適配器
21             myDA.SelectCommand = mycom;                       //讓適配器執行SELECT命令
22             DataSet myDS = new DataSet();                     //實例化結果數據集
23             int n = myDA.Fill(myDS, "register");              //將結果放入數據適配器,返回元祖個數
24             if (n == 0)
25             {
26                 MessageBox.Show("航班不存在,請填寫正確的航班號", "提示");
27             }
28             if (n != 0)
29             {
30                 MessageBox.Show("航班信息修改成功!", "提示");
31                 StringBuilder sql = new StringBuilder();
32                 sql.Append("update flightinfo set airwaysid = " + "'" + airwayid + "'" + " where flightno = " + "'" + flightno + "'");
33                 sql.Append("update flightinfo set leavecity = " + "'" + leavecity + "'" + " where flightno = " + "'" + flightno + "'");
34                 sql.Append("update flightinfo set destination = " + "'" + destination + "'" + " where flightno = " + "'" + flightno + "'");
35                 sql.Append("update flightinfo set leavetime = " + "'" + leavetime + "'" + " where flightno = " + "'" + flightno + "'");
36                 sql.Append("update flightinfo set landtime = " + "'" + landtime + "'" + " where flightno = " + "'" + flightno + "'");
37                 sql.Append("update flightinfo set price = " + "'" + price + "'" + " where flightno = " + "'" + flightno + "'");
38                 sql.Append("update flightinfo set total = " + "'" + total + "'" + " where flightno = " + "'" + flightno + "'");
39                 DBHelper helper = new DBHelper();
40  
41                 try
42                 {
43                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
44                     helper.OpenConnection();
45                     //執行增刪改
46                     int result = command.ExecuteNonQuery();
47                 }
48                 catch (Exception ex)
49                 {
50                     // Console.WriteLine(ex.Message);
51                     MessageBox.Show("發生錯誤,請聯系管理員" + ex.Message);
52                 }
53                 finally
54                 {
55                     helper.CloseConnection();
56                 }
57                 txtflyno.Text = "";//取出賬號
58                 txtairway.Text = "";
59                 txtleave.Text = "";
60                 txtarrive.Text = "";
61                 txtltime.Text = "";
62                 txtatime.Text = "";
63                 txtprice.Text = "";
64                 txtto.Text = "";
65             }
66  
67         }
航班信息管理

 

 

  圖6-8 航班信息管理

(9)  用戶信息管理代碼及界面

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             //檢查是否已經存在
 4             string account1 = txtno.Text.Trim();  //取出賬號
 5             string password1 = txtpw.Text.Trim();  //取出密碼
 6             //連接數據庫
 7             string constr = @"Data Source=.;Initial Catalog=MyTicket;User ID=20173967;Pwd=20173967";
 8             SqlConnection mycon = new SqlConnection(constr);                  //實例化連接對象
 9             mycon.Open();
10  
11             //查詢新注冊的用戶是否存在
12             SqlCommand checkCmd = mycon.CreateCommand();       //創建SQL命令執行對象
13             string s = "select account1 from managertable where account1 ='" + account1 + "'";
14             checkCmd.CommandText = s;
15             SqlDataAdapter check = new SqlDataAdapter();       //實例化數據適配器
16             check.SelectCommand = checkCmd;                    //讓適配器執行SELECT命令
17             DataSet checkData = new DataSet();                 //實例化結果數據集
18             int n = check.Fill(checkData, "register");         //將結果放入數據適配器,返回元祖個數
19             if (n != 0)
20             {
21                 MessageBox.Show("用戶名存在");
22             }
23  
24             else if (txtno.Text == "" || txtpw.Text == "")
25             {
26                 MessageBox.Show("賬號或密碼不能為空!", "提示");
27             }
28             else
29             {
30                 MessageBox.Show("添加成功!", "提示");
31  
32                 StringBuilder sql = new StringBuilder();
33                 sql.Append("insert into managertable ([account1],[password1])");
34                 sql.AppendFormat(" Values ('{0}','{1}')  ", account1, password1);
35  
36                 DBHelper helper = new DBHelper();
37  
38                 try
39                 {
40                     SqlCommand command = new SqlCommand(sql.ToString(), helper.Connection);
41                     helper.OpenConnection();
42                     //執行增刪改
43                     int result = command.ExecuteNonQuery();
44                 }
45                 catch (Exception ex)
46                 {
47                     // Console.WriteLine(ex.Message);
48                     MessageBox.Show("發生錯誤,請聯系管理員" + ex.Message);
49                 }
50                 finally
51                 {
52                     helper.CloseConnection();
53                 }
54                 txtno.Text = "";
55                 txtpw.Text = "";
56             }
57         }
用戶信息管理

 

 

   圖6-9 用戶信息管理

 

(10)數據庫連接代碼 

 1 namespace BookTicket
 2 {
 3     static class Program
 4     {
 5         /// <summary>
 6         /// 應用程序的主入口點。
 7         /// </summary>
 8         [STAThread]
 9         static void Main()
10         {
11             Application.EnableVisualStyles();
12             Application.SetCompatibleTextRenderingDefault(false);
13             Application.Run(new FrmLogin());
14         }
15     }
16 }
數據庫連接

 

第七章 總結

7.1 總結及心得體會

  通過這次課程設計,對數據庫系統開發有了比較深入的了解。首先,我們領會到了系統分析的重要性,如:系統功能模塊分析和設計、數據庫需求分析和設計和數據庫概念結構設計(E-R圖),都在系統開發中占有重要的地位。在開始開發系統之前,要花費大量時間做好系統需求分析,這是系統開發的實用性,可行性,安全性的前提和保障。其次,在設計數據庫過程中,邊學習邊實踐,學會了數據庫結構設計、存儲過程及觸發器的構建。在設計系統過程中,我們自主學習了c#語言,做出了符合本系統要求的可視化界面。

  本旅游公司機票查詢及預訂系統成功記錄了航班信息、航空公司信息、用戶登錄和訂單信息,可以通過顯示的可視化頁面有效的幫助用戶查詢航班信息和預定航班。通過本次小學期的自主實踐,深刻了解了系統開發的全過程,同時也認識到自身開發經驗的不足,為以后的學習積累經驗並為以后的工作打下基礎。

 

附:程序使用說明

本系統的數據庫名稱為MyTicket,賬號為yuanyuan,密碼為123456。

一、程序描述

  該系統為在SQL Server 2008與Visual Studio 2010環境下設計的旅游公司機票查詢及預定系統,具有機票的查詢和預定功能等。在SQL Server 2008中創建數據庫MyTicket,在Visual Studio 2010中使用c#語言設計界面,並與SQL Server 2008數據庫中數據庫MyTicket連接。首先,使用者需要選擇自己的身份:用戶還是管理員,然后輸入賬號和密碼登錄該系統(新用戶需注冊賬號密碼),登錄成功后通過用戶可查詢得到所需的機票,選擇機票,預定機票,用戶如果預定錯誤可以進行刪除訂單,重新選擇訂單。以上操作機票的庫存數量均會自動進行修改;管理員可以增加,刪除,修改機票信息和新管理員信息。

二、功能

  本系統主要用於機票查詢和預定,所以提供以下四個子功能:

  1. 機票查詢:用戶通過輸入出發地、目的地查詢機票信息。
  2. 機票預定:查詢出機票信息后,選擇所需機票進行預定。
  3. 刪除機票:如果已經預定的機票信息錯誤,可進行刪除訂單操作,重新預定。
  4. 機票信息和新管理員的增加、刪除、修改:管理員登錄系統后,可對機票信息和新管理員進行增加、刪除和修改。

三、性能

  1. 輸入信息要求精度:航班的基本信息,例如:出發地、目的地、出發時間、到達時間、預定數量、出發日期
  2. 適用范圍:本系統適用於一般小型旅游公司預定機票。
  3. 運行環境:適用於一般需求的SQL Server 2008和Visual Studio 2010環境下運行。

四、變量數據

  1. account char
  2. password char
  3. id  int
  4. cityname nvarchar
  5. airways  nvarchar
  6. flightno  nvarchar
  7. leavecity int
  8. destination int
  9. leavetime nvarchar
  10. landtime nvarchar
  11. price money
  12. leavedate nvarchar
  13. orderid int
  14. number int
  15. name  char
  16. idcard  char

五、算法

  1. 用戶輸入賬戶和密碼登錄該系統,新用戶需注冊后登錄。
  2. 管理員輸入賬戶和密碼登錄該系統,新管理員需要原管理員幫其注冊。
  3. 若需修改密碼,則進入修改界面進行修改。
  4. 按照出發地、目的地的關鍵信息進行機票查詢,若無相應機票信息不顯示查詢結果。
  5. 選擇所需機票進行預定。
  6. 若預定失敗,重新返回查詢窗口,重新輸入信息再次查詢。
  7. 若需退票,則刪除訂單即可,刪除成功后相應的庫存數量會增加相應的票數。
  8. 當預定成功時,系統將給出預定成功的提示及訂單號,將訂票信息結果插入數據庫中保存,預訂成功后庫存機票數量會相應地減少。
  9. 管理員可以對機票信息和新管理員信息進行增加、刪除和修改


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM