机房重构之组合查询

    组合查询,不好弄,因为需要有模板模式。不过经过问别人看博客之后,也算是理解着弄完了。只是弄完了不行啊,得总结啊,那就总结吧。
     我理解的模板方法就是先建立一个父模板,然后让子类来继承父类,使用父类的功能。

U层

         public virtual void todgv(Entity.groupcheck groupcheck)
        { 
        }
        public virtual string ToName(string combo)      

        {
            return "";                                   //将查询内容转化为数据库中列名
        }
        public virtual string tableName()              
        {
            return "";                                  //获得数据库名称
        }
        private void Form13_Load_1(object sender, EventArgs e)   

        {
            cmbopterator1.Items.Add(">");
            cmbopterator1.Items.Add("<");    //给下拉控件添加数据
            cmbopterator1.Items.Add("=");
            cmbopterator1.Items.Add("!=");

            cmbopterator2.Items.Add(">");
            cmbopterator2.Items.Add("<");
            cmbopterator2.Items.Add("=");
            cmbopterator2.Items.Add("!=");

            cmbopterator3.Items.Add(">");
            cmbopterator3.Items.Add("<");
            cmbopterator3.Items.Add("=");
            cmbopterator3.Items.Add("!=");

            cmbrelatinons1.Items.Add("与");
            cmbrelatinons1.Items.Add("或");

            cmbrelatinons2.Items.Add("与");
            cmbrelatinons2.Items.Add("或");

        }

        private void button1_Click(object sender, EventArgs e)      
        {                                     
            if (cmbrelatinons1.Text == "")    
            {                                                        
                if (cmbfield1.Text == "" || cmbopterator1.Text == "" || txtcontent1.Text == "")
                {
                    MessageBox.Show("请填写完整信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }                                                      
            }
            else if (cmbrelatinons2.Text == "")
            {
                if (cmbfield2.Text == "" || cmbopterator2.Text == "" || txtcontent2.Text == "")
                {
                    MessageBox.Show("请填写完整信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (cmbfield3.Text == "" || cmbopterator3.Text == "" || txtcontent3.Text == "")
            {
                MessageBox.Show("请填写完整信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Entity.groupcheck groupcheck = new Entity.groupcheck();//将参数传给实体
            DataTable flag = new DataTable();
            groupcheck.cmbfield1 = ToName(cmbfield1.Text);  //调用了重写虚方法的内容
            groupcheck.cmbfield2 = ToName(cmbfield2.Text);
            groupcheck.cmbfield3 = ToName(cmbfield3.Text);
            groupcheck.cmbopterator1 = cmbopterator1.Text;
            groupcheck.cmbopterator2 = cmbopterator2.Text;
            groupcheck.cmbopterator3 = cmbopterator3.Text;
            groupcheck.cmbrelatinons1 = ToName(cmbrelatinons1.Text);
            groupcheck.cmbrelatinons2 = ToName(cmbrelatinons2.Text);
            groupcheck.txtcontent1 = txtcontent1.Text;
            groupcheck.txtcontent2 = txtcontent2.Text;
            groupcheck.txtcontent3 = txtcontent3.Text;
            groupcheck.tableName =gettable() ;
        }

        private void button2_Click(object sender, EventArgs e)    
        {
            cmbfield1.Text = "";             //清空数据
            cmbfield2.Text = "";
            cmbfield3.Text = "";
            cmbopterator1.Text = "";
            cmbopterator2.Text = "";
            cmbopterator3.Text = "";
            txtcontent1.Text = "";
            txtcontent2.Text = "";
            txtcontent3.Text = "";
            cmbrelatinons1.Text = "";
            cmbrelatinons2.Text = "";
            dataGridView1.DataSource = null;
        }
    }
}

D层

  public class geoupcheckDAL : IDAL.groupcheckIDAL
    {
       public DataTable selectgroupcheck(Entity.groupcheck groupcheck)
       {

           DataTable dt = new DataTable();
           string sql = "gagaga";  //调用存储过程
           SQLHelper sqlHelper = new SQLHelper();
           SqlParameter[] sqlParameter ={  
                                        new SqlParameter ("@cmbfield1",groupcheck.cmbfield1),
                                        new SqlParameter ("@cmbfield2",groupcheck.cmbfield2),
                                        new SqlParameter ("@cmbfield3",groupcheck.cmbfield3),
                                        new SqlParameter ("@cmbopterator1",groupcheck.cmbopterator1),
                                        new SqlParameter ("@cmbopterator2",groupcheck.cmbopterator2),
                                        new SqlParameter ("@cmbopterator3",groupcheck.cmbopterator3),
                                        new SqlParameter ("@txtcontent1",groupcheck.txtcontent1),
                                        new SqlParameter ("@txtcontent2",groupcheck.txtcontent2),
                                        new SqlParameter ("@txtcontent3",groupcheck.txtcontent3),
                                        new SqlParameter ("@cmbrelatinons1",groupcheck.cmbrelatinons1),
                                        new SqlParameter ("@cmbrelatinons2",groupcheck.cmbrelatinons2),
                                        new SqlParameter ("@tableName",groupcheck.tableName),
                                        };


            dt = sqlHelper.ExecuteQuery(sql, sqlParameter, CommandType.StoredProcedure);
            return dt;
       }
     }

    这是 U层和D层 ,其他几层都是跟以前的一样,就不写了。

    子类只需要写U层就行。

  private void Form14_Load(object sender, EventArgs e)
        {
            cmbfield1.Items.Add("卡号");       //换成自己需要的字段名字
            cmbfield1.Items.Add("学号");
            cmbfield1.Items.Add("姓名");
            cmbfield1.Items.Add("性别");
            cmbfield1.Items.Add("专业");
            cmbfield1.Items.Add("年级");
            cmbfield1.Items.Add("班级");

            cmbfield2.Items.Add("卡号");
            cmbfield2.Items.Add("学号");
            cmbfield2.Items.Add("姓名");
            cmbfield2.Items.Add("性别");
            cmbfield2.Items.Add("专业");
            cmbfield2.Items.Add("年级");
            cmbfield2.Items.Add("班级");

            cmbfield3.Items.Add("卡号");
            cmbfield3.Items.Add("学号");
            cmbfield3.Items.Add("姓名");
            cmbfield3.Items.Add("性别");
            cmbfield3.Items.Add("专业");
            cmbfield3.Items.Add("年级");
            cmbfield3.Items.Add("班级");
        }

        public override string ToName(string combo)   
        {
            switch (combo)                                
            {
                case "姓名":
                    return "studentname";
                case "班级":
                    return "class";
                case "年级":
                    return "grade";                               //重写虚方法
                case "卡号":
                    return "cardno";
                case "性别":
                    return "sex";
                case "学号":
                    return "studentno";
                case "专业":
                    return "department";
                case"或":
                    return "or";
                case "与":
                    return "and";
                default:
                    return "";
            }                                                     
        }
        public override string tableName()       
        {
            return "student";      //获得表名
        }  

        private void button1_Click(object sender, EventArgs e)
        { 
            Facade.groupcheckFacade groupcheckfacade = new Facade.groupcheckFacade();
            flag = groupcheckfacade.selectgroupcheck(groupcheck);
            if (flag.Rows.Count > 0)               
             {                                    
                dataGridView1.DataSource = flag;                      //加载信息 
                dataGridView1.Columns[0].HeaderText = "卡号";
                dataGridView1.Columns[1].HeaderText = "学号";
                dataGridView1.Columns[2].HeaderText = "姓名";
                dataGridView1.Columns[3].HeaderText = "性别";
                dataGridView1.Columns[4].HeaderText = "专业";
                dataGridView1.Columns[5].HeaderText = "年级";
                dataGridView1.Columns[6].HeaderText = "班级";
                dataGridView1.Refresh();
            }
           else
            {
                MessageBox.Show("没有此信息");
                dataGridView1.DataSource = "";
            }
        }

建存储过程

找到数据库-可编程性-存储过程-鼠标右击存储过程

    自己可以查查怎么建存储过程,我在使用存储过程出现无法找到存储过程,我猜是因为用的存储过程是别人的,可能需要自己创建一个。
这里写图片描述
    一般用的就是类似上面图中这样的,但是我在网上查了查怎么建存储过程,就把上面的改了改,就变成下面图中的样子
这里写图片描述
     然后在D层中调用就能用了。

    希望这些能对你有帮助

评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值