1. 程式人生 > >C#如何根據時間控制元件顯示資料庫中對應資訊(上)

C#如何根據時間控制元件顯示資料庫中對應資訊(上)

前言

聽說有的朋友們機房合作時只用了兩天就把程式碼編寫完成了,但我實際編碼時卻經常被卡住,還是技術不到家啊,重構時用到的複用性強的還可以,以前沒做過的可就懵逼了。所以,我又要開始一波總結了。

這裡分為兩部分:根據一個時間控制元件顯示資訊在這篇。根據兩個時間控制元件顯示資訊請見下篇

C#如何根據時間控制元件顯示資料庫中對應資訊(下)

                    

 

選定時間,顯示資訊

簡單說明下需求:我要讓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上
            }
        }

 

大功告成!