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