火车站点售票系统

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TicketManager
{
    public partial class TicketForm : Form
    {
        DataSet dataSet;
        SqlDataAdapter dataAdapter;
        public TicketForm()
        {
            InitializeComponent();
        }

        private void TicketForm_Load(object sender, EventArgs e)
        {
            dataSet = new DataSet("TicketManager");//填充数据
            string sql = "SELECT TrainId,TrainNo,LeaveCity,ArriveCity,LeaveTime,SeatPrice,BedPrice from Ticket";
            dataAdapter = new SqlDataAdapter(sql,DBHelper.connection);
            dataAdapter.Fill(dataSet,"Ticket");
            dgvCheck.DataSource=dataSet.Tables["Ticket"];
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if(txtLeave.Text!="" && txtArrive.Text!="")
            try
            {
                string sql=string.Format("SELECT TrainId,TrainNo,LeaveCity,ArriveCity,LeaveTime,SeatPrice,BedPrice from Ticket where LeaveCity='{0}' and ArriveCity='{1}'",txtLeave.Text,txtArrive.Text);
                dataAdapter.SelectCommand.CommandText=sql;//对SQL语句进行查询读取
                dataSet.Tables["Ticket"].Clear();
                dataAdapter.Fill(dataSet,"Ticket");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        if (txtLeave.Text == "" && txtArrive.Text == "")
        {
            MessageBox.Show("始发站和终点站不能为空!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        }

        private void dgvCheck_MouseClick(object sender, MouseEventArgs e)
        {
            int id = Convert.ToInt32(dgvCheck.SelectedRows[0].Cells["TrainId"].Value);//获得当前选中ID列的值
            string sql = string.Format("SELECT * FROM Ticket where TrainId={0}",id);

            try
            {
                SqlCommand command=new SqlCommand(sql,DBHelper.connection);
                DBHelper.connection.Open();

                SqlDataReader dataRead=command.ExecuteReader();
                while(dataRead.Read())
                {
                    txtStart.Text=dataRead["LeaveCity"].ToString();
                    txtEnd.Text=dataRead["ArriveCity"].ToString();
                    txtTime.Text=dataRead["LeaveTime"].ToString();
                    txtYprice.Text=dataRead["SeatPrice"].ToString();
                    txtRprice.Text=dataRead["BedPrice"].ToString();
                }
                dataRead.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值