航班信息查询预订

private void btnCha_Click(object sender, EventArgs e)
        {
            //创建工具类对象
            DBHelper db = new DBHelper();

            //用变量获取所选中的值
            int fromCityNum = Convert.ToInt32(cobChu.SelectedValue);
            int toCityNum = Convert.ToInt32(cobMu.SelectedValue);

            //创建sql语句
            string sql = @"select * from AirwaysInfo as A,FlightInfo as F where A.Id = F.AirwaysId
                           and LeaveCity = '" + fromCityNum + "' and Destination = '" + toCityNum + "'";
            //连接数据库对象
            SqlConnection conn = new SqlConnection(db.Str);

            //数据适配器
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);

            //创建数据集对象
            DataSet ds = new DataSet();

            //清空上一次查询数据
            ds.Tables.Clear();

            //调用SqlDataAdapter对象的Fill()方法填充数据集
            da.Fill(ds, "ss");

            //创建数据源连接数据表
            dataGridView2.DataSource = ds.Tables["ss"];
        }

       
        private void cobChu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       private void Form1_Load(object sender, EventArgs e)
        {
            //去除多余不显示的列
            dataGridView2.AutoGenerateColumns = false;

            //创建工具类对象
            DBHelper db = new DBHelper();

            //创建sql语句
            string sql = "select * from CityInfo";

            //连接数据库对象
            SqlConnection conn = new SqlConnection(db.Str);

            //命令对象
            SqlCommand cmd = new SqlCommand(sql, conn);

            //数据适配器
            SqlDataAdapter da = new SqlDataAdapter();

            //选中命令对象
            da.SelectCommand = cmd;

            //创建数据集对象
            DataSet ds = new DataSet();

            //调用SqlDataAdapter对象的Fill()方法填充数据集
            da.Fill(ds, "s");

            //数据行
            DataRow row = ds.Tables["s"].NewRow();

            row["Id"] = -1;
            row["CityName"] = "请选择";
            ds.Tables["s"].Rows.InsertAt(row, 0);

            cobChu.DataSource = ds.Tables["s"];
            cobChu.DisplayMember = "CityName";
            cobChu.ValueMember = "Id";

            //创建数据集对象
            DataSet ds1 = new DataSet();
            //调用SqlDataAdapter对象的Fill()方法填充数据集
            da.Fill(ds, "a");
            //数据行
            DataRow row1 = ds.Tables["a"].NewRow();

            row1["Id"] = -1;
            row1["CityName"] = "请选择";
            ds.Tables["a"].Rows.InsertAt(row1, 0);

            cobMu.DataSource = ds.Tables["a"];
            cobMu.DisplayMember = "CityName";
            cobMu.ValueMember = "Id";

        }
     
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_Click(object sender, EventArgs e)
        {
            //获取选中dataGridView2每一行的值
            string name = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            string time = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();
            string money = dataGridView2.SelectedRows[0].Cells[2].Value.ToString();
            string hang = dataGridView2.SelectedRows[0].Cells[3].Value.ToString();
            string dao = dataGridView2.SelectedRows[0].Cells[4].Value.ToString();

            //数据传递
            txtHang.Text = name;
            txtFa.Text = cobChu.Text;
            txtShi.Text = time;
            txtCheng.Text = money;
            txtKong.Text = hang;
            txtMu.Text = cobMu.Text;
            txtDao.Text = dao;

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnYU_Click(object sender, EventArgs e)
        {
            //创建工具类对象
            DBHelper db = new DBHelper();

            //连接数据库对象
            SqlConnection conn = new SqlConnection(db.Str);
       
            //如果text文本框值为空就证明没有选择航班
            if ((txtHang.Text).Equals(string.Empty))
            {
                MessageBox.Show("请选择正确的航班!", "提示", MessageBoxButtons.OK);
            }
            else
            {
                //判断预定日期是否小于当前时间
                if (Convert.ToDateTime(dateTimePicker1.Text) < Convert.ToDateTime(DateTime.Now.ToString()))
                {
                    MessageBox.Show("请输入正确日期", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    //判断购买票数是否大于0
                    if (numericUpDown1.Value > 0)
                    {
                        //创建随机数对象
                        Random r = new Random();
                        //判断随机数范围
                        string a = Convert.ToString(r.Next(111111, 999999));
                       
                        //创建sql语句
                        string sql = @"insert into OrderInfo(OrderId,FlightNo,LeaveDate,Number) Values
                ('" + a + "','" + txtHang.Text + "','" + dateTimePicker1.Value.ToString() + "','" + numericUpDown1.Value.ToString() + "')";

                        conn.Open();//打开数据库
                        //命令对象
                        SqlCommand cmd = new SqlCommand(sql, conn);

                        int b = cmd.ExecuteNonQuery();//接收受影响的行数
                        if (b == 1)
                        {
                            MessageBox.Show("成功,您的机票编号为:" + a, "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
                            conn.Close();//关闭数据库
                        }
                    }
                    else
                    {
                        MessageBox.Show("乘坐人数为0!", "警告", MessageBoxButtons.OK);
                    }
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/aaaaliling/p/8528293.html