?? addrecord.cpp
字號:
// AddRecord.cpp: implementation of the CAddRecord class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AddRecord.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//連接數據庫的指針
_ConnectionPtr CAddRecord::m_pConnectionPtr = NULL;
CAddRecord::CAddRecord()
{
}
CAddRecord::~CAddRecord()
{
}
//-------------------------------------------------------
//從外界得到連接到數據的指針
//-------------------------------------------------------
void CAddRecord::SetConnectionPtr(_ConnectionPtr pConnectionPtr)
{
m_pConnectionPtr = pConnectionPtr;
}
BOOL CAddRecord::PointerIsNull()
{
if( m_pConnectionPtr == NULL )
{
AfxMessageBox("連接數據庫的指針為空,請先連接數據庫");
return false;
}
return true;
}
//--------------------------------------------------
//下面為點歌表操作
//--------------------------------------------------
LPCTSTR CAddRecord::GetSongPath(int nIndex)
{
if( !PointerIsNull() )
return false;
try
{
CString strsql;
strsql.Format("select songpath from songlist where ID=%d", nIndex);
_RecordsetPtr pRequestPtr;
pRequestPtr = m_pConnectionPtr->Execute((_bstr_t)strsql, NULL, adCmdText);
_variant_t vtpath;
vtpath = pRequestPtr->GetCollect("songpath");
m_songpath = (LPCTSTR)(_bstr_t)vtpath;
return m_songpath;
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
return NULL;
}
return NULL;
}
////////////////////////////////////////////////////////////////
//函數功能: 新增記錄或更改記錄
/*參數說明: bPlayed: 是否是已播放過的
為true時,更改歌曲狀態為已播放過, 需傳入用戶名和播放序號,其它可以為NULL
這false時,為新增記錄
返回值說明: 返回0: 成功 返回1: 失敗 返回2: 該首歌選過 */
int CAddRecord::SaveRecord(int nID,
LPCTSTR lpszUsername,
_variant_t vtSongname,
_variant_t vtSinger,
LPCTSTR lpszPlayseq,
LPCTSTR lpszPlayed,
BOOL bPlayed)
{
if( !PointerIsNull() )
return 1;
try
{
_RecordsetPtr pRequestPtr;
pRequestPtr.CreateInstance(__uuidof(Recordset));
char szID[10];
char szPlayseq[10];
CString strsql;
if( !bPlayed )//新增記錄
{
pRequestPtr->Open( "select * from request order by playseq DESC",
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
int nPlayseq;
if( !pRequestPtr->adoEOF )
{
pRequestPtr->MoveFirst();
_variant_t vtplayseq = pRequestPtr->GetCollect("playseq");
nPlayseq = atoi((_bstr_t)vtplayseq);
}
else
nPlayseq = 0;
//查找此ID的歌是否存在,存在則退出函數,不執行新增操作
strsql.Format("select * from request where ID=%d and username='%s'",
nID, lpszUsername);
pRequestPtr->Close();
pRequestPtr->Open( (_bstr_t)strsql,
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
if( !pRequestPtr->adoEOF )
return 2;
itoa(nID, szID, 10);
itoa(++nPlayseq, szPlayseq, 10);
pRequestPtr->AddNew();//執行新增操作
}
else//更改歌曲狀態為已播放過
{
strsql.Format("select * from request where username='%s' and playseq=%s",
lpszUsername, lpszPlayseq);
pRequestPtr->Open( (_bstr_t)strsql,
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
}
pRequestPtr->PutCollect("ID", (_variant_t)szID);
pRequestPtr->PutCollect("username", (_variant_t)lpszUsername);
pRequestPtr->PutCollect("songname", vtSongname);
pRequestPtr->PutCollect("singer", vtSinger);
pRequestPtr->PutCollect("playseq", (_variant_t)szPlayseq);
pRequestPtr->PutCollect("played", (_variant_t)lpszPlayed);
pRequestPtr->Update();
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
return 1;
}
return 0;
}
BOOL CAddRecord::DeleteAllRecord(LPCTSTR lpszUsername)
{
if( !PointerIsNull() )
return false;
try
{
CString strsql;
strsql.Format("delete from request where username='%s'", lpszUsername);
m_pConnectionPtr->Execute((_bstr_t)strsql, NULL, adCmdText);
}
catch(_com_error e)
{
return false;
}
return true;
}
BOOL CAddRecord::DeleteRecord(int nID)
{
if( !PointerIsNull() )
return false;
try
{
CString strsql;
strsql.Format("delete from request where ID=%d", nID);
m_pConnectionPtr->Execute((_bstr_t)strsql, NULL, adCmdText);
}
catch(_com_error e)
{
return false;
}
return true;
}
_RecordsetPtr CAddRecord::GetRecordsetPtr(LPCTSTR lpszsql)
{
if( !PointerIsNull() )
return NULL;
return m_pConnectionPtr->Execute((_bstr_t)lpszsql, NULL, adCmdText);
}
//-------------------------------------------------------
//上面為點歌表操作
//-------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -