C#实现航班查询及预订
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#实现航班查询及预订的更多相关文章
- C#航空查询及预订
关于航空查询及预订项目中出现的问题 namespace Flight{ public partial class Flight : Form { public Flight() { Initializ ...
- HTML系列(二):头部meta元素
有关name: 一.页面关键字 网站关键字:用户通过搜索引擎能搜到该网站的词汇.最好控制在10个以内. 基本语法: <meta name="keywords" content ...
- 超实用的HTML代码段(赵荣娇)
第1章 创建HTML文档 11.1 HTML文档的基本结构 2 <html> <head> <title>Title of page</title> & ...
- Elasticsearch 复合查询——多字符串多字段查询
前言 有时我们在搜索电影的时候,包含了多个条件,比如主演是周星驰,打分8分以上,上映时间是1990年~2001年的,那么Elasticsearch又该如何帮我们做查询呢?这里我们可以用 bool 查询 ...
- UML基础与Rose建模实训教程
目 录 第1章 初识UML. 1 1.1 初识UML用例图... 1 1.2 初识UML类图... 3 第2章 Rational Rose工具... 6 2.1 安装与配置Rational Ro ...
- LR12.53—第4课:准备Vuser脚本进行负载测试
第4课:准备Vuser脚本进行负载测试 在前面的课程中,您确认您的Vuser脚本的回放产生了真正的用户的精确仿真.下一个步骤是准备的脚本负载测试.如何将多用户系统同时工作的工作?会拖慢系统到不可接受的 ...
- 整理CSS引发的相关理论的梳理
写在前面 因为原先项目中的CSS样式乱得不行,所以领导决定要花大时间整理一下样式,也为了后续维护起来方便.其实也苦了自己,想想也是一件多烦的事情,烦的原因并非是说这件事情做起来没有意义,而是觉得这样的 ...
- 图解Android - Binder 和 Service
在 Zygote启动过程 一文中我们说道,Zygote一生中最重要的一件事就是生下了 System Server 这个大儿子,System Server 担负着提供系统 Service的重任,在深入了 ...
- 安卓 Android题目大全
安卓001个人事务管理系统(单端) 安卓002手机订餐系统 安卓003无线点菜 安卓004酒店房间预定系统 安卓005个人相册管理系统(单端) 安卓006计算器(单端) 安卓007英语学习(单端) ...
随机推荐
- UWP: 通过命令行启动 UWP 应用
最近在开发应用的过程中,我遇到了如标题所述的需求,其实主要是为了能够快捷启动应用,正像我们可以在"运行"对话框中可以输入一些可执行程序的名称后,就能够直接启动它:这样做,可以增加 ...
- 栏目class导航
<div id="index_nav"> <div class="index_nav"> <ul> <!-- 调用栏目 ...
- Android + Eclipse + PhoneGap 环境配置
用了3天的时间,终于把环境搭建完毕,郁闷了N天,终于完成了.这里我只是讲述我安装的过程,仅供大家参考. 环境搭建首先要去下载一些安装包: (下载前注意一下,电脑是32位还是64位的请注意选择安装包) ...
- 真正从零开始,TensorFlow详细安装入门图文教程!
本文转载地址:https://www.leiphone.com/news/201606/ORlQ7uK3TIW8xVGF.html AI这个概念好像突然就火起来了,年初大比分战胜李世石的AlphaGo ...
- action之间传参为中文;type='redirect'和 type='redirectAction'主要区别
摘录自:http://blog.csdn.net/lhi705/article/details/7446156 Struts2中action之间传参中文乱码的问题 解决方法一(已经验证,可以): 两个 ...
- pyspider解析
https://www.cnblogs.com/microman/p/6111711.html #!/usr/bin/env python # -*- encoding: utf-8 -*- # Cr ...
- 堡垒机之paramiko模块
一.paramiko简单介绍 场景预设: 很多运维人员平时进行维护linux/unix主机时候,无非通过ssh到相应主机操作,那么一旦主机有成千上百台,那该如何应对,这时候我们需要批处理工具,基于py ...
- c指针作为参数传递以及指针的指针
指针作为函数参数传递 函数参数传递的只能是数值,所以当指针作为函数参数传递时,传递的是指针的值,而不是地址. #include "stdio.h" void pointer(int ...
- 【原创】区分png图片格式和apng图片格式的解决办法
最近公司有个项目,要抓取客户微信公众号的文章,以及文章内容中的图片,并且在图片加上客户自己的水印.我们使用阿里云OSS存储图片和加水印,发现真心好用,提升了我们的开发效率,阿里云现在是越来越强大了.. ...
- python 中 reversed()函数
一个列表a: a=[1,2,3,4,5,6,7] 一个对象b: b=reversed(a) 输出: print(b) <list_reverseiterator object at 0x0000 ...