机票查询与订购系统
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 sousouFly
{
public partial class Fly : Form
{
public string strcon = "Data Source=.;Initial Catalog=fly;User ID=sa;Password=jinyi";
SqlConnection conn;
DataSet ds;
SqlDataAdapter adapter;
public Fly()
{
InitializeComponent();
}
private void cbocfd_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void dgvfly_SelectionChanged(object sender, EventArgs e)
{
}
private void dgvfly_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Fly_Load(object sender, EventArgs e)
{//窗体加载时填充出发地到达地的两个下拉列表框
cbochufadi();
cbodaodadi();
}
#region 出发地下拉框
public void cbochufadi()
{ //新建连接
conn = new SqlConnection(strcon);
//DataSet对象
ds = new DataSet();
//查询语句
string sql = "select * from CityInfo";
adapter = new SqlDataAdapter(sql, conn);
//填充数据流
adapter.Fill(ds, "CityInfo");
cbocfd.DataSource = ds.Tables["CityInfo"];
//绑定列表值
cbocfd.ValueMember = "Id";
cbocfd.DisplayMember = "CityName";
}
#endregion
#region 到达地下拉框
public void cbodaodadi()
{
//新建连接
conn = new SqlConnection(strcon);
//DataSet对象
ds = new DataSet();
//查询语句
string sql = "select * from CityInfo";
adapter = new SqlDataAdapter(sql, conn);
//填充数据流
adapter.Fill(ds, "CityInfo");
cbomdd.DataSource = ds.Tables["CityInfo"];
//绑定列表值
cbomdd.ValueMember = "Id";
cbomdd.DisplayMember = "CityName";
}
#endregion
#region 查询填充view
private void btnselect_Click(object sender, EventArgs e)
{ //新建连接
conn = new SqlConnection(strcon);
//DataSet对象
ds = new DataSet();
//查询语句
string sql = "select f.FlightNO,a.Airways,f.LeaveTime,f.LandTime,f.Price from FlightInfo as f , AirwaysInfo as a where f.AirwaysId = a.ID and f.LeaveCity='"+cbocfd.SelectedValue+"'and f.Destination ='"+cbomdd.SelectedValue+"'";
adapter = new SqlDataAdapter(sql, conn);
//填充数据流
adapter.Fill(ds, "FlightInfo");
//绑定数据源
dgvfly.DataSource = ds.Tables["FlightInfo"];
//隐藏多余的列
dgvfly.AutoGenerateColumns = false;
}
#endregion
#region 把选中的信息添加到列表
private void dgvfly_CellClick(object sender, DataGridViewCellEventArgs e)
{
//控件 文本 列表 选中行的第一行 第一列 的值 转换
txthbh.Text = dgvfly.SelectedRows[0].Cells[0].Value.ToString();
txthkgs.Text = dgvfly.SelectedRows[0].Cells[1].Value.ToString();
//控件 文本 控件 文本
txtcfd.Text = cbocfd.Text;
txtmdd.Text = cbomdd.Text;
//控件 文本 列表 选中行的第一行 第一列 的值 转换
txtstime.Text = dgvfly.SelectedRows[0].Cells[2].Value.ToString();
txtdtime.Text = dgvfly.SelectedRows[0].Cells[3].Value.ToString();
txtRMB.Text = dgvfly.SelectedRows[0].Cells[4].Value.ToString();
}
#endregion
#region 提交订单的方法
public void ding()
{ //判定是否选中航班
if ((txthbh.Text).Equals(string.Empty))
{
MessageBox.Show("请选择正确的航班!","提示",MessageBoxButtons.OK);
}
else
{ //判定日期是否是未来的日期
if (Convert.ToDateTime(Dtp1.Text) < Convert.ToDateTime(DateTime.Now.ToString()))
{
MessageBox.Show("请输入正确日期", "提示", MessageBoxButtons.OK);
}
else
{ //判定是否有乘坐人数
if (nup1.Value > 0)
{
Random r = new Random();//创建生成随机数
int a = r.Next(10000, 1000000);
//提交订单的语句
string sql = "insert into OrderInfo(OrderId,FlightNo,LeaveDate,Number) Values('" + a + "','" + txthbh.Text + "','" + Dtp1.Value.ToString() + "','" + nup1.Value.ToString() + "')";
conn = new SqlConnection(strcon);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
//接受修改成功的返回值
int b = cmd.ExecuteNonQuery();
if (b == 1)
{
MessageBox.Show("预定成功,您的机票编号为:" + a, "提示", MessageBoxButtons.OK);
conn.Close();
}
}
else
{
MessageBox.Show("乘坐人数为0!", "警告", MessageBoxButtons.OK);
}
}
}
}
#endregion
#region 提交订单
private void btnyes_Click(object sender, EventArgs e)
{
ding();//引用提交订单的方法
}
#endregion
}
}
//总结
事件!属性!方法!三要素!
搞清需求
缕清思路