C#重构之透过查询学生基本信息看组合查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37508531/article/details/82821160

前言

  组合查询可以看成系统的亮点和难点吧,学完设计模式之后,组合查询便可以使用模板模式进行操作,同时,之前咱们也进行过存储过程的讲解,所以,组合查询可以结合设计模式和存储过程,现在我们以查询学生基本信息为例,来看看组合查询。

详细过程

  1.使用存储过程:注意查询语句中的空格

USE [Computer]
GO
/****** Object:  StoredProcedure [dbo].[PROC_GroupCheck]    Script Date: 09/23/2018 16:04:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		郝平平
-- Create date: 2018-8-30
-- Description:	用于组合查询时和数据库进行交互
-- =============================================
ALTER PROCEDURE [dbo].[PROC_GroupCheck]
	-- Add the parameters for the stored procedure here
	@DbName nvarchar(50),
	@cmbFieldOne nvarchar(20),
	@cmbFieldTwo nvarchar(20),
	@cmbFieldThree nvarchar(20),
	@cmbOpertOne nvarchar(10),
	@cmbOpertTwo nvarchar(10),
	@cmbOpertThree nvarchar(10),
	@txtInquiryOne nvarchar(20),
	@txtInquiryTwo nvarchar(20),
	@txtInquiryThree nvarchar(20),
	@cmbLineOne nvarchar(10),
	@cmbLineTwo nvarchar(10)
AS
declare @TempSql varchar(500)   --临时存放SQL语句
--Char(32)是空格  Char(39)是单引号
BEGIN
	--一个条件的查询
	SET @TempSql='SELECT * FROM ' +@DbName+' WHERE' + char(32) + @cmbFieldOne+@cmbOpertOne+char(39)+@txtInquiryOne+char(39)
	if(@cmbLineOne !='')
	begin
	
	--两个条件的查询
	SET @TempSql=@TempSql+char(32)+@cmbLineOne+char(32)+@cmbFieldTwo+@cmbOpertTwo+char(39)+@txtInquiryTwo+char(39)
	if(@cmbLineTwo !='')
	begin
	
	--三个条件的查询
	SET @TempSql=@TempSql+char(32)+@cmbLineTwo+char(32)+@cmbFieldThree+@cmbOpertThree+char(39)+@txtInquiryThree+char(39)
    END
    
    END
    
    Execute(@TempSql)
    
END

  2.D层的代码:和之前的代码类似,主要还是存储过程的使用

public class GroupCheckDAL:IDAL.GroupCheckIDAL
    {
        public DataTable InquiryStudentInfo(Entity.GroupCheck GroupCheck)
        {
            DataTable dt = new DataTable();
            string sql = "PROC_GroupCheck";

            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@cmbFieldOne",GroupCheck.cmbFieldOne),
                new SqlParameter("@cmbFieldTwo",GroupCheck.cmbFieldTwo),
                new SqlParameter("@cmbFieldThree",GroupCheck.cmbFieldThree),

                new SqlParameter("@cmbOpertOne",GroupCheck.cmbOpertOne),
                new SqlParameter("@cmbOpertTwo",GroupCheck.cmbOpertTwo),
                new SqlParameter("@cmbOpertThree",GroupCheck.cmbOpertThree),

                new SqlParameter("@txtInquiryOne",GroupCheck.txtInquiryOne),
                new SqlParameter("@txtInquiryTwo",GroupCheck.txtInquriyTwo),
                new SqlParameter("@txtInquiryThree",GroupCheck.txtInquiryThree),

                new SqlParameter("@cmbLineOne",GroupCheck.cmbLineOne),
                new SqlParameter("@cmbLineTwo",GroupCheck.cmbLineTwo),

                new SqlParameter("@DbName",GroupCheck.getdbName)
            };

            SQLHelper SQLHelper = new SQLHelper();
            dt = SQLHelper.ExecuteQuery(sql, paras, CommandType.StoredProcedure);

            return dt;
        }
    }

  3.其他层的代码和之前的类似,只是需要更改方法名,这里就不进行介绍

  4.建一个基类,基类的代码:

  1)基类里的虚方法,不需要具体的方法体,在子类重写这些虚方法

protected virtual void ToDgv(Entity.GroupCheck GroupCheck)    //通过实体将不同的内容显示在各个窗体中  
        { }

        public virtual string ToEnglish(string cmobo)     //将汉字转化为数据库识别的形式
        {
            return "";
        }

        protected virtual string GetDBName()  //获得数据库名字
        {
            return "";
        }

  2)设置查询的条件,进行“查询条件不能为空的提示”,如果输入的条件合法即会执行here下面的代码,走七层进行查询,有数据的话输出,无数据的话进行提示。

		Entity.GroupCheck groupcheck = new Entity.GroupCheck();   //实例化一个实体
		
private void btnInquiry_Click(object sender, EventArgs e)   //单击查询的时候触发的事件
        {
            if (cmbFieldOne.Text=="" || cmbOpertOne.Text=="" || txtInquiryOne.Text=="")     //条件不能为空
            {
                MessageBox.Show("第一行查询条件不能为空", "提示");
                return;
            }
            else
            {
                if (cmbLineOne.Text !="")        //如果第一个关系不为空的话,第二行的条件不能为空
                {                                     
				   if (cmbFieldTwo.Text=="" || cmbOpertTwo.Text=="" ||txtInquiryTwo.Text=="")
                    {
                        MessageBox.Show("第二行查询条件不能为空", "提示");
                        return;
                    }
                    else
                    {
                        if (cmbLineTwo.Text !="")     //如果第二个关系不为空的话,第三行的条件不能为空
                        {
                            if (cmbFieldThree.Text=="" || cmbOpertThree.Text=="" ||txtInquiryThree.Text=="")
                            {
                                MessageBox.Show("第三行查询条件不能为空", "提示");
                                return;
                            }
                            else
                            {
                                goto here;                //如果条件都不为空的话就执行 here 下面的语句
                            }
                        }
                        else
                        {
                            goto here;
                        }
                    }
                }
                else
                {
                    goto here;
                }
            }

            here:

            groupcheck.cmbFieldOne = ToEnglish(cmbFieldOne.Text);      //把相对应的字段转化为英文,获取字段,在D层的时候会用到
            groupcheck.cmbFieldTwo = ToEnglish(cmbFieldTwo.Text);
            groupcheck.cmbFieldThree = ToEnglish(cmbFieldThree.Text);

            groupcheck.cmbOpertOne = cmbOpertOne.Text;
            groupcheck.cmbOpertTwo = cmbOpertTwo.Text;
            groupcheck.cmbOpertThree = cmbOpertThree.Text;

            groupcheck.cmbLineOne = ToEnglish(cmbLineOne.Text);
            groupcheck.cmbLineTwo = ToEnglish(cmbLineTwo.Text);

            groupcheck.txtInquiryOne = txtInquiryOne.Text.Trim();
            groupcheck.txtInquriyTwo = txtInquiryTwo.Text.Trim();
            groupcheck.txtInquiryThree = txtInquiryThree.Text.Trim();

            groupcheck.getdbName = GetDBName();
	
			Facade.GroupCheckFacade facade = new Facade.GroupCheckFacade();     //走7层
            DataTable dt = facade.InquiryStudentInfo(groupcheck);

            if (dt.Rows.Count<=0)          //如果返回的表<=0行
            {
                MessageBox.Show("没有符合条件的查询,请重新输入", "提示");
                dataGridView1.DataSource = null;
            }
            else
            {
                dataGridView1.DataSource = dt;                
            }

  5.建立具体的窗体类,单击添加→新建项→Windows Forms→继承的窗体→选择要继承的组件,这样建好的窗体就和基类的窗体是一样的了。在具体窗体里的代码:

  1)窗体登录时给控件添加相应的内容,例如:

cmbFieldOne.Items.Add("卡号");
cmbOpertOne.Items.Add("=");
cmbLineOne.Items.Add("与");

  2)重写基类里面的方法:

public override string ToEnglish(string cmobo)   //重写基类的方法
        {
            switch (cmobo)
            {
                case "卡号":
                    return "cardNo";
                case "学生姓名":
                    return "studentName";
                case "性别":
                    return "sex";
                case "部门":
                    return "department";
                case "专业":
                    return "specialty";
                case "年级":
                    return "grade";
                case "与":
                    return "and";
                case "或":
                    return "or";
                default:
                    return "";
            }
        }

        protected override string GetDBName()
        {
            return "Student";
        }

        protected override void ToDgv(Entity.GroupCheck GroupCheck)
        {
            DataTable dt = new DataTable();
            Facade.GroupCheckFacade fact = new Facade.GroupCheckFacade();

            dt = fact.InquiryStudentInfo(GroupCheck);

            dataGridView1.DataSource = dt;                   
        }

  3)如此,窗体上的DataGridView控件内容就等于了我通过组合查询到的表的内容,当我单击查询按钮的时候,设置控件的显示情况即可。

  6.到此,基本的组合查询的功能就可以实现了,当然如果想进行优化的话,还可以有下列功能。

  1)当第一个关系下拉框里有内容的话,第二行的控件才可用;第二个关系的下拉框同理

private void cmbLineOne_SelectedIndexChanged(object sender, EventArgs e) 
        {
            cmbFieldTwo.Enabled = true;
            cmbOpertTwo.Enabled = true;
            txtInquiryTwo.Enabled = true;
            cmbLineTwo.Enabled = true;
        }

  2)单击取消按钮的时候,应该清空所有控件上的内容

	Control a = new Control();
            this.Clear(a);

public void Clear(Control ctrl)
 {
        foreach (Control c in this.Controls)     //循环遍历,如果是textbox框的话,就清空,如果是combobox的话,索引为-1
        {
             if (c is TextBox)
             {
                 c.Text = "";
             }
             if (c is ComboBox)
             {
                 ComboBox cb = c as ComboBox;
                 cb.SelectedIndex = -1;
             }

             dataGridView1.DataSource = null;      //清空数据源
        }
 }

结语

  到此,学生基本信息查询就完成了,中间用到了存储过程和模板模式,同时呢,学生基本信息查询还有一个功能是修改学生信息,关于修改学生信息的实现咱们下次见啊!

猜你喜欢

转载自blog.csdn.net/m0_37508531/article/details/82821160