C#机房重构-组合查询(模板模式)

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

存储过程

存储过程是SQL语句和控制流语句预编译的集合。也可视为批处理,是保存在服务器上的命名批处理,系统预先对它进行编译。作为一个单元进行处理。可以通过调用存储过程来执行多条对数据库的管理和操作。在一定程度上提高了效率。具体代码如下:

-- =============================================
-- Author:      <刘檬>
-- Create date: <2018/8/22>
-- Description: <组合查询>
-- =============================================
ALTER PROCEDURE [dbo].[PRO_inquirybasic]   //定义了存储过程的名称
-- Add the parameters for the stored procedure here
(
@Field1 char(10),   //执行SQL语句所需要的参数
@Operator1 char(10),
@content1 char(20),
@relation1 char(20),
@Field2 char(10),
@Operator2 char(10),
@content2 char(20),
@relation2 char(20),
@Field3 char(10),
@Operator3 char(10),
@content3 char(20),
@GetdbName char(50)
)
AS
BEGIN
declare @SQL varchar(2000)    //组合查询语句
select @SQL ='select * from '+ @GetdbName + 'where' +char(32)+ @Field1 + @Operator1 +char(39)+ @content1+char(39)
if @relation1 !=''
select @SQL =@SQL+ char(32)+ @relation1 + char(32)+ @Field2 + @Operator2 +char(39)+ @content2 +char(39)
if @relation2 !=''
select @SQL=@SQL +char(32)+ @relation2 + char(32) + @Field3 + @Operator3 +char(39)+ @content3 +char(39)

exec(@SQL)
END

模板方法

在组合查询中使用模板方法,主要用在将三个组合查询的窗体及针对窗体的共同操作抽象出来,使其作为子窗体去继承父窗体。从一定程度进行代码复用

父窗体
    protected virtual void ToDGv(Entity.Querygroup querygroup)//通过实体将不同的内容显示在各个窗体中
    {

    }
    public virtual string ToName(string combo)//将Combobox框中的文字转换成数据库里字段的形式
    {
        return "";
    }
    protected virtual string GetdbName()//获得要访问的数据表名字
    {
        return "";
    }
子窗体

这里写图片描述

public override string ToName(string combo)//重写父窗体方法,将具体的汉字转化成对应的数据表字段名
    {
        switch (combo)
        {
            case "卡号":
                return "cardno";
            case "学号":
                return "studentNo";
            case "姓名":
                return "studentName";
            case "系别":
                return "department";
            case "年级":
                return "grade";
            case "班级":
                return "class";
            case"与":
                return "and";
            case "或":
                return "or";
            default:     //什么都没有选择,返回空
                return "";
        }

    }
   protected override string GetdbName()
    {
        return "student_Info";// 获取数据表名称,可通过实体传递到存储过程中
    }

    protected override void ToDGv(Querygroup querygroup)
    {
        GroupFacade gf = new GroupFacade();
        DataTable table = gf.Selectstudent(querygroup);
        if (table.Rows.Count == 0)
        {
            MessageBox.Show("查询无结果!","提示");
        }
        else
        {
            dataGridView1.DataSource = table;
            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();
        }
    }

DAL层(使用存储过程进行操作)

SqlHelper sqlhelper = new SqlHelper();
string sql = "PRO_inquirybasic"; //获取存储过程名称
SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@Field1", querygroup.Field1),
                                            new SqlParameter("@Field2", querygroup.Field2),
                                            new SqlParameter("@Field3", querygroup.Field3),
                                            new SqlParameter("@Operator1",querygroup.Operator1),
                                            new SqlParameter("@Operator2",querygroup.Operator2),
                                            new SqlParameter("@Operator3",querygroup.Operator3),
                                            new SqlParameter("@content1",querygroup.Content1),
                                            new SqlParameter("@content2",querygroup.Content2),
                                            new SqlParameter("@content3",querygroup.Content3),
                                            new SqlParameter("@relation1",querygroup.Relation1),
                                            new SqlParameter("@relation2",querygroup.Relation2),
                                            new SqlParameter("@GetdbName",querygroup.GetdbName)};
 DataTable dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.StoredProcedure);
 return dt;

猜你喜欢

转载自blog.csdn.net/TheBestAge/article/details/82082611
今日推荐