一、建立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();
}
}
}
}

 

版权声明:本文为chenyang520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenyang520/p/8529595.html