簡單資料緩衝類(c#)

來源:互聯網
上載者:User

using System;

using System.Data;

using System.Data.SqlClient;

using System.Threading;

 

namespace WJ.Lib.Base

{

     /// <summary>

     /// DateBuffer 的摘要說明。

     /// </summary>

     public class DateBuffer

     {

        int mGetDateType;   //資料類型

        //object mobjBuffer;  

        string mSql;

        SqlCommand mSqlCom;

        DateTime mFlagDT; //上次更新時間

        int mBufferTime;     //更新時間間隔

        Thread m_Thread;  //逾時時開啟進程更新資料

 

        DataTable mdtBuffer;

 

        public DateBuffer()

        {

            mBufferTime = 2;

        }

 

        public DateBuffer(SqlCommand sSqlCom)

        {

            mBufferTime = 2;

            mSqlCom = sSqlCom;

            //從預存程序中擷取資料

            mGetDateType = 2;

        }

 

 

        public DateBuffer(string sSql)

        {

            mBufferTime = 2;

            Sql = sSql;

            //從查詢語句中擷取資料

            mGetDateType = 1;

        }

 

        /// <summary>

        /// 設定緩衝時間(分鐘)

        /// </summary>

        public int BufferTime

        {

            set

            {

                mBufferTime = value;

            }

        }

 

 

        /// <summary>

        /// 擷取快取資料

        /// </summary>

        public DataTable Buffer

        {

            get

            {

                CheckDate(true);

                return mdtBuffer;

            }

        }

 

        /// <summary>

        /// 設定資料查詢預存程序

        /// </summary>

        public SqlCommand SqlCom

        {

            set

            {

                if (mdtBuffer == null)

                {

                    mdtBuffer = new DataTable();

                }

                mSqlCom = value;

                //從預存程序中擷取資料

                mGetDateType = 2;

                CheckDate(false);

            }

            get

            {

                return mSqlCom;

            }

        }

 

        /// <summary>

        /// 設定資料查詢SQL

        /// </summary>

        public string Sql

        {

            set

            {

                if (mdtBuffer == null)

                {

                    mdtBuffer = new DataTable();

                }

                mSql = value;

                //從查詢語句中擷取資料

                mGetDateType = 1;

                CheckDate(false);

            }

            get

            {

                return mSql;

            }

        }

 

        /// <summary>

        /// 保持資料更新

        /// </summary>

        void CheckDate(bool CheckTime)

        {

            try

            {

                if (!CheckTime)

                {

                    //需要立即更新資料

                    UpdateDate();

                }

                else if (mFlagDT < DateTime.Now)

                {

                    //更新資料時間逾時,採用線程更新資料

                    if(m_Thread == null

                        || (m_Thread.ThreadState != System.Threading.ThreadState.Running

                        && m_Thread.ThreadState != System.Threading.ThreadState.WaitSleepJoin ))

                    {

                        m_Thread = new Thread(new ThreadStart(UpdateDate));

                        m_Thread.Start();

                    }

                }

            }

            catch

            {

            }

        }

 

        void UpdateDate()

        {

            DataTable dt = null;

            try

            {

                if (mGetDateType == 1)

                {

                   //通過查詢語句擷取表資料

                    dt = GetDateTable(mSql);

                }

                else if(mGetDateType == 2)

                {

                   //通過預存程序擷取表資料

                    dt =GetDateTable(ref mSqlCom);

                }

                mFlagDT = DateTime.Now.AddMinutes(mBufferTime);

            }

            catch

            {

            }

            finally

            {

                if (dt != null)

                {

                    mdtBuffer = dt;

                }

            }

        }

 

 

     }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.