第七章航空系统

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


转载于:https://www.cnblogs.com/yvsheng123/p/8695189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值