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
}
}

原文地址:https://www.cnblogs.com/wenlonghui/p/8452871.html

时间: 2024-11-04 14:02:11

C#实现航班查询及预订的相关文章

喜羊羊系列之【项目一】:c基础+数据结构+线程+冒泡实现《航班查询系统》

博客:http://blog.csdn.net/muyang_ren 1.实现功能: 1)信息录入 2)信息显示(按照起飞时间先后顺序显示) 3)信息查询(可根据不同的关键字进行查询) 4)信息删除 2.  要求: 1)使用链表对录入的信息进行存储 2)对录入的信息进行排序(可按起飞时间进行排序) 3)Make管理项目 3.结构体: struct flydate{ int ID; //航班号 char staddress[18]; //起点站 char arraddress[18]; //终点站

航班查询

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Net; using System.IO; using System.IO.Compression; using System.Text.Re

航班查询接口

using System; using System.Text; using System.Net; namespace plane { class Program { static void Main(string[] args) { string name="CA5901"; string key = "**********************";//申请的key值 string url = "http://apis.haoservice.com/

航班查询接口文档

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Net;using System.IO;using System.IO.Compression;using System.Text.RegularExpr

简单试用航班查询接口

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Security;using System.Security.Cryptography.X509Certificates;using System.Net;using System.IO;using System.IO.Compression;using System.Text.RegularExpr

航班查询 接口

using System; using System.Text; using System.Net; namespace weizhang { class Program { static void Main(string[] args) { string name="CA5901"; string key = "**********************";//申请的key值 string url = "http://apis.haoservice.c

航班查询接口演示

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Net; using System.IO; using System.IO.Compression; using System.Text.Re

C#航空查询及预订

关于航空查询及预订项目中出现的问题 1.  容易忘随手修改控件 name 2. 容易忘记修改和不会修改一些控件的属性 如:防止在窗口程序运行时修改textbox中的数据的readonly属性 原文地址:https://www.cnblogs.com/www-zsl187-com/p/8523762.html

某航空项目第三方接口测试总结及接口测试技术的探讨

XX项目第三方接口测试总结及接口测试技术的探讨 1. 了解第三方接口 1.1 第三方接口的概念 XX项目第三方接口也就是电子客票网站扩展接口,主要功能是为了满足用户通过对接口的调用来替代原来用户必须进入网站才能进行相关操作的需求,方便代理人(大客户)提高自有系统流程的自动化程度.常见的第三方接口为商旅网接口包括携程.去哪儿等OTA及一些较大的代理人网站等. 1.2 网站与第三方接口的关系 接口的业务功能与现有网站功能必须一致,用户使用接口生成的订单,也可以通过登录网站来进行处理,如用户可以在第三