⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addrecord.cpp

📁 本系统实现了简单的点歌功能
💻 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 + -