標籤:pos init exec val flight wro cmd 字串 load
public string con = "Data Source=.;Initial Catalog=ticket;Integrated Security=True";//串連資料庫語句
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(con);//串連資料庫
string sql = "select * from CityInfo";//sql語句
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();//卡車對象
da.SelectCommand = cmd;
DataSet ds = new DataSet();//建立兩個臨時資料表
da.Fill(ds, "CityInfo");
da.Fill(ds, "CityInfo2");
//向資料集第一行添加資料"請選擇"
DataRow row = ds.Tables["CityInfo"].NewRow();
row["Id"] = -1;
row["CityName"] = "請選擇";
ds.Tables["CityInfo"].Rows.InsertAt(row, 0);
DataRow row1 = ds.Tables["CityInfo2"].NewRow();
row1["Id"] = -1;
row1["CityName"] = "請選擇";
ds.Tables["CityInfo2"].Rows.InsertAt(row1, 0);
//向下拉框填充資料
this.comboBox1.DataSource = ds.Tables["CityInfo"];
this.comboBox1.ValueMember = "Id";
this.comboBox1.DisplayMember = "CityName";
this.comboBox2.DataSource = ds.Tables["CityInfo2"];
this.comboBox2.ValueMember = "Id";
this.comboBox2.DisplayMember = "CityName";
}
private void but1_Click(object sender, EventArgs e)
{
int go = Convert.ToInt32(comboBox1.SelectedValue);//接受下拉框的值
int qu = Convert.ToInt32(comboBox2.SelectedValue);
string str = "Data Source=.;Initial Catalog=Ticket;Integrated Security=True";//串連資料庫語句
string sql = " select f.FlightNo,a.Airways ,f.LeaveTime,f.LandTime,f.Price from AirwaysInfo as a,FlightInfo as f where a.Id=f.AirwaysId and LeaveCity=‘" + go + "‘ and Destination=‘" + qu + "‘";//連表查詢語句
SqlConnection conn = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "aa");//儲存在臨時資料表
dgv1.DataSource = ds.Tables["aa"];
}
private void dgv1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//擷取資料庫中的值,賦給變數
string no = dgv1.SelectedRows[0].Cells[0].Value.ToString();
string hangban = dgv1.SelectedRows[0].Cells[1].Value.ToString();
string chu = Convert.ToString(dgv1.SelectedRows[0].Cells[2].Value);
string mudidi = dgv1.SelectedRows[0].Cells[3].Value.ToString();
int piaojia = Convert.ToInt32(dgv1.SelectedRows[0].Cells[4].Value);
txt1.Text = no;//航班號
txt5.Text = hangban;//航班公司
txt2.Text = comboBox1.Text;//出發地
txt6.Text = comboBox2.Text;//目的地
txt3.Text = Convert.ToString(chu);//出發時間
txt7.Text = mudidi;//到達時間
txt4.Text = piaojia.ToString();//成人票價
}
private void but2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(con);
string date = dtp1.Value.ToString();//時間控制項轉字串
conn.Open();
Random rd = new Random();
int num1 = rd.Next(10000, 10000000);//隨機數
string sql [email protected]"insert into OrderInfo ([OrderId],[FlightNo],
[LeaveDate],[Number])values(‘" + num1 + "‘,‘" + txt1.Text + "‘,‘" + date + "‘,‘" + nup1.Text + "‘)";
try
{
if (txt1.Text==string.Empty)//判斷是否有航班
{
MessageBox.Show("請選擇一個航班!");
}
else
{
if (DateTime.Now < dtp1.Value)//判斷時間
{
SqlCommand cmd = new SqlCommand(sql, conn);
int num = cmd.ExecuteNonQuery();
if (num > 0)
{
MessageBox.Show("增加‘" + nup1.Text + "‘張成功!");
}
else
{
MessageBox.Show("增加失敗!");
}
}
else {
MessageBox.Show("時間格式不對!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("異常!"+ex, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
conn.Close();
}
}
private void but3_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("確定退出嗎!!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
this.Close();
}
}
做項目啊,還是應該熟悉所有的知識!書到用時方恨少!
好好學習,天天向上!
C#視窗航空總結