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

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

選定時間範圍,顯示資訊

下篇的需求是選擇一個時間段之間的資料 

 

程式碼如下:

<1>Entity

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    /// <summary>
    /// 退卡表實體類
    /// </summary>
    public class CancelCardEntity
    {
        //卡號
        private int cardNo;
        public int CardNo
        {
            get { return cardNo; }
            set { cardNo = value; }
        }

        //學號
        private int studentNo;
        public int StudentNo
        {
            get { return studentNo; }
            set { studentNo = value; }
        }

        //學生姓名
        private string studentName;
        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }

        //退卡金額
        private double cancelCash;
        public double CancelCash
        {
            get { return cancelCash; }
            set { cancelCash = value; }
        }

        //退卡日期
        private string date;
        public string Date
        {
            get { return date; }
            set { date = value; }
        }

        //開始日期
        private string dtStart;
        public string DtStart
        {
            get { return dtStart; }
            set { dtStart = value; }
        }

        //結束日期
        private string dtend;
        public string Dtend
        {
            get { return dtend; }
            set { dtend = value; }
        }

        //退卡時間
        private string time;
        public string Time
        {
            get { return time; }
            set { time = value; }
        }

        //結賬
        private string isCheck;
        public string IsCheck
        {
            get { return isCheck; }
            set { isCheck = value; }
        }

        //操作人
        private string head;
        public string Head
        {
            get { return head; }
            set { head = value; }
        }


    }
}

 

<2>IDAL

   public  interface IQueryCancelRecord
    {

        /// <summary>
        ///  金額返還資訊查詢
        /// </summary>
        /// <param name="cancel"></param>
        /// <returns></returns>
        List<Entity.CancelCardEntity> QueryCancelRecord(Entity.CancelCardEntity cancel);
    }

 

<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;

namespace DAL
{
   public  class QueryCancelRecordDAL : IDAL.IQueryCancelRecord
    {
        //查詢日期範圍內是否有記錄
        public List<Entity.CancelCardEntity> QueryCancelRecord(Entity.CancelCardEntity cancel)
        {
            SQLHelper sqlhelper = new SQLHelper();
            SqlParameter[] sqlparams = { new SqlParameter("@dateTimePicker1", cancel.DtStart), new SqlParameter("@dateTimePicker2", cancel.Dtend) };
            string sql = @"SELECT * FROM T_CancelCard WHERE date between @dateTimePicker1 and @dateTimePicker2";            
            DataTable table = sqlhelper.ExecuteQuery(sql, sqlparams, CommandType.Text);

            //將Datatable型別轉換為list泛型
            ConvertHelper ct = new ConvertHelper();
            List<Entity.CancelCardEntity> List = new List<Entity.CancelCardEntity>();
            List = ct.ConvertTomodel<Entity.CancelCardEntity>(table);
            return List;

        }
    }
}

 

<4>Factory

        /// <summary>
        /// 金額返還資訊查詢
        /// </summary>
        /// <returns></returns>
        public IDAL.IQueryCancelRecord QueryCancelRecord()
        {
            string ClassName = StrDB + "." + "QueryCancelRecordDAL";  //DAL層的類名
            return (IDAL.IQueryCancelRecord)Assembly.Load(StrDB).CreateInstance(ClassName);   //反射+工廠的應用
        }

 

<5>BLL

namespace BLL
{
    public class QueryCancelRecordBLL  //金額返還資訊查詢
    {


        /// <summary>
        /// 金額返還資訊查詢
        /// </summary>
        /// <param name="cancelCard"></param>
        /// <returns></returns>
        public List<Entity.CancelCardEntity> QueryCancelRecord(Entity.CancelCardEntity cancelCard)
        {
            Factory.OperatorFactory fact = new Factory.OperatorFactory();
            IDAL.IQueryCancelRecord idal = fact.QueryCancelRecord();
            return idal.QueryCancelRecord(cancelCard);
        }
    }
}

 

<6>Facade

namespace Facade
{
    public class QueryCancelRecordFacade  //金額返還資訊查詢
    {
        /// <summary>
        /// 金額返還資訊查詢
        /// </summary>
        /// <param name="cancelCard"></param>
        /// <returns></returns>
        public List<Entity.CancelCardEntity> QueryCancelRecord(Entity.CancelCardEntity cancelCard)
        {
            return new BLL.QueryCancelRecordBLL().QueryCancelRecord(cancelCard);
        }
    }
}

 

<7>UI

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //根據日期來確定資訊
            DateTime dtStart;
            DateTime dtEnd;
            dtStart = Convert.ToDateTime(dtpStart.Value.Date.ToString("yyyy-MM-dd"));
            dtEnd = Convert.ToDateTime(dtpEnd.Value.Date.ToString("yyyy-MM-dd"));

            Entity.CancelCardEntity cancel = new Entity.CancelCardEntity();
            cancel.DtStart = dtStart.ToString("yyyy-MM-dd");
            cancel.Dtend = dtEnd.ToString("yyyy-MM-dd");

            if (DateTime.Compare(dtStart, dtEnd) >= 0)
            {
                MessageBox.Show("時間範圍不合理,請重新選擇時間範圍");
            }
            else
            {
                Facade.QueryCancelRecordFacade facade = new Facade.QueryCancelRecordFacade();
                List<Entity.CancelCardEntity> list = new List<Entity.CancelCardEntity>();
                list = facade.QueryCancelRecord(cancel);
                if (list.Count > 0)
                {
                    dataGridView1.DataSource = list;
                }
                else
                {
                    MessageBox.Show("無查詢記錄");
                    dataGridView1.DataSource = "";
                }
            }
        }

 

解決Bug

 

 

功能實現了,先彆著急高興。紅色圈中的這兩個屌是幹嘛的?再仔細一看,這不就是我們在實體層中定義的時間屬性用來接收兩個時間控制元件的資訊的嘛,這東西顯示出來沒什麼卵用呀。別急,有一種超簡單的辦法砍掉它!

耐心一數,原來是資料庫中第5個,第6個欄位顯示了不必要的部分,我們用程式碼將他隱藏起來 

 

 

 

 

 最後我們來看下效果,跳過這兩個時間屬性,它又繼續顯示後面的屬性了