航班查询及预订系统的主要代码块实现

【实现查询航班信息的功能】

/// <summary>
/// 窗体加载填充数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Flight_Load(object sender, EventArgs e)
{
try{
//查询用的sql语句
string sql = "select * from CityInfo";
//查询并填充数据集
DBHelper db = new DBHelper();
this.ds = new DataSet();//创建数据集对象
//创建DataAdapter
sda = new SqlDataAdapter(sql, db.Connection);
//填充数据集
sda.Fill(ds,"CityInfo");
//请选择实现
DataRow row = ds.Tables["CityInfo"].NewRow();
row["Id"] = -1;
row["CityName"] = "请选择";
ds.Tables["CityInfo"].Rows.InsertAt(row,0);
//绑定城市数据
this.cboCityInfo.DataSource = ds.Tables["CityInfo"];
this.cboCityInfo.ValueMember = "Id";
this.cboCityInfo.DisplayMember = "CityName";
//绑定目的地数据
DataView dv = new DataView(ds.Tables["CityInfo"]);
this.cboCitytermini.DataSource = dv;
this.cboCitytermini.ValueMember = "Id";
this.cboCitytermini.DisplayMember = "CityName";
}
catch(Exception ex){
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}

private void button1_Click(object sender, EventArgs e)
{
//查询数据并显示
if (cboCitytermini.Text != "请选择" && cboCitytermini.Text != "请选择")
{
int fromid = Convert.ToInt32(cboCityInfo.SelectedValue);
int toid = Convert.ToInt32(cboCitytermini.SelectedValue);
FlightSelect(fromid,toid);
}
}

【实现航班选择,分组框,文本框传值】

/// <summary>
/// 实现航班选择,文本框,分组框传值
/// </summary>
public void FlightDetails()
{
//在第二个分组框显示信息
//拿到当前选中项的航班号
string Flightnum = Convert.ToString(dgvTicketInfo.SelectedRows[0].Cells[0].Value); //航空公司
string Airways = Convert.ToString(dgvTicketInfo.SelectedRows[0].Cells[1].Value);
//出发时间
string LeaveTime = Convert.ToString(dgvTicketInfo.SelectedRows[0].Cells[2].Value);
//到达时间
string LandTime = Convert.ToString(dgvTicketInfo.SelectedRows[0].Cells[3].Value);
//成人票价
string Price = Convert.ToString(dgvTicketInfo.SelectedRows[0].Cells[4].Value);
//出发地
string formCity = cboCityInfo.Text;
//目的地
string toidCity = cboCitytermini.Text;
//赋值给第二个分组框text文本框各个值
txtFlightNum.Text = Flightnum.ToString();
txtAirways.Text = Airways;
txtLeaveTime.Text = LeaveTime;
txtLandTime.Text = LandTime;
txtPrice.Text = Price.ToString();
txtCityInfo.Text = formCity;
txtDetination.Text = toidCity;

}

【增加预订记录,实现航班预订功能】

/// <summary>
/// 增加预订记录,实现航班预订功能
/// </summary>
/// <returns></returns>
public int InsertAt()
{

//获取学生数据
//打开数据库连接
DBHelper db = new DBHelper();
Random r = new Random();
int OrderId = r.Next(1000,1000000);//6位随机数的订单编号

string FlightNo = Convert.ToString(txtFlightNum.Text);//航班号

int Number = Convert.ToInt32(numericUpDown1.Value);//预订数量

DateTime LeaveDate = DtlPicker.Value;//出发日期
try
{
//sql语句
string sql = "INSERT INTO OrderInfo([OrderId],[FlightNo],[LeaveDate],[Number]) VALUES('" + OrderId + "','" + FlightNo + "','" + LeaveDate + "','" + Number + "')";
//创建Command对象
SqlCommand comm = new SqlCommand(sql, db.Connection);
db.OpenConnection();
//执行命令
int result = comm.ExecuteNonQuery();
if (result > 0)
{
return OrderId;
}
else
{
return -1;
}
}
catch (Exception)
{
return -1;
}
finally
{
//关闭数据库连接
db.CloseConnection();

}
}

//点击预订按钮,订票
if (Convert.ToDateTime(DtlPicker.Text) < Convert.ToDateTime(DateTime.Now.ToString()))
{
MessageBox.Show("请选择正确的出发日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (this.txtFlightNum == null)
{
MessageBox.Show("请选择一个航班!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
int sum = InsertAt();
MessageBox.Show("预订成功!您的订单编号为:"+sum,"提示",MessageBoxButtons.OK);
}
}

原文地址:https://www.cnblogs.com/java-123/p/8523673.html