前言:上篇博客讲了有关组合查询与模板方法模式的结合,那么这篇博客我将向大家继续讲解有关封装与存储过程的内容。
上一篇博客:https://blog.csdn.net/Marshallren/article/details/90347254
封装的博客:https://blog.csdn.net/Marshallren/article/details/90404216
相信不少接触了机房重构的同学都用到了存储过程这一功能,说来惭愧,我直到现在才开始用。原因是因为组合查询这块要访问数据库的语句较前面而言实在是繁琐一些,并且这样也势必会带来更多的代码冗余,因此才开始决定使用存储过程。
存储过程的创建:打开SQL Server-数据库-自己要使用的数据库-可编程性-右击存储过程-新建-存储过程
存储过程代码:
USE [GRAS]
GO
/****** Object: StoredProcedure [dbo].[PROC_Offline] Script Date: 2019/5/24 17:49:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: 任博扬
-- Create date: 2019—05—18
-- Description: 组合查询时候进行数据交互
-- =============================================
CREATE PROCEDURE [dbo].[PROC_Offline]
/****需要传递的参数****/
@Filed1 varchar(20),
@Filed2 varchar(20),
@Filed3 varchar(20),
@Compute1 varchar(10),
@Compute2 varchar(10),
@Compute3 varchar(10),
@Content1 varchar(20),
@Content2 varchar(20),
@Content3 varchar(20),
@Relate1 varchar(10),
@Relate2 varchar(10),
@DbtableName varchar(20)
AS
declare @TempSql varchar(500)
BEGIN
SET @TempSql='SELECT * FROM '+@DbtableName+' WHERE '+@Filed1+@Compute1+char(39)+@Content1+char(39)
if(@Relate1!='')
BEGIN
SET @TempSql=@TempSql+@Relate1+CHAR(32)+@Filed2+@Compute2+char(39)+@Content2+char(39)
if(@Relate2!='')
BEGIN
SET @TempSql=@TempSql+@Relate2+CHAR(32)+@Filed3+@Compute3+char(39)+@Content3+char(39)
END
END
ExECUTE(@TempSql)
END
[dbo].[PROC_Offline]是存储过程的名称,在DAL层访问时需要用到;之下的代码就是存储过程要在DAL层和数据库之间传递的参数。
DAL层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _5.IDAL;
using _7.Entity;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace _6.DAL
{
public partial class OperateMessageDAL: OperateMessageIDAL
{
//实例化数据操作类,进行数据查询,获取返回值
SQL_Helper sqlHelper = new SQL_Helper();
public DataTable DALViewInquiry(_7.Entity.OperateMessageEntity operatemessage)
{
SqlParameter[] sqlparams = {new SqlParameter("@Filed1",operatemessage.Field1),
new SqlParameter("@Filed2",operatemessage.Field2),
new SqlParameter("@Filed3",operatemessage.Field3),
new SqlParameter("@Compute1",operatemessage.Compute1),
new SqlParameter("@Compute2",operatemessage.Compute2),
new SqlParameter("@Compute3",operatemessage.Compute3),
new SqlParameter("@Content1",operatemessage.Content1),
new SqlParameter("@Content2",operatemessage.Content2),
new SqlParameter("@Content3",operatemessage.Content3),
new SqlParameter("@Relate1",operatemessage.Relate1),
new SqlParameter("@Relate2",operatemessage.Relate2),
new SqlParameter("@Dbtablename",operatemessage.Dbtablename)};
string sql = "PROC_Offline";
DataTable table = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.StoredProcedure);
return table;
}
}
}
上篇博客中,子类重写父类的方法正是将这些值返回出来从而传给实体层,再通过DAL层调用存储过程从而达到查询的目的。
封装:在子类方法中,我写了一个修改英文表头成中文表头的方法,因为两个子类窗体都要用,因此就将方法封装了起来。
方法:(由于父类“查询”按钮和子类“查询按钮”都有代码,若不在这个方法内加上空值判定,即使没有在UI层输全信息也会继续走下去从而出现错误)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Windows.Forms;
namespace _1.UI
{
public class ChangeHeader
{
//调用方法改变表头
public void StudentHeaderText(DataGridView dgvViewOnlineMessage)
{
if(dgvViewOnlineMessage.DataSource==null)
{
return;
}
else
{
dgvViewOnlineMessage.Columns[0].HeaderText = "序列号";
dgvViewOnlineMessage.Columns[1].HeaderText = "卡号";
dgvViewOnlineMessage.Columns[2].HeaderText = "结账状态";
dgvViewOnlineMessage.Columns[3].HeaderText = "消费金额";
dgvViewOnlineMessage.Columns[4].HeaderText = "消费时间";
dgvViewOnlineMessage.Columns[5].HeaderText = "上机日期";
dgvViewOnlineMessage.Columns[6].HeaderText = "下机日期";
dgvViewOnlineMessage.Columns[7].HeaderText = "上机时间";
dgvViewOnlineMessage.Columns[8].HeaderText = "下机时间";
dgvViewOnlineMessage.Columns[9].HeaderText = "学生姓名";
}
}
//调用方法改变表头
public void OperatorHeaderText(DataGridView dgvViewOnlineMessage)
{
if (dgvViewOnlineMessage.DataSource == null)
{
return;
}
else
{
dgvViewOnlineMessage.Columns[0].HeaderText = "序列号";
dgvViewOnlineMessage.Columns[1].HeaderText = "工号";
dgvViewOnlineMessage.Columns[2].HeaderText = "上机日期";
dgvViewOnlineMessage.Columns[3].HeaderText = "下机日期";
dgvViewOnlineMessage.Columns[4].HeaderText = "上机时间";
dgvViewOnlineMessage.Columns[5].HeaderText = "下机时间";
}
}
}
}
封装好处都有啥,谁说对了秃头发~
好了,组合查询的内容要告一段落了,如果本篇博客有什么不正确的地方请您指正!