C#实现航班查询及预订功能
具体代码如下所示:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceFrmHangBanUser
{
publicpartialclassFrmUser:Form
{
//1.连接字符串
stringconnString="DataSource=.;InitialCatalog=Ticket;UserID=sa;Pwd=sa";
//3.创建DataSet对象
DataSetset=newDataSet();
publicFrmUser()
{
InitializeComponent();
}
#region窗体加载事件
///
///窗体加载事件!!
///
///
///
privatevoidFrmUser_Load(objectsender,EventArgse)
{
//FlightInfo();
AirwaysInfo();
CityInfo();
}
#endregion
#region出发地
///
///出发地
///
publicvoidAirwaysInfo()
{
try
{
//2.创建Connection对象
SqlConnectionconn=newSqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder_sb=newStringBuilder();
_sb.AppendLine(@"SELECTId,CityNameFROMCityInfo");
SqlDataAdapteradapter=newSqlDataAdapter(_sb.ToString(),conn);
//5.填充数据集
adapter.Fill(set,"CityUser");
//6.绑定数据源到ComboBox控件中
this.cboAirways.DataSource=set.Tables["CityUser"];
this.cboAirways.ValueMember="Id";
this.cboAirways.DisplayMember="CityName";
//7.添加行对象
DataRowrow=set.Tables["CityUser"].NewRow();
row["Id"]=-1;
row["CityName"]="请选择";
set.Tables["CityUser"].Rows.InsertAt(row,0);
//8.默认选中一行
this.cboAirways.SelectedIndex=0;
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region非空验证
///
///非空验证
///
publicvoidCheck()
{
if(this.cboAirways.SelectedIndex==0)
{
MessageBox.Show("请输入你要出发的城市啊!!","操作提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
}
elseif(this.cboMudidi.SelectedIndex==0)
{
MessageBox.Show("请输入你的目的地啊啊啊!!","操作提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
}
}
#endregion
#region目的地
///
///目的地
///
publicvoidCityInfo()
{
try
{
//2.创建Connection对象
SqlConnectionconn=newSqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder_sb=newStringBuilder();
_sb.AppendLine(@"SELECTId,CityNameFROMCityInfo");
SqlDataAdapteradapter=newSqlDataAdapter(_sb.ToString(),conn);
//5.填充数据集
adapter.Fill(set,"City");
//6.绑定数据源到ComboBox控件中
this.cboMudidi.DataSource=set.Tables["City"];
this.cboMudidi.ValueMember="Id";
this.cboMudidi.DisplayMember="CityName";
//7.添加行对象
DataRowrow=set.Tables["City"].NewRow();
row["Id"]=-1;
row["CityName"]="请选择";
set.Tables["City"].Rows.InsertAt(row,0);
//8.默认选中一行
this.cboMudidi.SelectedIndex=0;
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region航班信息
///
///航班信息
///
publicvoidFlightInfo()
{
try
{
//2.创建Connection对象
SqlConnectionconn=newSqlConnection(connString);
//4.创建DataAdapter对象
StringBuilder_sb=newStringBuilder();
_sb.AppendLine(@"SELECTF.FlightNO,A.Airways,F.LeaveTime,F.LandTime,F.Price
FROMFlightInfoASFINNERJOINAirwaysInfoASAONF.AirwaysId=A.Id");
SqlDataAdapteradapter=newSqlDataAdapter(_sb.ToString(),conn);
//5.填充数据集
if(set.Tables["Airways"]!=null)
{
set.Tables["Airways"].Clear();
}
adapter.Fill(set,"Airways");
//6.绑定数据源
this.dvgUserInfo.DataSource=set.Tables["Airways"];
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region查询按钮功能
///
///查询按钮功能
///
///
///
privatevoidbtnChaXun_Click(objectsender,EventArgse)
{
Check();
Filter();
}
#endregion
#region按选择的条件筛选
///
///按选择的条件筛选
///
publicvoidFilter()
{
try
{
SqlConnectionconn=newSqlConnection(connString);
//筛选条件
DataSetds=newDataSet();
intcity=Convert.ToInt32(cboAirways.SelectedValue);
intDestination=Convert.ToInt32(cboMudidi.SelectedValue);
StringBuildersb=newStringBuilder();
if(city!=-1&&Destination!=-1)
{
sb.AppendLine(@"SELECTF.FlightNO,A.Airways,F.LandTime,F.LeaveTime,F.Price
FROMFlightInfoASFINNERJOINAirwaysInfoASAONF.AirwaysId=A.Id");
sb.AppendFormat(@"WHERELeaveCity={0}ANDDestination={1}",city,Destination);
}
SqlDataAdapteradapter=newSqlDataAdapter(sb.ToString(),conn);
adapter.Fill(ds,"User");
this.dvgUserInfo.DataSource=ds.Tables["User"];
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region关闭按钮功能
///
///关闭按钮功能
///
///
///
privatevoidbtnGuanbi_Click(objectsender,EventArgse)
{
DialogResultresult=MessageBox.Show("你确定要退出程序嘛~","退出提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Stop);
if(result==DialogResult.OK)
{
Application.Exit();//退出程序了。。--|||
}
}
#endregion
#region实现航班选择功能
///
///实现航班选择功能
///
privatevoiddvgUserInfo_MouseClick(objectsender,MouseEventArgse)
{
txtHNo.Text=dvgUserInfo.SelectedRows[0].Cells["FlightNO"].Value.ToString();
txtGongSi.Text=dvgUserInfo.SelectedRows[0].Cells["Airways"].Value.ToString();
this.txtChuFa.Text=this.cboAirways.Text;
this.txtMuDi.Text=this.cboMudidi.Text;
txtShijian.Text=dvgUserInfo.SelectedRows[0].Cells["LeaveTime"].Value.ToString();
txtDaoDa.Text=dvgUserInfo.SelectedRows[0].Cells["LandTime"].Value.ToString();
txtPiaoJia.Text=dvgUserInfo.SelectedRows[0].Cells["Price"].Value.ToString();
}
#endregion
#region航班预定功能
///
///航班预定功能
///
///
publicboolInsert()
{
Random_dom=newRandom();
intno=_dom.Next(100000,1000000);
SqlConnectionconn=null;
stringNo=this.txtHNo.Text;
DateTimeLeaveDate=this.dateTimePicker1.Value;
stringNumber=this.nuShang.Value.ToString();
try
{
conn=newSqlConnection(connString);
//构建插入学生记录的SQL的语句
StringBuilder_sbu=newStringBuilder();
_sbu.AppendLine("INSERTINTOOrderInfo(OrderId,FlightNo,LeaveDate,Number)");
_sbu.AppendFormat("VALUES('{0}','{1}','{2}','{3}')",no,No,LeaveDate,Number);
_sbu.AppendFormat("SELECT@@IDENTITY");
//创建Command对象
SqlCommandcommand=newSqlCommand(_sbu.ToString(),conn);
//打开连接
conn.Open();
//调用方法
intresult=command.ExecuteNonQuery();
//对返回值进行处理
if(result>0)
{
MessageBox.Show("恭喜你!预定成功!订单编号为"+no);
returntrue;
}
else
{
MessageBox.Show("预定失败!请重试!");
returnfalse;
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
returnfalse;
}
finally
{
//关闭连接
conn.Close();
}
}
#endregion
#region预定按钮
///
///预定按钮!
///
///
///
privatevoidbtnYuDing_Click(objectsender,EventArgse)
{
Insert();
}
#endregion
}
}
总结
以上所述是小编给大家介绍的C#实现航班查询及预订功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!