DBCheck.cpp
资源名称:1731.rar [点击查看]
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:9k
源码类别:
游戏
开发平台:
Visual C++
- //DBCheck.cpp内容如下:
- // DBCheck.cpp: implementation of the CDBCheck class.
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DBCheck.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDBCheck::CDBCheck(CString strConn)
- {
- m_strSQL=_T("");
- m_strConnectString=strConn;
- DoOpenDatabase();
- }
- CDBCheck::~CDBCheck()
- {
- DoCloseRecordset();
- DoCloseDatabase();
- }
- //========打开数据库=========
- //input: void
- //output: TRUE/FALSE
- BOOL CDBCheck::DoOpenDatabase ()
- {
- if (m_strConnectString.IsEmpty ())
- return FALSE;
- if(m_database.IsOpen ()) m_database.Close ();
- BOOL bRet;
- try{
- bRet=m_database.OpenEx(m_strConnectString,0);//write and read
- }
- catch(CDBException *pe){
- Alert(pe->m_strError );
- pe->Delete ();
- if(m_database.IsOpen ())
- m_database.Close ();
- return FALSE;
- }
- return TRUE;
- }
- //=============关闭数据库======
- //input: void
- //output: void
- void CDBCheck::DoCloseDatabase()
- {
- if(m_database.IsOpen ())
- m_database.Close();
- }
- //=============执行SQL语句========
- //input: 数据库SQL语句
- //output: TRUE/FALSE
- BOOL CDBCheck::DoExecuteSQL (CString strSQL)
- {
- if(strSQL.IsEmpty ()) return FALSE;
- //if(!DoOpenDatabase()) return FALSE;
- try{
- m_database.ExecuteSQL (strSQL);
- }
- catch(CDBException *pe){
- pe->Delete ();
- return FALSE;
- }
- //DoCloseDatabase();
- return TRUE;
- }
- //===========打开记录集=========
- //input: 打开类型,SQL语句,标志
- //output: TRUE/FALSE
- BOOL CDBCheck::DoOpenRecordset (UINT nOpenType ,CString strSQL , DWORD dwOptions)
- {
- if(strSQL.IsEmpty ()) return FALSE;
- //if(!DoOpenDatabase()) return FALSE;
- m_recordset.m_pDatabase =&m_database;
- if(m_recordset.IsOpen ()) m_recordset.Close ();
- BOOL bRet;
- try{
- // bRet=m_recordset.Open (nOpenType,strSQL,dwOptions);
- bRet=m_recordset.Open (AFX_DB_USE_DEFAULT_TYPE,strSQL);
- }
- catch(CDBException *pe){
- pe->Delete ();
- return FALSE;
- }
- return TRUE;
- }
- //=======关闭记录集=====
- //input: void
- //output: void
- void CDBCheck::DoCloseRecordset ()
- {
- if(m_recordset.IsOpen ())
- m_recordset.Close ();
- }
- //====获取字段值(字符串)=======
- //input: 字段名称,该字段的值内存
- //output: TRUE/FALSE
- BOOL CDBCheck::GetFieldString (CString strFieldName,CString &strFieldValue)
- {
- strFieldValue="";
- try{
- m_recordset.GetFieldValue (strFieldName,strFieldValue);
- }
- catch(CDBException *e)
- {
- e->Delete();
- return FALSE;
- }
- return TRUE;
- }
- //====获取字段值(整形)=======
- //input: 字段名称,该字段的值内存
- //output: TRUE/FALSE
- BOOL CDBCheck::GetFieldString (CString strFieldName,int &nFieldValue)
- {
- nFieldValue=0;
- CString strFieldValue;
- try{
- m_recordset.GetFieldValue (strFieldName,strFieldValue);
- }
- catch(CDBException *e)
- {
- TRACE("%s",e->m_strError );
- e->Delete();
- return FALSE;
- }
- // TRACE(strFieldValue+"n");
- nFieldValue=atoi(strFieldValue) ;
- return TRUE;
- }
- //=====给当前用户信息的结构负值==
- //input: 用户信息结构指针,用户名
- //output: void
- void CDBCheck::SetCurUserInfo(LPNODECURUSERINFO lpNodeCurUserInfo,
- CString strUserName)
- {
- CString strFieldValue;
- int nFieldValue=0;
- if(GetFieldString("UserLevel",nFieldValue))
- lpNodeCurUserInfo->nUserLevel =nFieldValue;
- if(GetFieldString("UserScore",nFieldValue))
- lpNodeCurUserInfo->nUserScore =nFieldValue;
- if(GetFieldString("TotalCardCount",nFieldValue))
- lpNodeCurUserInfo->nTotalCardCount =nFieldValue;
- if(GetFieldString("VictoryCount",nFieldValue))
- lpNodeCurUserInfo->nVictoryCount =nFieldValue;
- if(GetFieldString("BreakNetCount",nFieldValue))
- lpNodeCurUserInfo->nBreakNetCount =nFieldValue;
- if(GetFieldString("EnterText",strFieldValue))
- lpNodeCurUserInfo->strEnterText =strFieldValue;
- if(GetFieldString("ExitText",strFieldValue))
- lpNodeCurUserInfo->strExitText =strFieldValue;
- if(GetFieldString("ShowRegardText",nFieldValue))
- lpNodeCurUserInfo->bShowRegardText =(BOOL)nFieldValue;
- if(GetFieldString("ShowMoveText",nFieldValue))
- lpNodeCurUserInfo->bShowMoveText =(BOOL)nFieldValue;
- if(GetFieldString("ShowEnemyText",nFieldValue))
- lpNodeCurUserInfo->bShowEnemyText =(BOOL)nFieldValue;
- if(GetFieldString("ShowSysInfo",strFieldValue))
- lpNodeCurUserInfo->bShowSysInfo =(BOOL)atoi(strFieldValue);
- if(GetFieldString("RoomBreakRate",strFieldValue))
- lpNodeCurUserInfo->fRoomBreakRate =(float)atof(strFieldValue);
- if(GetFieldString("RoomNetSpeed",strFieldValue))
- lpNodeCurUserInfo->nRoomNetSpeed=atoi(strFieldValue);
- if(GetFieldString("RoomPlayNum",strFieldValue))
- lpNodeCurUserInfo->nRoomPlayNum =atoi(strFieldValue);
- if(GetFieldString("RoomTableNum",strFieldValue))
- lpNodeCurUserInfo->nRoomTableNum =atoi(strFieldValue);
- if(GetFieldString("RoomName",strFieldValue))
- lpNodeCurUserInfo->strRoomName =strFieldValue;
- if(GetFieldString("BoolTabBreakRate",strFieldValue))
- lpNodeCurUserInfo->bTableBreakRate =(BOOL)atoi(strFieldValue);
- if(GetFieldString("BoolTabDislike",strFieldValue))
- lpNodeCurUserInfo->bTableDislike=(BOOL)atoi(strFieldValue);
- if(GetFieldString("BoolTabNetRate",strFieldValue))
- lpNodeCurUserInfo->bTableNetRate =(BOOL)atoi(strFieldValue);
- if(GetFieldString("TableBreakRate",strFieldValue))
- lpNodeCurUserInfo->fTableBreakRate =(float)atof(strFieldValue);
- if(GetFieldString("TableNetRate",strFieldValue))
- lpNodeCurUserInfo->nTableNetRate =atoi(strFieldValue);
- if(GetFieldString("AllFriend",strFieldValue))
- lpNodeCurUserInfo->strAllFriend =strFieldValue;
- if(GetFieldString("AllEnemy",strFieldValue))
- lpNodeCurUserInfo->strAllEnemy=strFieldValue;
- if(GetFieldString("EqualCount",strFieldValue))
- lpNodeCurUserInfo->nEqualCount=atoi(strFieldValue);
- if(GetFieldString("LossCount",strFieldValue))
- lpNodeCurUserInfo->nLossCount=atoi(strFieldValue);
- if(GetFieldString("AgreeLook",strFieldValue))
- lpNodeCurUserInfo->bAgreeLook=atoi(strFieldValue);
- return;
- }
- //======校验用户是否合法=======
- //input: 用户ID,PASSWORD,用户信息内存指针
- //output: TRUE/FALSE
- BOOL CDBCheck::IsUserValid(CString strUserId,CString strPassWord,LPNODECURUSERINFO lpNodeCurUserInfo)
- {
- CString strSQL;
- BOOL bRet;
- //先验证该用户是否正确
- strSQL.Format("select * from userinfo Where userid='%s' and password='%s'",strUserId,strPassWord);
- bRet=DoOpenRecordset(CRecordset::dynaset,strSQL,CRecordset::readOnly);
- if(!bRet) { lpNodeCurUserInfo=NULL;return FALSE;}
- CString strUserName="";
- if(m_recordset.IsEOF())
- return FALSE;
- if(!m_recordset.IsEOF())
- {
- m_recordset.MoveFirst();
- GetFieldString("UserName",strUserName);
- }
- strUserName.TrimRight();
- strUserName.TrimLeft();
- //如果该用户存在将该用户信息从Userinfo表中取得用户信息
- strSQL.Format("select * from userinfo Where userId='%s'",strUserId);
- bRet=DoOpenRecordset(CRecordset::dynaset,strSQL,CRecordset::readOnly);
- if(!bRet) { lpNodeCurUserInfo=NULL;return FALSE;}
- int i=0;
- while(!m_recordset.IsEOF())
- {
- i++;
- m_recordset.MoveNext ();
- }
- if(i>0)
- {
- m_recordset.MoveFirst ();
- SetCurUserInfo(lpNodeCurUserInfo,strUserName);
- return TRUE;
- }
- return FALSE;
- }
- //=============更新用户的信息到数据库中
- //input: 用户名称,当前用户信息指针
- //outptu: TRUE/FALSE
- BOOL CDBCheck::DoUpdateRecordset(CString strUserId,LPNODECURUSERINFO lpNCUI)
- {
- if (lpNCUI==NULL) return FALSE;
- CString strSQL;
- CString strFormat;
- strFormat="update userinfo set ";
- strFormat+="userlevel=%d,userscore=%d,";
- strFormat+="totalcardcount=%d,victorycount=%d,breaknetcount=%d,";
- strFormat+="entertext='%s',exittext='%s',showregardtext=%1d,showmovetext=%1d,";
- strFormat+="showenemytext=%1d,";
- strFormat+="showsysinfo=%1d,booltabbreakrate=%1d,booltabdislike=%1d,";
- strFormat+="booltabnetrate=%1d,roombreakrate=%f,tablebreakrate=%f,";
- strFormat+="roomnetspeed=%d,roomplaynum=%d,roomtablenum=%d,";
- strFormat+="tablenetrate=%d,allenemy='%s',allfriend='%s',roomname='%s',";
- strFormat+="equalcount=%d,losscount=%d,agreelook=%d ";
- strFormat+="where userid='%s'";
- strSQL.Format (strFormat,5,//lpNCUI->nUserLevel ,
- lpNCUI->nUserScore ,lpNCUI->nTotalCardCount ,
- lpNCUI->nVictoryCount ,lpNCUI->nBreakNetCount ,
- lpNCUI->strEnterText ,lpNCUI->strExitText ,
- lpNCUI->bShowRegardText ,lpNCUI->bShowMoveText ,
- lpNCUI->bShowEnemyText ,lpNCUI->bShowSysInfo,
- lpNCUI->bTableBreakRate,lpNCUI->bTableDislike,lpNCUI->bTableNetRate,
- lpNCUI->fRoomBreakRate,lpNCUI->fTableBreakRate,
- lpNCUI->nRoomNetSpeed,lpNCUI->nRoomPlayNum,lpNCUI->nRoomTableNum,
- lpNCUI->nTableNetRate,lpNCUI->strAllEnemy,lpNCUI->strAllFriend,
- lpNCUI->strRoomName,lpNCUI->nEqualCount,lpNCUI->nLossCount,
- lpNCUI->bAgreeLook,
- strUserId);
- BOOL bRet;
- bRet=DoExecuteSQL(strSQL);
- if(!bRet) { lpNCUI=NULL;return FALSE;}
- return TRUE;
- }