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();
}
}
}