关于航空查询及预订项目中出现的问题
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();
}
}
}
}