写这个项目用到的Windows窗体有button标签 ,comboBox组合框,dataGridView数据显示,panel,numericUpDown数字选择,dateTimePicker日期表,label。
1:先把所有属性中的name和text改了 ,目的是方便于接下来的使用。
2:改项目中所需要的所有属性值,增删改查。
3:连接数据库,用代码完成相关信息的转换。
具体代码:
全部参考代码:
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 BookPlane
{
public partial class FIightOrderMSG : Form
{
public FIightOrderMSG()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FillDataGridView();
}
private void Form1_Load(object sender, EventArgs e)
{
dgvList.AutoGenerateColumns = false;
FindCityInfo();
}
DBHelper db = new DBHelper();
/// <summary>
/// 查询航班出发地
/// </summary>
public void FindCityInfo()
{
string sql = "select * from CityInfo";
try
{
db.conOpen();
SqlCommand comm = new SqlCommand(sql, db.conn);
SqlDataReader reader = comm.ExecuteReader();
cboStart.Items.Add("请选择");
cboStart.Items.IndexOf(0);
cboStart.SelectedIndex = 0;
cboEnd.Items.Add("请选择");
cboEnd.Items.IndexOf(0);
cboEnd.SelectedIndex = 0;
if (reader.HasRows)
{
while (reader.Read())
{
string city = reader["CityName"].ToString();
cboStart.Items.Add(city);
cboEnd.Items.Add(city);
}
reader.Close();
}
else
{
MessageBox.Show("未检测到有效信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception)
{
MessageBox.Show("发生错误!");
}
finally
{
db.conClose();
}
}
/// <summary>
/// 填充DataGridView中的数据
/// </summary>
public void FillDataGridView()
{
//连接数据库
SqlConnection conn = new SqlConnection(db.strCon);
//创建SQL语句
StringBuilder sql = new StringBuilder();
//获取comboBox被选中的下标
int AirId = cboStart.SelectedIndex;
int DestinationId = cboEnd.SelectedIndex;
//创建SQL语句
sql.AppendLine(" select * from FlightInfo,AirwaysInfo");
sql.AppendLine(" where AirwaysInfo.Id = FlightInfo.AirwaysId");
sql.AppendLine(" and FlightInfo.LeaveCity='" + AirId + "' and FlightInfo.Destination='" + DestinationId + "'");
//创建适配器,卡车对象
SqlDataAdapter da = new SqlDataAdapter(sql.ToString(), conn);
DataSet ds = new DataSet();
da.Fill(ds, "air");
dgvList.DataSource = ds.Tables["air"];
//string FlightNo = reader["FlightNo"].ToString();
//string Airways = reader["Airways"].ToString();
//string LeaveTime = reader["LeaveTime"].ToString();
//string LandTime = reader["LandTime"].ToString();
//string Price = reader["Price"].ToString();
}
private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
{
//获取DataGridView中的值,显示在各个文本框中
txtFlightNo.Text = Convert.ToString(dgvList.SelectedCells[0].Value);
txtFlightCompany.Text = dgvList.SelectedRows[0].Cells[1].Value.ToString();
txtStartTime.Text = dgvList.SelectedRows[0].Cells[2].Value.ToString();
txtEndTime.Text = dgvList.SelectedRows[0].Cells[3].Value.ToString();
txtPrice.Text = dgvList.SelectedRows[0].Cells[4].Value.ToString();
txtStart.Text = cboStart.SelectedItem.ToString();
txtEnd.Text = cboEnd.SelectedItem.ToString();
}
private void btnOrder_Click(object sender, EventArgs e)
{
//产生6位随机数
Random r = new Random();
string random = Convert.ToString(r.Next(100000, 1000000));
string flightNo = txtFlightNo.Text;
int num = Convert.ToInt32(nudOrderNum.Value);
if (txtFlightCompany.Text != string.Empty)
{
if (dtpStart.Value < DateTime.Now)
{
MessageBox.Show("请选择一个有效的时间!", "提示");
}
else
{
SqlConnection conn = new SqlConnection(db.strCon);
DateTime leaveTime = dtpStart.Value;
try
{
//连接数据库
conn.Open();
string sql = @"insert into OrderInfo values('" + random + "','" + flightNo + "','" + leaveTime + "'," + num + ")";
SqlCommand comm = new SqlCommand(sql, conn);
int isReact = comm.ExecuteNonQuery();
if (isReact > 0)
{
MessageBox.Show("预定成功!祝您旅途愉快!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("发生错误!" + ex);
}
finally
{
conn.Close();
}
}
}
else
{
MessageBox.Show("请选择一个航班!", "提示");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要关闭吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
this.Close();
}
}
}
}