SoFunction
Updated on 2025-03-06

C# realizes flight inquiry and booking functions

The specific code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace FrmHangBanUser
{
public partial class FrmUser : Form
{
//1.Connection stringstring connString = "Data Source = .;Initial Catalog=Ticket;User ID = sa; Pwd = sa";
//3. Create DataSet objectDataSet set = new DataSet();
public FrmUser()
{
InitializeComponent();
}
#region Form Loading Event/// <summary>
/// Form loading event!  !/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmUser_Load(object sender, EventArgs e)
{
//FlightInfo();
AirwaysInfo();
CityInfo();
}
#endregion
#region Departure/// <summary>
/// Departure place/// </summary>
public void AirwaysInfo()
{
try
{
//2. Create a Connection objectSqlConnection conn = new SqlConnection(connString);
//4. Create DataAdapter objectStringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT Id,CityName FROM CityInfo");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.Fill the dataset(set, "CityUser");
//6. Bind the data source into the ComboBox control = ["CityUser"];
 = "Id";
 = "CityName";
//7. Add row objectDataRow row = ["CityUser"].NewRow();
row["Id"] = -1;
row["CityName"] = "Please select";
["CityUser"].(row, 0);
//8. Select a line by default = 0;
}
catch (Exception ex)
{
();
}
}
#endregion
#region Non-null verification/// <summary>
/// Non-empty verification/// </summary>
public void Check()
{
if( == 0)
{
("Please enter the city you are going to leave for!!","Operation prompt",,);
}
else if ( == 0)
{
("Please enter your destination ahhhhhhh!", "Operation prompt", , );
}
}
#endregion
#region Destination/// <summary>
/// destination/// </summary>
public void CityInfo()
{
try
{
//2. Create a Connection objectSqlConnection conn = new SqlConnection(connString);
//4. Create DataAdapter objectStringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT Id,CityName FROM CityInfo");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.Fill the dataset(set, "City");
//6. Bind the data source into the ComboBox control = ["City"];
 = "Id";
 = "CityName";
//7. Add row objectDataRow row = ["City"].NewRow();
row["Id"] = -1;
row["CityName"] = "Please select";
["City"].(row, 0);
//8. Select a line by default = 0;
}
catch (Exception ex)
{
();
}
}
#endregion
#region Flight Information/// <summary>
/// Flight information/// </summary>
public void FlightInfo()
{
try
{
//2. Create a Connection objectSqlConnection conn = new SqlConnection(connString);
//4. Create DataAdapter objectStringBuilder _sb = new StringBuilder();
_sb.AppendLine(@"SELECT ,,,,
FROM FlightInfo AS F INNER JOIN AirwaysInfo AS A ON  = ");
SqlDataAdapter adapter = new SqlDataAdapter(_sb.ToString(), conn);
//5.Fill the datasetif (["Airways"]!=null)
{
["Airways"].Clear();
}
(set, "Airways");
//6.Binding data source = ["Airways"];
}
catch (Exception ex)
{
();
}
}
#endregion
#region query button function/// <summary>
/// Query button function/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnChaXun_Click(object sender, EventArgs e)
{
Check();
Filter();
}
#endregion
#region Filter by selected criteria/// <summary>
/// Filter by selected conditions/// </summary>
public void Filter()
{
try
{
SqlConnection conn=new SqlConnection(connString);
//Filtering criteriaDataSet ds = new DataSet();
int city = Convert.ToInt32();
int Destination = Convert.ToInt32();
StringBuilder sb = new StringBuilder();
if(city!=-1 && Destination!=-1)
{
(@"SELECT ,,,,
FROM FlightInfo AS F INNER JOIN AirwaysInfo AS A ON  = ");
(@"WHERE LeaveCity = {0} AND Destination = {1}", city, Destination);
}
SqlDataAdapter adapter = new SqlDataAdapter((),conn);
(ds,"User");
 = ["User"];
}
catch (Exception ex)
{
();
}
}
#endregion
#region Close button function/// <summary>
/// Close button function/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGuanbi_Click(object sender, EventArgs e)
{
DialogResult result = ("Are you sure you want to exit the program ~","Exit prompt",,);
if(result == )
{
(); //Exit the program.  .  - - ||||}
}
#endregion
#region implements flight selection function/// <summary>
/// Implement flight selection function/// </summary>
private void dvgUserInfo_MouseClick(object sender, MouseEventArgs e)
{
 = [0].Cells["FlightNO"].();
 = [0].Cells["Airways"].();
 = ;
 = ;
 = [0].Cells["LeaveTime"].();
 = [0].Cells["LandTime"].();
 = [0].Cells["Price"].();
}
#endregion
#region Flight reservation function/// <summary>
/// Flight reservation function/// </summary>
/// <returns></returns>
public bool Insert()
{
Random _dom = new Random();
int no = _dom.Next(100000, 1000000);
SqlConnection conn = null;
string No = ;
DateTime LeaveDate = this.;
string Number = ();
try
{
conn = new SqlConnection(connString);
//Construct SQL statements that insert student recordsStringBuilder _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");
//Create Command objectSqlCommand command = new SqlCommand(_sbu.ToString(), conn);
//Open the connection();
//Calling methodint result = ();
// Process the return valueif (result > 0)
{
("Congratulations! The reservation was successful! Order number is"+no);
return true;
}
else
{
("The reservation failed! Please try again!");
return false;
}
}
catch (Exception ex)
{
();
return false;
}
finally
{
//Close the connection();
}
}
#endregion
#region Booking Button/// <summary>
/// Booking button!/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnYuDing_Click(object sender, EventArgs e)
{
Insert();
}
#endregion
}
}

Summarize

The above is the C# implementation of flight inquiry and booking functions introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!