1. 程式人生 > >航班系統總結

航班系統總結

ber fin show cat datagrid pri click user dia

      關於航空查詢及預訂項目中出現的問題

namespace Flight
{
public partial class Flight : Form
{
public Flight()
{
InitializeComponent();
}

private void labgo_Click(object sender, EventArgs e)
{

}
public string connstr = "Data Source=.;Initial Catalog=Ticket;Persist Security Info=True;User ID=sa;Password=19981120";

//查詢按鈕
private void Flight_Load(object sender, EventArgs e)
{
this.goplace();
this.inplace();
// MessageBox.Show("沒有要查找的記錄!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

//DataRow row = ds.Tables["CityInfo"].NewRow();
//row[0] = -1;
//row[1] = "請選擇";
//ds.Tables["CityInfo"].Rows.InsertAt(row, 0);

}

private void cmbgo_SelectedIndexChanged(object sender, EventArgs e)
{

}

//查詢顯示臨時數據
private void bttcha_Click(object sender, EventArgs e)
{
int one = Convert.ToInt32(cmbgo.SelectedValue);//獲得航班id
int two = Convert.ToInt32(cmbin.SelectedValue);
SqlConnection conn = new SqlConnection(connstr);
try
{

conn.Open();
//兩表聯查
string sql = @"select f.FlightNo,a.Airways ,f.LeaveTime,f.LandTime,f.Price
from AirwaysInfo as a,FlightInfo as f
where a.Id=f.AirwaysId and f.LeaveCity = ‘" + one + "‘ and f.Destination = ‘" + two + "‘ ";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "ang");
dgvone.DataSource = ds.Tables["ang"];
}
catch (Exception ex)
{

MessageBox.Show("異常!" + ex, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

finally
{
conn.Close();
}
}

//出發地
public void goplace()
{
SqlConnection conn = new SqlConnection(connstr);
string sql = "select * from CityInfo";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "CityInfo");
//dgvone.DataSource = ds.Tables["CityInfo"];
DataRow row = ds.Tables["CityInfo"].NewRow();
row["Id"] = -1;
row["CityName"] = "請選擇";
ds.Tables["CityInfo"].Rows.InsertAt(row, 0);
cmbgo.DataSource = ds.Tables["CityInfo"];
cmbgo.DisplayMember = "CityName";
cmbgo.ValueMember = "Id";
}

//目的地
public void inplace()
{
SqlConnection conn = new SqlConnection(connstr);
string sql = "select * from CityInfo";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "CityInfo");
//dgvone.DataSource = ds.Tables["CityInfo"];
DataRow row = ds.Tables["CityInfo"].NewRow();
row["Id"] = -1;
row["CityName"] = "請選擇";
ds.Tables["CityInfo"].Rows.InsertAt(row, 0);
cmbin.DataSource = ds.Tables["CityInfo"];
cmbin.DisplayMember = "CityName";
cmbin.ValueMember = "Id";

}


//詳細顯示查詢信息
public void details()
{
string flnum = dgvone.SelectedRows[0].Cells[0].Value.ToString();//定義接受值
string company = dgvone.SelectedRows[0].Cells[1].Value.ToString();
DateTime date = Convert.ToDateTime(dgvone.SelectedRows[0].Cells[2].Value);
string intime = dgvone.SelectedRows[0].Cells[3].Value.ToString();
int flomney = Convert.ToInt32(dgvone.SelectedRows[0].Cells[4].Value);
textflnum.Text = flnum;//將接受值付給控件並顯示
textflcob.Text = company;
textflgo.Text = cmbgo.Text;
textflin.Text = cmbin.Text;
textflgotime.Text = Convert.ToString(date);
textflintime.Text = intime;
textflmoney.Text = flomney.ToString();




}


//顯示查詢信息入口
private void dgvone_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.details();
}


//添加預定航班信息
private void bttyuding_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connstr);
string date = fldtime.Value.ToString();
conn.Open();
Random rd = new Random();
int num1 = rd.Next(10000, 10000000);//隨機數
//將預定信息添加到數據庫中
string sql = "insert into OrderInfo ([OrderId],[FlightNo],[LeaveDate],[Number])values(‘" + num1 + "‘,‘" + textflnum.Text + "‘,‘" + date + "‘,‘" + flupdnum.Text + "‘)";
try
{
if (textflnum.Text == string.Empty)
{
MessageBox.Show("請選擇一個航班!");
}
else if(DateTime.Now>=fldtime.Value)
{
MessageBox.Show("請選擇一個正確的時間!");
}
else
{


SqlCommand cmd = new SqlCommand(sql, conn);
int num = cmd.ExecuteNonQuery();
if (num > 0)
{
MessageBox.Show("你的訂單編號為" + num1 + "\n 預定票數:" + flupdnum.Text + " \n你的航班編號為"+textflnum.Text+"");
}
else
{
MessageBox.Show("增加失敗!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(sql);
MessageBox.Show("異常!" + ex, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
conn.Close();
}


}


//關閉退出訂票系統
private void bttclose_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("確定退出嗎!!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
this.Close();
}

}
}
}

技術分享圖片

技術分享圖片

技術分享圖片

航班系統總結