版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Delicious_Life/article/details/84896532
前言
听说有的朋友们机房合作时只用了两天就把代码编写完成了,但我实际编码时却经常被卡住,还是技术不到家啊,重构时用到的复用性强的还可以,以前没做过的可就懵逼了。所以,我又要开始一波总结了。
这里分为两部分:根据一个时间控件显示信息在这篇。根据两个时间控件显示信息请见下篇
选定时间,显示信息
简单说明下需求:我要让datagridview控件根据时间显示数据库中对应的记录
纵使我们不知道该如何实现,但我们还是可以肯定一定要走一遍七层,从结账表中根据条件查找信息......
<1>Entity层。按表建的实体,要查询的是结账记录,所以实体中要有结账表的所有属性,并且我还加上了一个用来接收DTPicker的字段
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
//结账表实体类
public class CheckDayEntity
{
//当前余额
private double remainCash;
public double RemainCash
{
get { return remainCash; }
set { remainCash = value; }
}
//充值金额
private double rechargeCash;
public double RechargeCash
{
get { return rechargeCash; }
set { rechargeCash = value; }
}
//消费金额
private double consumeCash;
public double ConsumeCash
{
get { return consumeCash; }
set { consumeCash = value; }
}
//退卡金额
private double cancelCash;
public double CancelCash
{
get { return cancelCash; }
set { cancelCash = value; }
}
//净利润
private double allCash;
public double AllCash
{
get { return allCash; }
set { allCash = value; }
}
//结账日期
private string date;
public string Date
{
get { return date; }
set { date = value; }
}
//结账人
private string head;
public string Head
{
get { return head; }
set { head = value; }
}
//选择的结账日期
private string dtpDate;
public string DtpDate
{
get { return dtpDate; }
set { dtpDate = value; }
}
}
}
<2>IDAL层
namespace IDAL
{
public interface ICheckDay
{
List<Entity.CheckDayEntity> QueryCheckDay(Entity.CheckDayEntity check);
}
}
<3>DAL层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Entity;
using IDAL;
namespace DAL
{
public class CheckDayDAL : IDAL.ICheckDay
{
List<CheckDayEntity> ICheckDay.QueryCheckDay(CheckDayEntity check)
{
SQLHelper sqlhelper = new SQLHelper();
SqlParameter[] sqlparams = { new SqlParameter("@dtpDate", check.DtpDate)};
string sql = @"SELECT * FROM T_CheckDay WHERE date = @dtpDate";
DataTable table = sqlhelper.ExecuteQuery(sql, sqlparams, CommandType.Text);
//将Datatable类型转换为list泛型
ConvertHelper ct = new ConvertHelper();
List<Entity.CheckDayEntity> List = new List<Entity.CheckDayEntity>();
List = ct.ConvertTomodel<Entity.CheckDayEntity>(table);
return List;
}
}
}
<4>Factory层
///// <summary>
///// 日结账单查询
///// </summary>
///// <returns></returns>
public IDAL.ICheckDay QueryCheckDay()
{
string ClassName = StrDB + "." + "CheckDayDAL"; //DAL层的类名
return (IDAL.ICheckDay)Assembly.Load(StrDB).CreateInstance(ClassName); //反射+工厂的应用
}
<5>BLL层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class CheckDayBLL
{
public List<Entity.CheckDayEntity> QueryCheckDay(Entity.CheckDayEntity check)
{
Factory.AdministratorFactory fact = new Factory.AdministratorFactory();
IDAL.ICheckDay idal = fact.QueryCheckDay();
return idal.QueryCheckDay(check);
}
}
}
<6>Facade层
public class CheckDayFacade
{
public List<Entity.CheckDayEntity> QueryCheckDay(Entity.CheckDayEntity check)
{
return new BLL.CheckDayBLL().QueryCheckDay(check);
}
}
<7>UI层
private void btnQuery_Click(object sender, EventArgs e)
{
//根据日期来显示结账信息
DateTime dt;
dt = Convert.ToDateTime(dtpDate.Value.Date.ToString("yyyy-MM-dd"));
Entity.CheckDayEntity checkday = new Entity.CheckDayEntity();
checkday.DtpDate = dt.ToString("yyyy-MM-dd");
Facade.CheckDayFacade facade = new Facade.CheckDayFacade();
List<Entity.CheckDayEntity> list = new List<Entity.CheckDayEntity>();
list = facade.QueryCheckDay(checkday);
if (list.Count <= 0)
{
MessageBox.Show("改日暂无结账记录");
dataGridView1.DataSource = "";
}
else
{
dataGridView1.DataSource = list;
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.Columns[7].Visible = false; //因为实体层中加入了dtpicker控件,如果不设置成false这一列会显示在datagridview上
}
}
大功告成!