這篇文章給大家分享的是有關C#怎么實現航班查詢及預訂功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
公司主營業(yè)務:網站建設、網站制作、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現互聯網宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯推出大慶免費做網站回饋大家。
具體代碼如下所示:
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.創(chuàng)建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 出發(fā)地 /// <summary> /// 出發(fā)地 /// </summary> public void AirwaysInfo() { try { //2.創(chuàng)建Connection對象 SqlConnection conn = new SqlConnection(connString); //4.創(chuàng)建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("請輸入你要出發(fā)的城市?。?!","操作提示",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.創(chuàng)建Connection對象 SqlConnection conn = new SqlConnection(connString); //4.創(chuàng)建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.創(chuàng)建Connection對象 SqlConnection conn = new SqlConnection(connString); //4.創(chuàng)建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"); //創(chuàng)建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#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發(fā),繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優(yōu)雅的語法風格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。
感謝各位的閱讀!關于“C#怎么實現航班查詢及預訂功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享題目:C#怎么實現航班查詢及預訂功能
瀏覽路徑:http://jinyejixie.com/article20/pgeico.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站策劃、網站內鏈、網站制作、企業(yè)建站、服務器托管、ChatGPT
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯