C#实现航班查询及预订功能
程序员文章站
2023-12-19 16:05:40
具体代码如下所示:
using system;
using system.collections.generic;
using system.compon...
具体代码如下所示:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.data.sqlclient; namespace frmhangbanuser { public partial class frmuser : form { //1.连接字符串 string connstring = "data source = .;initial catalog=ticket;user id = sa; pwd = sa"; //3.创建dataset对象 dataset set = new dataset(); public frmuser() { initializecomponent(); } #region 窗体加载事件 /// <summary> /// 窗体加载事件!! /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmuser_load(object sender, eventargs e) { //flightinfo(); airwaysinfo(); cityinfo(); } #endregion #region 出发地 /// <summary> /// 出发地 /// </summary> public void airwaysinfo() { try { //2.创建connection对象 sqlconnection conn = new sqlconnection(connstring); //4.创建dataadapter对象 stringbuilder _sb = new stringbuilder(); _sb.appendline(@"select id,cityname from cityinfo"); sqldataadapter adapter = new sqldataadapter(_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.添加行对象 datarow row = set.tables["cityuser"].newrow(); row["id"] = -1; row["cityname"] = "请选择"; set.tables["cityuser"].rows.insertat(row, 0); //8.默认选中一行 this.cboairways.selectedindex = 0; } catch (exception ex) { messagebox.show(ex.message); } } #endregion #region 非空验证 /// <summary> /// 非空验证 /// </summary> public void check() { if(this.cboairways.selectedindex == 0) { messagebox.show("请输入你要出发的城市啊!!","操作提示",messageboxbuttons.okcancel,messageboxicon.question); } else if (this.cbomudidi.selectedindex == 0) { messagebox.show("请输入你的目的地啊啊啊!!", "操作提示", messageboxbuttons.okcancel, messageboxicon.question); } } #endregion #region 目的地 /// <summary> /// 目的地 /// </summary> public void cityinfo() { try { //2.创建connection对象 sqlconnection conn = new sqlconnection(connstring); //4.创建dataadapter对象 stringbuilder _sb = new stringbuilder(); _sb.appendline(@"select id,cityname from cityinfo"); sqldataadapter adapter = new sqldataadapter(_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.添加行对象 datarow row = set.tables["city"].newrow(); row["id"] = -1; row["cityname"] = "请选择"; set.tables["city"].rows.insertat(row, 0); //8.默认选中一行 this.cbomudidi.selectedindex = 0; } catch (exception ex) { messagebox.show(ex.message); } } #endregion #region 航班信息 /// <summary> /// 航班信息 /// </summary> public void flightinfo() { try { //2.创建connection对象 sqlconnection conn = new sqlconnection(connstring); //4.创建dataadapter对象 stringbuilder _sb = new stringbuilder(); _sb.appendline(@"select f.flightno,a.airways,f.leavetime,f.landtime,f.price from flightinfo as f inner join airwaysinfo as a on f.airwaysid = a.id"); sqldataadapter adapter = new sqldataadapter(_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 (exception ex) { messagebox.show(ex.message); } } #endregion #region 查询按钮功能 /// <summary> /// 查询按钮功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnchaxun_click(object sender, eventargs e) { check(); filter(); } #endregion #region 按选择的条件筛选 /// <summary> /// 按选择的条件筛选 /// </summary> public void filter() { try { sqlconnection conn=new sqlconnection(connstring); //筛选条件 dataset ds = new dataset(); int city = convert.toint32(cboairways.selectedvalue); int destination = convert.toint32(cbomudidi.selectedvalue); stringbuilder sb = new stringbuilder(); if(city!=-1 && destination!=-1) { sb.appendline(@"select f.flightno,a.airways,f.landtime,f.leavetime,f.price from flightinfo as f inner join airwaysinfo as a on f.airwaysid = a.id"); sb.appendformat(@"where leavecity = {0} and destination = {1}", city, destination); } sqldataadapter adapter = new sqldataadapter(sb.tostring(),conn); adapter.fill(ds,"user"); this.dvguserinfo.datasource = ds.tables["user"]; } catch (exception ex) { messagebox.show(ex.message); } } #endregion #region 关闭按钮功能 /// <summary> /// 关闭按钮功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnguanbi_click(object sender, eventargs e) { dialogresult result = messagebox.show("你确定要退出程序嘛~","退出提示",messageboxbuttons.okcancel,messageboxicon.stop); if(result == dialogresult.ok) { application.exit(); //退出程序了。。 - - ||| } } #endregion #region 实现航班选择功能 /// <summary> /// 实现航班选择功能 /// </summary> private void dvguserinfo_mouseclick(object sender, mouseeventargs e) { 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 航班预定功能 /// <summary> /// 航班预定功能 /// </summary> /// <returns></returns> public bool insert() { random _dom = new random(); int no = _dom.next(100000, 1000000); sqlconnection conn = null; string no = this.txthno.text; datetime leavedate = this.datetimepicker1.value; string number = this.nushang.value.tostring(); try { conn = new sqlconnection(connstring); //构建插入学生记录的sql的语句 stringbuilder _sbu = new stringbuilder(); _sbu.appendline("insert into orderinfo(orderid,flightno,leavedate,number)"); _sbu.appendformat("values('{0}','{1}','{2}','{3}')", no, no, leavedate, number); _sbu.appendformat("select @@identity"); //创建command对象 sqlcommand command = new sqlcommand(_sbu.tostring(), conn); //打开连接 conn.open(); //调用方法 int result = command.executenonquery(); //对返回值进行处理 if (result > 0) { messagebox.show("恭喜你!预定成功!订单编号为"+no); return true; } else { messagebox.show("预定失败!请重试!"); return false; } } catch (exception ex) { messagebox.show(ex.message); return false; } finally { //关闭连接 conn.close(); } } #endregion #region 预定按钮 /// <summary> /// 预定按钮! /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnyuding_click(object sender, eventargs e) { insert(); } #endregion } }
总结
以上所述是小编给大家介绍的c#实现航班查询及预订功能,希望对大家有所帮助