C#实现航班预订系统

 更新时间:2022年5月27日 23:14  点击:249 作者:Xuxiaoooo

本文实例为大家分享了C#实现航班预订的具体代码,供大家参考,具体内容如下

连接数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace MyTickets
{
    public class DBHelper
    {
        //数据库连接字符串
        public string connString = @"Data Source=.;Initial Catalog=MyTicket;Trusted_Connection=Yes;Connect Timeout=90";
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        private SqlConnection connction;
    public SqlConnection Connction
        {
            get
            {
                if (connction == null)
                {
                    connction = new SqlConnection(connString);
                }
                return connction;
            }
        }
        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public void OpenConnection()
        {
            if (connction.State == ConnectionState.Closed)
            {
                Connction.Open();
            }
            else if (connction.State == ConnectionState.Broken)
            {
                Connction.Close();
                Connction.Open();
            }
           
        }
        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        public void CloseConnection()
        {
            if(connction.State==ConnectionState.Open|| connction.State == ConnectionState.Broken)
            {
                Connction.Close();
            }
        }

    }
}

开头动画代码

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.Threading;

namespace MyTickets
{
    public partial class 开头 : Form
    {
        public 开头()
        {
            InitializeComponent();
            timer1.Interval = 1000;
            timer1.Start();
            timer1.Tick += new EventHandler(timer1_Tick);
        }


        private void 开头_Load(object sender, EventArgs e)
        {
            TransparencyKey = BackColor;
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            初始界面 f0 = new 初始界面();
            this.Hide();
            f0.ShowDialog();
        }
    }
}

机票预订界面

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 MyTickets
{
    public partial class 机票预订 : Form
    {
        #region 构造函数
        public 机票预订()
        {
            InitializeComponent();
        }

        public 机票预订(string text)
        {
            InitializeComponent();
            usename.Text = text;
        }
        #endregion

        #region 方法

        #region 绑定cbo
        /// <summary>
        /// 绑定cbo
        /// </summary>
        private void BindCbo()
        {
            DBHelper dbHelper = new DBHelper();
            //sql语句
            string sql = "select * from cityInfo";
            //适配器adapter
            SqlDataAdapter adapter = new SqlDataAdapter(sql, dbHelper.Connction);
            //数据集
            DataSet ds = new DataSet();
            //填充数据集
            adapter.FillSchema(ds, SchemaType.Source, "cityInfo");
            adapter.Fill(ds, "cityInfo");
            //新的一行
            DataRow row = ds.Tables["cityInfo"].NewRow();
            row[0] = -1;
            row[1] = "请选择";
            //插入
            ds.Tables["cityInfo"].Rows.InsertAt(row, 0);
            //获取视图
            DataView dv1 = new DataView(ds.Tables["cityInfo"]);
            DataView dv2 = new DataView(ds.Tables["cityInfo"]);
            //绑定
            this.cboDestinationCity.DataSource = dv1;
            this.cboDestinationCity.DisplayMember = "cityName";
            this.cboDestinationCity.ValueMember = "id";

            this.cboLeaveCity.DataSource = dv2;
            this.cboLeaveCity.DisplayMember = "cityName";
            this.cboLeaveCity.ValueMember = "id"; 
        }
        #endregion

        #region 绑定dgv
        /// <summary>
        /// 绑定dgv
        /// </summary>
        private void BindDgv()
        {
            DBHelper dbHelper = new DBHelper();
            string sql = string.Format(@"select flightNo,airways,leaveTime,landTime,price
                                        from flightInfo,airwaysInfo
                                        where flightInfo.airwaysId=airwaysInfo.id
                                        and leaveCity={0}
                                        and destinationCity={1}", cboLeaveCity.SelectedValue, cboDestinationCity.SelectedValue);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, dbHelper.Connction);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "flightInfo");
            this.dataGridView1.DataSource = ds.Tables["flightInfo"];
        }
        #endregion

        #region 验证预订部分的用户输入
        /// <summary>
        /// 验证预订部分的用户输入
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            if (txtFlightNo.Text == string.Empty)
            {
                MessageBox.Show("请选择一个航班!");
                return false;
            }
            if (dateTimePicker1.Value < DateTime.Now)
            {
                MessageBox.Show("请选择正确的出发日期!");
                dateTimePicker1.Focus();
                return false;
            }
            return true;
        } 
        #endregion

        #endregion

        #region 事件
        //加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            BindCbo();
            TransparencyKey = BackColor;

        }
        //查询事件
        private void tbnQuery_Click(object sender, EventArgs e)
        {
            if(cboLeaveCity.Text=="请选择"||cboDestinationCity.Text=="请选择")
            {
                MessageBox.Show("请选择出发地与目的地!");
                this.dataGridView1.DataSource = null;
                return;
            }
            BindDgv();
            //清空txt
            foreach (Control c in groupBox2.Controls)
            {
                if(c is TextBox)
                {
                    c.Text = string.Empty;
                }
            }
        }
        //单击dgv
        private void dataGridView1_Click(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count>0)
            {
                this.txtFlightNo.Text = dataGridView1.CurrentRow.Cells["flightNo"].Value.ToString();
                this.txtAirways.Text = dataGridView1.CurrentRow.Cells["airways"].Value.ToString();
                this.txtFrom.Text = cboLeaveCity.Text;
                this.txtTo.Text = cboDestinationCity.Text;
                this.txtLeaveTime.Text = dataGridView1.CurrentRow.Cells["leaveTime"].Value.ToString();
                this.txtLandTime.Text = dataGridView1.CurrentRow.Cells["landTime"].Value.ToString();
                this.txtPrice.Text = dataGridView1.CurrentRow.Cells["price"].Value.ToString();
            }
        }
        //点击关闭
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //点击预定
        private void button1_Click(object sender, EventArgs e)
        {
            if(ValidateInput())
            {
                Random random = new Random();
                int orderId= random.Next(100000, 9999999);
                string flightNo = this.txtFlightNo.Text;
                string leaveDate = this.dateTimePicker1.Value.ToShortDateString();
                string landTime = this.txtLandTime.Text;
                string price = this.txtPrice.Text;
                int num = (int)this.numNunber.Value;
                string leavePlace = this.txtFrom.Text;
                string landPlace = this.txtTo.Text;
                string usename = this.usename.Text;
                string sql = string.Format(@"insert into orderInfo(orderId,flightNo,leaveDate,landTime,price,Number,leavePlace,landPlace,useId)
                                            values({0},'{1}','{2}','{3}','{4}',{5},'{6}','{7}','{8}')", orderId, flightNo, leaveDate,landTime,price,num, leavePlace, landPlace,usename);
                DBHelper dbHelper = new DBHelper();
                try
                {
                    //执行工具
                    SqlCommand cmd = new SqlCommand(sql, dbHelper.Connction);
                    //打开数据库
                    dbHelper.OpenConnection();
                    //执行
                    int result =cmd.ExecuteNonQuery();
                    //判断
                    if(result>0)
                    {
                        MessageBox.Show("预订成功!订单编号是:" + orderId);
                    }
                    else
                    {
                        MessageBox.Show("预定失败,请重试!");
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show("发生错误,请联系管理员,错误原因是:"+ex.Message);
                }
                finally
                {
                    dbHelper.CloseConnection();
                }
            }
        }

        #endregion

        #region 鼠标移动
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键
        private void 机票预订_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;

            }
        }

        private void 机票预订_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {

                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void 机票预订_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion

    }
}

订单查询界面

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 MyTickets
{
    public partial class 订单查询 : Form
    {
        public 订单查询(string text)
        {
            InitializeComponent();
            usename.Text = text.ToString();
        }
        #region 鼠标移动
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键
        private void 订单查询_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;

            }
        }

        private void 订单查询_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {

                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void 订单查询_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion

        private void 订单查询_Load(object sender, EventArgs e)
        {
            DBHelper dbHelper = new DBHelper();
            string sql = string.Format(@"select orderId,flightNo,leaveDate,landTime,leavePlace,landPlace from orderInfo where useId ='{0}'",usename);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, dbHelper.Connction);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "orderInfo");
            this.dataGridView1.DataSource = ds.Tables["orderInfo"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

登录界面

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.Threading;
using System.Data.SqlClient;


namespace MyTickets
{
    public partial class 初始界面 : Form
    {

        public 初始界面()
        {
            InitializeComponent();
        }
        int count = 0;

        #region 鼠标移动
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键
        private void 初始界面_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;

            }
        }

        private void 初始界面_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {

                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void 初始界面_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion

        #region 打开其他窗口

        private void 查询订单btn_Click(object sender, EventArgs e)
        {
            string userid = this.用户名TEXT.Text;
            string psword = this.passwordtxt.Text;
            if (userid.Equals("") || psword.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
            }
            else
            {
                string sql = string.Format(@"select useId,psword from LoginIn where useId = '{0}' and psWord = '{1}'", userid, psword);
                DBHelper dbHelper = new DBHelper();
                SqlCommand cmd = new SqlCommand(sql, dbHelper.Connction);
                dbHelper.OpenConnection();
                SqlDataReader sdr = cmd.ExecuteReader();

                if (sdr.Read())
                {
                    MessageBox.Show("信息验证成功");

                    //跳转到主页面
                    订单查询 fd = new 订单查询(用户名TEXT.Text);
                    fd.ShowDialog();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
        }
        #endregion

        #region 轮播
        private void ChangeImage(Image img, int millisecondsTimeOut)
        {
            if (this.IsHandleCreated)
            {
                this.Invoke(new Action(() =>
                {
                    轮播1.Image = img;
                })
                );
            }
            Thread.Sleep(millisecondsTimeOut);
        }
        private void 初始界面_Load(object sender, EventArgs e)
        {
            Thread th;
            th = new Thread
                (
                    delegate ()
                    {
                        // 3就是要循环轮数了
                        for (int i = 0; i < 100; i++)
                        {
                            //调用方法
                            ChangeImage(Properties.Resources.东方航空, 2000);
                            count++;
                            ChangeImage(Properties.Resources.南方航空, 2000);
                            count++;
                            ChangeImage(Properties.Resources.四川航空, 2000);
                            count++;
                            ChangeImage(Properties.Resources.海南航空, 2000);
                            count++;
                        }
                    }
                );
            th.IsBackground = true;
            th.Start();
        }

        //关闭
        private void label1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //轮播
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (count % 4 == 0)
            {
                System.Diagnostics.Process.Start("http://www.ceair.com/");
            }
            if (count % 4 == 3)
            {
                System.Diagnostics.Process.Start("https://www.hnair.com/");
            }
            if (count % 4 == 1)
            {
                System.Diagnostics.Process.Start("https://www.csair.com/");
            }
            if (count % 4 == 2)
            {
                System.Diagnostics.Process.Start("http://www.sichuanair.com/");
            }
        }
        #endregion

        #region 绑定登录
        private void 登录btn_Click(object sender, EventArgs e)
        {
            string userid = this.用户名TEXT.Text;
            string psword = this.passwordtxt.Text;
            if (userid.Equals("") || psword.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
            }
            else
            {
                string sql = string.Format(@"select useId,psword from LoginIn where useId = '{0}' and psWord = '{1}'",userid, psword);
                DBHelper dbHelper = new DBHelper();
                SqlCommand cmd = new SqlCommand(sql, dbHelper.Connction);
                dbHelper.OpenConnection();
                SqlDataReader sdr = cmd.ExecuteReader();

                if (sdr.Read())
                {
                    MessageBox.Show("信息验证成功");

                    //跳转到主页面
                    机票预订 f2 = new 机票预订(用户名TEXT.Text);
                    this.Hide();
                    f2.ShowDialog();

                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
            

        }   
        #endregion
        
        #region 绑定注册
        private void 注册btn_Click(object sender, EventArgs e)
        {
            string userid = this.用户名TEXT.Text;
            string psword = this.passwordtxt.Text;
            string sql = string.Format(@"insert into LoginIn(useId,psWord)
                                            values('{0}','{1}')", userid, psword);
            DBHelper dbHelper = new DBHelper();
            try
            {
                //执行工具
                SqlCommand cmd = new SqlCommand(sql, dbHelper.Connction);
                //打开数据库
                dbHelper.OpenConnection();
                //执行
                int result = cmd.ExecuteNonQuery();
                //判断
                if (result > 0)
                {
                    MessageBox.Show("注册成功,请登录!");
                }
                else
                {
                    MessageBox.Show("注册失败,请重试!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误,请联系管理员,错误原因是:" + ex.Message);
            }

        }
        #endregion


    }
}

下面是一些效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/qq_43182421/article/details/90315820

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25