整体的首页
//创建配适器 SqlDataAdapter sa; //创建临时数据集 DataSet ds; public FrmLogin() { InitializeComponent(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //创建连接对象 SqlConnection conn = new SqlConnection(DBHelper.str); } private void FrmLogin_Load(object sender, EventArgs e) { //调用出发地的显示信息 add(); //调用到达目的地的显示信息 Goto(); } /// <summary> /// 显示出发地的信息 /// </summary> public void add() { //连接对象 SqlConnection conn = new SqlConnection(DBHelper.str); //拼接sql语句 string sql = "select * from CityInfo"; //适配器 sa = new SqlDataAdapter(sql, conn); //创建数据集字符串 ds = new DataSet(); //填充数据集 sa.Fill(ds); //向数据集和中添加文字 //Tables获取包含DataSet中的集合 DataRow row = ds.Tables[0].NewRow(); row["Id"] = -1; row["CityName"] = "请选择"; ds.Tables[0].Rows.InsertAt(row, 0); //获取或者设置文本框的数据源 cmbGo.DataSource = ds.Tables[0]; //显示属性 cmbGo.DisplayMember = "CityName"; //真实属性 cmbGo.ValueMember = "Id"; cmbGo.DataSource = ds.Tables[0];//绑定数据源 } /// <summary> /// 显示目的地的信息 /// </summary> public void Goto() { //连接对象 SqlConnection conn = new SqlConnection(DBHelper.str); //拼接sql语句 string sql = "select * from CityInfo"; //适配器 sa = new SqlDataAdapter(sql, conn); //创建数据集字符串 ds = new DataSet(); //填充数据集 sa.Fill(ds); //向数据集和中添加文字 //Tables获取包含DataSet中的集合 DataRow row = ds.Tables[0].NewRow(); row["Id"] = -1; row["CityName"] = "请选择"; ds.Tables[0].Rows.InsertAt(row, 0); //获取或者设置文本框的数据源 cmbBourn.DataSource = ds.Tables[0]; //显示属性 cmbBourn.DisplayMember = "CityName"; //真实属性 cmbBourn.ValueMember = "Id"; cmbBourn.DataSource = ds.Tables[0];//绑定数据源 } /// <summary> /// 查询按钮把数据库的信息显示到DataGridView中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRefer_Click(object sender, EventArgs e) { //创建连接对象 SqlConnection conn = new SqlConnection(DBHelper.str); try { int grade = Convert.ToInt32(cmbBourn.SelectedValue); int grade1 = Convert.ToInt32(cmbGo.SelectedValue); //拼接sql语句 string sql = @"select f.FlightNO,a.Airways,f.LeaveTime,f.LandTime,f.Price from FlightInfo as f,AirwaysInfo as a where f.AirwaysId = a.Id and f.LeaveCity ='" + grade + "' and f.Destination = '" + grade1 + "'"; ds = new DataSet(); sa = new SqlDataAdapter(sql, conn); sa.Fill(ds);//填充数据 dataGridView1.DataSource = ds.Tables[0];//绑定数据源 } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 将单元格的值显示在文本框中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { dataGridView1.AutoGenerateColumns = false; //在第二个分组框显示信息 //拿到当前选中项的航班号 string Flightnum = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value); //航空公司 string Airways = Convert.ToString(dataGridView1.SelectedRows[0].Cells[1].Value); //出发时间 string LeaveTime = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value); //到达时间 string LandTime = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value); //成人票价 string Price = Convert.ToString(dataGridView1.SelectedRows[0].Cells[4].Value); //出发地 string formCity = cmbGo.Text; //目的地 string toidCity = cmbBourn.Text; //赋值给第二个分组框text文本框各个值 txtNo.Text = Flightnum.ToString(); textBox2.Text = Airways; txtTime.Text = LeaveTime; txtG.Text = LandTime; txtfares.Text = Price.ToString(); textBox3.Text = formCity; txtDd.Text = toidCity; }
/// <summary> /// 实现航班预定功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnreserve_Click(object sender, EventArgs e) { //创建对象的连接 SqlConnection conn = new SqlConnection(DBHelper.str); //获取时间 string date = dateTimePicker1.Value.ToString(); //获取随机数 Random dom = new Random(); int num = dom.Next(10000, 10000000); try { //打开数据库 conn.Open(); //时间间隔的比较 if (DateTime.Now > dateTimePicker1.Value) { MessageBox.Show("请i选择正确的出发时间!"); } else { //拼接sql语句 string sql = "insert into OrderInfo ([OrderId],[FlightNo],[LeaveDate],[Number]) VALUES ('" + num + "','" + textBox2 + "','" + txtTime.Text + "','" + txtfares.Text + "')"; SqlCommand cmd = new SqlCommand(sql, conn); int Ex = cmd.ExecuteNonQuery(); if (Ex > 0) { MessageBox.Show("预定成功!订单编号为:" + num); } else { MessageBox.Show("添加失败!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } }
错误的提示语: