服务器之家

服务器之家 > 正文

C#实现航班查询及预订功能

时间:2022-02-20 14:02     来源/作者:格调c

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FrmHangBanUser
{
public partial class FrmUser : Form
{
//1.连接字符串
string connString = "Data Source = .;Initial Catalog=Ticket;User ID = sa; Pwd = sa";
//3.创建DataSet对象
DataSet set = new DataSet();
public FrmUser()
{
InitializeComponent();
}
#region 窗体加载事件
/// <summary>
/// 窗体加载事件!!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmUser_Load(object sender, EventArgs e)
{
//FlightInfo();
AirwaysInfo();
CityInfo();
}
#endregion
#region 出发地
/// <summary>
/// 出发地
/// </summary>
public void AirwaysInfo()
{
try
{
//2.创建Connection对象
SqlConnection conn = new SqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT Id,CityName FROM CityInfo");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.填充数据集
adapter.Fill(set, "CityUser");
//6.绑定数据源到ComboBox控件中
this.cboAirways.DataSource = set.Tables["CityUser"];
this.cboAirways.ValueMember = "Id";
this.cboAirways.DisplayMember = "CityName";
//7.添加行对象
DataRow row = set.Tables["CityUser"].NewRow();
row["Id"] = -1;
row["CityName"] = "请选择";
set.Tables["CityUser"].Rows.InsertAt(row, 0);
//8.默认选中一行
this.cboAirways.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 非空验证
/// <summary>
/// 非空验证
/// </summary>
public void Check()
{
if(this.cboAirways.SelectedIndex == 0)
{
MessageBox.Show("请输入你要出发的城市啊!!","操作提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
}
else if (this.cboMudidi.SelectedIndex == 0)
{
MessageBox.Show("请输入你的目的地啊啊啊!!", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
}
}
#endregion
#region 目的地
/// <summary>
/// 目的地
/// </summary>
public void CityInfo()
{
try
{
//2.创建Connection对象
SqlConnection conn = new SqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT Id,CityName FROM CityInfo");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.填充数据集
adapter.Fill(set, "City");
//6.绑定数据源到ComboBox控件中
this.cboMudidi.DataSource = set.Tables["City"];
this.cboMudidi.ValueMember = "Id";
this.cboMudidi.DisplayMember = "CityName";
//7.添加行对象
DataRow row = set.Tables["City"].NewRow();
row["Id"] = -1;
row["CityName"] = "请选择";
set.Tables["City"].Rows.InsertAt(row, 0);
//8.默认选中一行
this.cboMudidi.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 航班信息
/// <summary>
/// 航班信息
/// </summary>
public void FlightInfo()
{
try
{
//2.创建Connection对象
SqlConnection conn = new SqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT F.FlightNO,A.Airways,F.LeaveTime,F.LandTime,F.Price
FROM FlightInfo AS F INNER JOIN AirwaysInfo AS A ON F.AirwaysId = A.Id");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.填充数据集
if (set.Tables["Airways"]!=null)
{
set.Tables["Airways"].Clear();
}
adapter.Fill(set, "Airways");
//6.绑定数据源
this.dvgUserInfo.DataSource = set.Tables["Airways"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 查询按钮功能
/// <summary>
/// 查询按钮功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnChaXun_Click(object sender, EventArgs e)
{
Check();
Filter();
}
#endregion
#region 按选择的条件筛选
/// <summary>
/// 按选择的条件筛选
/// </summary>
public void Filter()
{
try
{
SqlConnection conn=new SqlConnection(connString);
//筛选条件
DataSet ds = new DataSet();
int city = Convert.ToInt32(cboAirways.SelectedValue);
int Destination = Convert.ToInt32(cboMudidi.SelectedValue);
StringBuilder sb = new StringBuilder();
if(city!=-1 && Destination!=-1)
{
sb.AppendLine(@"SELECT F.FlightNO,A.Airways,F.LandTime,F.LeaveTime,F.Price
FROM FlightInfo AS F INNER JOIN AirwaysInfo AS A ON F.AirwaysId = A.Id");
sb.AppendFormat(@"WHERE LeaveCity = {0} AND Destination = {1}", city, Destination);
}
SqlDataAdapter adapter = new SqlDataAdapter(sb.ToString(),conn);
adapter.Fill(ds,"User");
this.dvgUserInfo.DataSource = ds.Tables["User"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 关闭按钮功能
/// <summary>
/// 关闭按钮功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGuanbi_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("你确定要退出程序嘛~","退出提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Stop);
if(result == DialogResult.OK)
{
Application.Exit(); //退出程序了。。 - - |||
}
}
#endregion
#region 实现航班选择功能
/// <summary>
/// 实现航班选择功能
/// </summary>
private void dvgUserInfo_MouseClick(object sender, MouseEventArgs e)
{
txtHNo.Text = dvgUserInfo.SelectedRows[0].Cells["FlightNO"].Value.ToString();
txtGongSi.Text = dvgUserInfo.SelectedRows[0].Cells["Airways"].Value.ToString();
this.txtChuFa.Text = this.cboAirways.Text;
this.txtMuDi.Text = this.cboMudidi.Text;
txtShijian.Text = dvgUserInfo.SelectedRows[0].Cells["LeaveTime"].Value.ToString();
txtDaoDa.Text = dvgUserInfo.SelectedRows[0].Cells["LandTime"].Value.ToString();
txtPiaoJia.Text = dvgUserInfo.SelectedRows[0].Cells["Price"].Value.ToString();
}
#endregion
#region 航班预定功能
/// <summary>
/// 航班预定功能
/// </summary>
/// <returns></returns>
public bool Insert()
{
Random _dom = new Random();
int no = _dom.Next(100000, 1000000);
SqlConnection conn = null;
string No = this.txtHNo.Text;
DateTime LeaveDate = this.dateTimePicker1.Value;
string Number = this.nuShang.Value.ToString();
try
{
conn = new SqlConnection(connString);
//构建插入学生记录的SQL的语句
StringBuilder _sbu = new StringBuilder();
_sbu.AppendLine("INSERT INTO OrderInfo(OrderId,FlightNo,LeaveDate,Number)");
_sbu.AppendFormat("VALUES('{0}','{1}','{2}','{3}')", no, No, LeaveDate, Number);
_sbu.AppendFormat("SELECT @@IDENTITY");
//创建Command对象
SqlCommand command = new SqlCommand(_sbu.ToString(), conn);
//打开连接
conn.Open();
//调用方法
int result = command.ExecuteNonQuery();
//对返回值进行处理
if (result > 0)
{
MessageBox.Show("恭喜你!预定成功!订单编号为"+no);
return true;
}
else
{
MessageBox.Show("预定失败!请重试!");
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
//关闭连接
conn.Close();
}
}
#endregion
#region 预定按钮
/// <summary>
/// 预定按钮!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnYuDing_Click(object sender, EventArgs e)
{
Insert();
}
#endregion
}
}

总结

以上所述是小编给大家介绍的C#实现航班查询及预订功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/wenlonghui/p/8452871.html

标签:

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部