第七章 总复习


//链接字符串
string conn = "Data Source=.;Initial Catalog=Ticket;User ID =sa; password =980216";

#region 下拉框
public void xialakuang()
{
String sql = "select * from CityInfo ";
SqlConnection conn1 = new SqlConnection(conn);//连接
SqlDataAdapter da = new SqlDataAdapter(sql, conn1);//数据适配器
DataSet ds = new DataSet();// 数据集

da.Fill(ds, "CityInfo");//填充数据
//请选择实现
//下拉列表1
DataRow dr = ds.Tables["CityInfo"].NewRow();
dr[0] = -1;
dr[1] = "请选择";
ds.Tables["CityInfo"].Rows.InsertAt(dr, 0);
comboBox1.DataSource = ds.Tables["CityInfo"];
comboBox1.DisplayMember = "CityName";
comboBox1.ValueMember = "ID";
//下拉列表2
DataView dv = new DataView(ds.Tables["CityInfo"]);
comboBox2.DataSource = dv;
comboBox2.DisplayMember = "CityName";
comboBox2.ValueMember = "ID";
}
#endregion

#region 查询按钮
public void showw()
{

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, "FlightInfo");
dgvStudent.DataSource = ds.Tables["FlightInfo"];

}
public void btnSearch_click(object sender, EventArgs e)
{
if (comboBox1.Text != "请选择" && comboBox2.Text != "请选择")
{
int fromid = Convert.ToInt32(comboBox1.SelectedValue);
int a = Convert.ToInt32(comboBox2.SelectedValue);
FlightSelect(fromid, a);
}
}

private void FlightSelect(int fromid, int a)
{
throw new NotImplementedException();
}
#endregion

//引用方法
private void Form1_Load(object sender, EventArgs e)
{
showw();
xialakuang();

}

private void dgvTicketInfo_Click(object sender, EventArgs e)
{
showw();
}

#region 预定信息
private void dgvStudent_CellClick_1(object sender, DataGridViewCellEventArgs e)
{
txthbh.Text = dgvStudent.SelectedRows[0].Cells[0].Value.ToString();
txthba.Text = comboBox1.Text;
txthaa.Text = dgvStudent.SelectedRows[0].Cells[3].Value.ToString();
txtaaa.Text = dgvStudent.SelectedRows[0].Cells[4].Value.ToString();
txtccc.Text = dgvStudent.SelectedRows[0].Cells[1].Value.ToString();
txtlll.Text = comboBox2.Text;
txtooo.Text = dgvStudent.SelectedRows[0].Cells[3].Value.ToString();
}
#endregion

private void btnyu_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();//产生随机数
int num1 = r.Next(10000, 1000000);
string time = Dtp1.Value.ToString();
string shu=domainUpDown1.Text.ToString();
string str = "Data Source=.;Initial Catalog=Ticket;Integrated Security=True";
string sql = "insert into OrderInfo ([OrderId],[FlightNo],[LeaveDate],[Number])values('" + num1.ToString() + "','" + txthbh.Text + "','" + time + "','" + shu + "')";
SqlConnection conn = new SqlConnection(str);
if (txthbh.Text == string.Empty)
{
MessageBox.Show("请输入有效的航班号", "提示", MessageBoxButtons.OK);

}
else
{
try
{
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
int num = comm.ExecuteNonQuery();
if (num > 0)
{
MessageBox.Show("预定成功!" + num1, "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
}
else
{
MessageBox.Show("预定失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
}

}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}

}

原文地址:https://www.cnblogs.com/SFHa/p/8523405.html