重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家分享的是有关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 窗体加载事件 ////// 窗体加载事件!! /// /// /// private void FrmUser_Load(object sender, EventArgs e) { //FlightInfo(); AirwaysInfo(); CityInfo(); } #endregion #region 出发地 ////// 出发地 /// 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 非空验证 ////// 非空验证 /// 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 目的地 ////// 目的地 /// 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 航班信息 ////// 航班信息 /// 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 查询按钮功能 ////// 查询按钮功能 /// /// /// private void btnChaXun_Click(object sender, EventArgs e) { Check(); Filter(); } #endregion #region 按选择的条件筛选 ////// 按选择的条件筛选 /// 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 关闭按钮功能 ////// 关闭按钮功能 /// /// /// private void btnGuanbi_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("你确定要退出程序嘛~","退出提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Stop); if(result == DialogResult.OK) { Application.Exit(); //退出程序了。。 - - ||| } } #endregion #region 实现航班选择功能 ////// 实现航班选择功能 /// 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 航班预定功能 ////// 航班预定功能 /// ///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 预定按钮 /// /// 预定按钮! /// /// /// private void btnYuDing_Click(object sender, EventArgs e) { Insert(); } #endregion } }
C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
感谢各位的阅读!关于“C#怎么实现航班查询及预订功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!