模拟航班查询及预定系统 编写示例
一、建立C#窗體
所需控件:
Label標簽 ?
Button 按鈕
TextBox 文本框
ComboBox 組合框
DATaGridView 數據顯示
DateTimePicker 日期表
NumericUpDown 數字選擇
二、建立后臺數據庫
大概需要四張表
1,航空公司表
2,城市信息表
3,航班信息表
3,訂單信息表
三、向comboBox中插入城市信息供用戶選擇
示例SQL語句:
select CityName from CityInfo
四、讓DataGridView顯示查詢到的數據
示例SQL語句:
int AirId = comboBox1.SelectedIndex ;
int DestinationId = comBox2.SelectedIndex;
select * from FlightInfo as F,AirwaysInfo as A where?AirwaysInfo.Id = FlightInfo.AirwaysId and?FlightInfo.LeaveCity='" + AirId + "' and FlightInfo.Destination='" + DestinationId + "
將行顯示屬性 設置為 FullRowSelect
并將每個值顯示在各個TextBox 文本框
五、預定訂單
獲取訂單數量,用戶選擇的航班信息和日期
添加到返回到數據庫中,預定成功
?
全部參考代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BookPlane
{
public partial class FIightOrderMSG : Form
{
public FIightOrderMSG()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FillDataGridView();
}
private void Form1_Load(object sender, EventArgs e)
{
dgvList.AutoGenerateColumns = false;
FindCityInfo();
}
DBHelper db = new DBHelper();
/// <summary>
/// 查詢航班出發地
/// </summary>
public void FindCityInfo()
{
string sql = "select * from CityInfo";
try
{
db.conOpen();
SqlCommand comm = new SqlCommand(sql, db.conn);
SqlDataReader reader = comm.ExecuteReader();
cboStart.Items.Add("請選擇");
cboStart.Items.IndexOf(0);
cboStart.SelectedIndex = 0;
cboEnd.Items.Add("請選擇");
cboEnd.Items.IndexOf(0);
cboEnd.SelectedIndex = 0;
if (reader.HasRows)
{
while (reader.Read())
{
string city = reader["CityName"].ToString();
cboStart.Items.Add(city);
cboEnd.Items.Add(city);
}
reader.Close();
}
else
{
MessageBox.Show("未檢測到有效信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception)
{
MessageBox.Show("發生錯誤!");
}
finally
{
db.conClose();
}
}
/// <summary>
/// 填充DataGridView中的數據
/// </summary>
public void FillDataGridView()
{
//連接數據庫
SqlConnection conn = new SqlConnection(db.strCon);
//創建SQL語句
StringBuilder sql = new StringBuilder();
//獲取comboBox被選中的下標
int AirId = cboStart.SelectedIndex;
int DestinationId = cboEnd.SelectedIndex;
//創建SQL語句
sql.AppendLine(" select * from FlightInfo,AirwaysInfo");
sql.AppendLine(" where AirwaysInfo.Id = FlightInfo.AirwaysId");
sql.AppendLine(" and FlightInfo.LeaveCity='" + AirId + "' and FlightInfo.Destination='" + DestinationId + "'");
//創建適配器,卡車對象
SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), conn);
DataSet ds = new DataSet();
da.Fill(ds, "air");
dgvList.DataSource = ds.Tables["air"];
//string FlightNo = reader["FlightNo"].ToString();
//string Airways = reader["Airways"].ToString();
//string LeaveTime = reader["LeaveTime"].ToString();
//string LandTime = reader["LandTime"].ToString();
//string Price = reader["Price"].ToString();
}
private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
{
//獲取DataGridView中的值,顯示在各個文本框中
txtFlightNo.Text = Convert.ToString(dgvList.SelectedCells[0].Value);
txtFlightCompany.Text = dgvList.SelectedRows[0].Cells[1].Value.ToString();
txtStartTime.Text = dgvList.SelectedRows[0].Cells[2].Value.ToString();
txtEndTime.Text = dgvList.SelectedRows[0].Cells[3].Value.ToString();
txtPrice.Text = dgvList.SelectedRows[0].Cells[4].Value.ToString();
txtStart.Text = cboStart.SelectedItem.ToString();
txtEnd.Text = cboEnd.SelectedItem.ToString();
}
private void btnOrder_Click(object sender, EventArgs e)
{
//產生6位隨機數
Random r = new Random();
string random = Convert.ToString(r.Next(100000, 1000000));
string flightNo = txtFlightNo.Text;
int num = Convert.ToInt32(nudOrderNum.Value);
if (txtFlightCompany.Text != string.Empty)
{
if (dtpStart.Value < DateTime.Now)
{
MessageBox.Show("請選擇一個有效的時間!", "提示");
}
else
{
SqlConnection conn = new SqlConnection(db.strCon);
DateTime leaveTime = dtpStart.Value;
try
{
//連接數據庫
conn.Open();
string sql = @"insert into OrderInfo values('" + random + "','" + flightNo + "','" + leaveTime + "'," + num + ")";
SqlCommand comm = new SqlCommand(sql, conn);
int isReact = comm.ExecuteNonQuery();
if (isReact > 0)
{
MessageBox.Show("預定成功!祝您旅途愉快!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("發生錯誤!" + ex);
}
finally
{
conn.Close();
}
}
}
else
{
MessageBox.Show("請選擇一個航班!", "提示");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("確定要關閉嗎?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
this.Close();
}
}
}
}
?
轉載于:https://www.cnblogs.com/chenyang520/p/8529595.html
總結
以上是生活随笔為你收集整理的模拟航班查询及预定系统 编写示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到两口红色棺材代表什么
- 下一篇: 梦到蛇咬人是什么意思周公解梦