一、 1.首先连接数据库
2.创建连接数据库语句接受值.创建sql语句,cmd语句操作数据,sqldataapter起到卡车搬运作用,dataset创建临时数据源,然后填充
每一行都是新的,加全部索引=-1
datarow rw = xx.tables["表名".newrow]
rw["Id"] = -1;
rw["CityName"] = "全部";
ds.Tables["CityInfo"].Rows.InsertAt(rw, 0);
ds.Tables["CityInfo"].Rows.InsertAt(rw, 0);
下拉框赋值 数据源读取表。隐藏值ValueMember
代码:
string con = "server =.; dataBase =Ticket; Uid =sa; pwd =123456";
SqlConnection conn = new SqlConnection(con);
string sql = "select * from CityInfo";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "CityInfo");
DataRow rw = ds.Tables["CityInfo"].NewRow();
rw["Id"] = -1;
rw["CityName"] = "全部";
ds.Tables["CityInfo"].Rows.InsertAt(rw, 0);
DataSet ds1 = new DataSet();
comboBox2.DataSource = ds.Tables["CityInfo"];
comboBox2.DisplayMember = "CityName";
comboBox2.ValueMember = "Id";
二、 查询七步骤,出发地目的地分别用go,qv 用int来限制位数,不同表格实现连表查询 表中的共同项,最后赋值填充绑定数据源
代码: Class1 a = new Class1();
SqlConnection conn = new SqlConnection(a.str);
int go = Convert.ToInt32(comboBox1.SelectedValue);
int qu = Convert.ToInt32(comboBox2.SelectedValue);
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 + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "aa");
dataGridView1.DataSource = ds.Tables["aa"];
三、 给每个表格赋值
string no = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
表名 控件名 行 每行索引 转string
int piaojia = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[4].Value);
定义int类型给一个限定
文本框名.text=变量名; 特殊 下拉框=文本框名.text int数量=变量名.tostring();
代码: string no = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string hangban = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string chu = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string mudidi = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
int piaojia = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[4].Value);
texthang.Text = no;
textgs.Text = hangban;
textchufa.Text = comboBox1.Text;
textzhong.Text = comboBox2.Text;
textime.Text = Convert.ToString(chu);
textzhongtime.Text = mudidi;
textmoney.Text = piaojia.ToString();
四、 DateTime.Now < dateTimePicker1.Value
时间小于当前
Random rd = new Random();
int num1 = rd.Next(10000, 10000000);//随机数
代码: SqlConnection conn = new SqlConnection(a.str);
conn.Open();
Random rd = new Random();
int num1 = rd.Next(10000, 10000000);//随机数
string sql = "insert into OrderInfo ([OrderId],[FlightNo],[LeaveDate],[Number])values('" + num1 + "','" + texthang.Text + "','" + textime.Text + "','" + numericUpDown1.Text + "')";
try
{
if (texthang.Text == string.Empty)
{
MessageBox.Show("请选择一个航班!");
}
else
{
if (DateTime.Now < dateTimePicker1.Value)
{
SqlCommand cmd = new SqlCommand(sql, conn);
int num = cmd.ExecuteNonQuery();
if (num > 0)
{
MessageBox.Show("增加'" + numericUpDown1.Text + "'张成功!");
}
else
{
MessageBox.Show("增加失败!");
}
}
else
{
MessageBox.Show("时间格式不对!");
}
}
}
catch (Exception ex)
{
MessageBox.Show(sql);
MessageBox.Show("异常!" + ex, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
conn.Close();
}