C++ builder學習之簡單Session 實現

來源:互聯網
上載者:User

#pragma once
#include<string>
#include<MSAccess.hpp>
using namespace std;

class CMySession
{
public:
 CMySession();
 //資料操作
 TMSQuery* QueryData(string strSQL);
 void ConnectDatabase(TMSConnection* ptmsConnection);
 bool WriteData(string strSQL);
 bool DeleteData(string strSQL);
 //商務邏輯
 bool VerifyUserStatus();//檢驗使用者狀態
 bool CreateSession(string strUserName,TMSConnection* ptmsConn);//建立會話
public:
 ~CMySession(void);
private:
 string m_strUserName;
 unsigned int m_uSessionID;
 TMSConnection *m_ptmsConn;

};

 

 

#include "MySession.h"

void CMySession::ConnectDatabase(TMSConnection* ptmsConnection)
{
 m_ptmsConn = ptmsConnection;
 return;

}
//建立會話
bool CMySession::CreateSession(string strUserName,TMSConnection* ptmsConn)
{
        m_strUserName = strUserName;
 ConnectDatabase(ptmsConn);
 string strSQL;
        strSQL = "select * from T_log where user_name = '" + strUserName + "'";

        TMSQuery* ptmsQuery = QueryData(strSQL);
        if(ptmsQuery == NULL)
           return false;
 if(ptmsQuery->RecordCount == 0)
 {
                delete ptmsQuery;
                ptmsQuery = NULL;
  strSQL = "insert into T_log(user_name) values( '"+strUserName+"')";
  if(!WriteData(strSQL))
  {
   return false;
  }
                strSQL = "select log_id from T_log where user_name = '"+strUserName+"'";
                TMSQuery* ptmsQuery = QueryData(strSQL);
                if(ptmsQuery == NULL)
                     return false;
                m_uSessionID = ptmsQuery->FieldByName("log_id")->AsInteger;
                ptmsQuery->SQL->Clear();
                delete ptmsQuery;
                ptmsQuery = NULL;
                return true;

 }
 else
 {
  strSQL = "select * from T_log where user_name = '"+m_strUserName+"'";
  TMSQuery* ptmsQuery = QueryData(strSQL);
                if(ptmsQuery == NULL)
                     return false;
  unsigned int uid;
  uid = ptmsQuery->FieldByName("log_id")->AsInteger;
                ptmsQuery->SQL->Clear();
                ptmsQuery->Close();
                delete ptmsQuery;
                ptmsQuery = NULL;
                char idBuf[10];
                itoa(uid, idBuf, 10);
                strSQL = "delete  from  T_log where log_id = ";
                strSQL += idBuf;
  if(!DeleteData(strSQL))
  {
   return false;
  }
  else
  {
   strSQL = "insert into T_log(user_name) values('"+m_strUserName+"')";
   if(!WriteData(strSQL))//寫入Session
   {
    return false;
   }
                        strSQL = "select log_id from T_log where user_name = '"+m_strUserName+"'";
                        TMSQuery* ptmsQuery = QueryData(strSQL);
                        if(ptmsQuery == NULL)
                            return false;
                        m_uSessionID = ptmsQuery->FieldByName("log_id")->AsInteger;
                        ptmsQuery->SQL->Clear();
                        delete ptmsQuery;
                        ptmsQuery = NULL;
                        return true;
  }
 }

}
//檢驗使用者狀態
bool CMySession::VerifyUserStatus()
{
 string strSQL;
 strSQL = "select * from T_log where log_id = " ;
        char szBuffer[10];
        itoa(m_uSessionID,szBuffer,10);
        strSQL+=szBuffer;
 TMSQuery* ptmsQuery = QueryData(strSQL);
        if(ptmsQuery != NULL)
        {
    if(ptmsQuery->RecordCount == 0)
    {
                ptmsQuery->SQL->Clear();
                ptmsQuery->Close();
                delete ptmsQuery;
                ptmsQuery = NULL;
  return false;
    }
   else
   {
                ptmsQuery->SQL->Clear();
                ptmsQuery->Close();
                delete ptmsQuery;
                ptmsQuery = NULL;
  return true;
    }
        }
        else
        {
               return false;
        }
}
TMSQuery* CMySession::QueryData(string strSQL)
{
        if(m_ptmsConn != NULL)
        {
    TMSQuery* ptmsQuery = new TMSQuery(NULL);
           ptmsQuery->Connection = m_ptmsConn;
    ptmsQuery->SQL->Add(strSQL.c_str());
    ptmsQuery->Execute();
    return ptmsQuery;
        }
        else
        {
            return NULL;
        }

}
bool CMySession::DeleteData(string strSQL)
{
   if(m_ptmsConn != NULL)
   {
      TMSQuery* ptmsQuery = new TMSQuery(NULL);
      ptmsQuery->Connection = m_ptmsConn;
      ptmsQuery->SQL->Add(strSQL.c_str());
      ptmsQuery->Execute();
      ptmsQuery->SQL->Clear();
      ptmsQuery->Close();
      delete ptmsQuery;
      ptmsQuery = NULL;
      return true;
   }
   else
   {
       return false;
   }
}
bool CMySession::WriteData(string strSQL)
{
    if(m_ptmsConn != NULL)
    {
       TMSQuery* ptmsQuery = new TMSQuery(NULL);
       ptmsQuery->Connection = m_ptmsConn;
       ptmsQuery->SQL->Add(strSQL.c_str());
       ptmsQuery->Execute();
       ptmsQuery->SQL->Clear();
       ptmsQuery->Close();
       delete ptmsQuery;
       return true;
    }
    else
    {
      return false;
    }
}

CMySession::CMySession()
{
 m_strUserName = "";
 m_uSessionID = 0;
        m_ptmsConn = NULL;
}

CMySession::~CMySession(void)
{
 //m_ptmsConn->Close();
}

 

其主要思路是:

在用戶端首先建立一個會話,(主要是在資料庫中查詢自己使用者的記錄,如果有,則刪除,然後再寫入,再擷取ID並儲存到m_uSessionID中),然後在客戶每次對資料庫進行操作的時候進行判斷,(從資料庫中查詢客戶的記錄,將記錄中的ID 和客戶的SessionID進行對比,如果相等則會話正常,否則,會話結束)!

思路簡單,但比較好用

,用法:

在登陸的時候產生對象Session

然後Session.Create(userName,pConn);

在每次操作資料庫的時候就Session.VerifyUserStatus()就OK了,呵呵!!!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.