<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了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 } }
下面是一些效果圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45