using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class frmhangban : Form
{
private DataSet dataSet = new DataSet(); //声明初始化DataSet
private SqlDataAdapter dataAdapter; //声明 DataAdapter
public frmhangban()
{
InitializeComponent();
}
private void frmhangban_Load(object sender, EventArgs e)
{
//查询用的 SQL 语句
string sql = "select Id,FlightNO,LeaveCity,Destination,LeaveTime,SecondClass,firstClass from TicketInfo";
//初始化 DataAdapter
dataAdapter = new SqlDataAdapter(sql, DBHelper.connection);
//填充 DataSet
dataAdapter.Fill(dataSet ,"TicketInfo");
//指定 DataGridView 的数据源
dgvTicketInfo.DataSource = dataSet.Tables["TicketInfo"];
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSearch_Click(object sender, EventArgs e)
{
fillListView();
}
#region 全部查询方法
private void fillListView()
{
// string FlightNO; //航班号
//string LeaveCity;//出发城市
//string Destination;//目的地城市
try
{
if (txtSearchFrom.Text == "" || txtSearchTo.Text == "")
{
MessageBox.Show("请输入地点", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = "select id,flightNo,Leavecity,Destination,leaveTime,SecondClass ,FirstClass from ticketinfo";
if (txtSearchFrom.Text != "" && txtSearchTo.Text == "")
{
sql = sql + string.Format(" where leavecity={0}", txtSearchFrom.Text);
}
else if (txtSearchFrom.Text == "" && txtSearchTo.Text != "")
{
sql = sql + string.Format(" where destination={0}", txtSearchTo.Text);
}
else
{
sql = sql + string.Format(" where leavecity='{0}' and destination='{1}'", txtSearchFrom.Text, txtSearchTo.Text);
}
dataSet.Tables.Clear();
dataAdapter = new SqlDataAdapter(sql, DBHelper.connection);
dataAdapter.Fill(dataSet, "table");
this.dgvTicketInfo.DataSource = dataSet.Tables["table"];
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DBHelper.connection.Close();
}
}
#endregion
#region 详细查询
private void dgvTicketInfo_MouseClick(object sender, MouseEventArgs e)
{
this.txtFrom.Text = this.dgvTicketInfo.SelectedRows[0].Cells["leavecity"].Value.ToString();
this.txtTo.Text = this.dgvTicketInfo.SelectedRows[0].Cells["Destination"].Value.ToString();
this.txtLeaveDate.Text = this.dgvTicketInfo.SelectedRows[0].Cells["LeaveTime"].Value.ToString();
this.txtjingji.Text = this.dgvTicketInfo.SelectedRows[0].Cells["SecondClass"].Value.ToString();
this.txtToudeng.Text = this.dgvTicketInfo.SelectedRows[0].Cells["FirstClass"].Value.ToString();
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
try
{
if (txtHangban.Text == "" || cboSeatType.Text == "" || txtShuliang.Text=="" || txtChufa.Text == "")
{
MessageBox.Show("请输入全查询", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
int number;
int num = Convert.ToInt32(txtShuliang.Text);
string sql = string.Format("select sum(Number) from OrderInfo where seattype='{0}' and flightno='{1}'", cboSeatType.Text, txtHangban.Text);
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
int result=-1;
if (!(result !=0 ))
{
result = Convert.ToInt32(command.ExecuteScalar());
}
switch (cboSeatType.Text)
{
case "头等舱":
number = 8 - result;
if (number > num)
{
MessageBox.Show("订票成功");
}
else
{
MessageBox.Show("机票只剩余"+number +"张机票","请选择其他航班",MessageBoxButtons .OK ,MessageBoxIcon.Information );
}
break;
case "经济舱":
number = 160 - num ;
if (number > num )
{
MessageBox .Show ("订票成功");
}
else
{
MessageBox .Show ("机票只剩余"+number +"张机票","请选择其他航班",MessageBoxButtons .OK ,MessageBoxIcon.Information );
}
break ;
}
}
catch (Exception ex)
{
MessageBox .Show (ex .Message );
}
finally
{
DBHelper .connection .Close ();
}
}
}