?? addrecord.cpp
字號:
// AddRecord.cpp: implementation of the CAddRecord class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "songserver.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()
{
m_pstrUserlist = NULL;
}
CAddRecord::~CAddRecord()
{
if( m_pstrUserlist != NULL )
{
delete[] m_pstrUserlist;
m_pstrUserlist = NULL;
}
}
//-------------------------------------------------------
//從外界得到連接到數據的指針
//-------------------------------------------------------
void CAddRecord::SetConnectionPtr(_ConnectionPtr pConnectionPtr)
{
m_pConnectionPtr = pConnectionPtr;
}
BOOL CAddRecord::PointerIsNull()
{
if( m_pConnectionPtr == NULL )
{
AfxMessageBox("連接數據庫的指針為空,請先設置指針");
return false;
}
return true;
}
//--------------------------------------------------
//下面為歌曲表操作
//--------------------------------------------------
BOOL CAddRecord::SaveRecord(CString strSongname,
CString strNameLen,
CString strSpeech,
CString strFstalphabet,
CString strSinger,
CString strSongpath,
BOOL bNewrecord,
int nID)
{
if( !PointerIsNull() )
return false;
if( strSongname.IsEmpty() || strNameLen.IsEmpty() || strSpeech.IsEmpty()
|| strFstalphabet.IsEmpty() || strSinger.IsEmpty() || strSongpath.IsEmpty())
{
AfxMessageBox("數據不能為空,請重新輸入.");
return false;
}
// 在ADO操作中建議語句中要常用try...catch()來捕獲錯誤信息,
// 因為它有時會經常出現一些想不到的錯誤。
try
{
_RecordsetPtr pSonglistPtr;
pSonglistPtr.CreateInstance(__uuidof(Recordset));
if( bNewrecord )//如果是新記錄,則按ID倒序排序,得到當前最大的ID,新記錄的ID自動加1
{
pSonglistPtr->Open( "select * from songlist order by ID DESC",
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
int nMaxid;
_variant_t vtMaxid;
if( !pSonglistPtr->BOF )//根據排序結果,得到最大ID
{
pSonglistPtr->MoveFirst();
vtMaxid = pSonglistPtr->GetCollect("ID");
nMaxid = atoi((_bstr_t)vtMaxid);
}
else//表為空,最大ID值為0
nMaxid = 0;
char szIndex[10];
itoa(++nMaxid, szIndex, 10);
pSonglistPtr->AddNew();//執行新增操作
pSonglistPtr->PutCollect("ID", (_variant_t)szIndex);//寫ID
}
else//如果不是新記錄,則執行重寫操作,根據nID找到需要重寫的記錄
{
if( nID == -1 )
{
AfxMessageBox("ID值錯誤");
return false;
}
CString strsql;
strsql.Format("select * from songlist where ID = %d", nID);
pSonglistPtr->Open( (_bstr_t)strsql,
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
}
// CString strNamelen;
// strNamelen.Format("%d", (strSpeech == "國語") ? strSongname.GetLength()/2 : strSongname.GetLength());
pSonglistPtr->PutCollect("songname", (_variant_t)strSongname);
pSonglistPtr->PutCollect("namelen", (_variant_t)strNameLen);
pSonglistPtr->PutCollect("speech", (_variant_t)strSpeech);
pSonglistPtr->PutCollect("fstalphabet", (_variant_t)strFstalphabet);
pSonglistPtr->PutCollect("singer", (_variant_t)strSinger);
pSonglistPtr->PutCollect("songpath", (_variant_t)strSongpath);
pSonglistPtr->Update();
}
catch(_com_error e)
{
AfxMessageBox((CString)"保存數據出錯: " + e.ErrorMessage());
return false;
}
AfxMessageBox( bNewrecord ? "添加歌曲成功." : "修改記錄成功.", MB_OK | MB_ICONINFORMATION);
return true;
}
//------------------------------------------------------------
//刪除記錄,nIndex為0時刪除當前記錄,不為0時刪除ID為nIndex的記錄
//------------------------------------------------------------
BOOL CAddRecord::DeleteRecord(int nIndex)
{
if( !PointerIsNull() )
return false;
try
{
CString strsql;
strsql.Format("delete from songlist where ID=%d", nIndex);
_variant_t RecordsAffected;
m_pConnectionPtr->Execute((_bstr_t)strsql, &RecordsAffected, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox((CString)"刪除數據出錯: " + e.ErrorMessage());
return false;
}
return true;
}
_RecordsetPtr CAddRecord::GetRecordsetPtr(LPCTSTR lpszsql)
{
if( !PointerIsNull() )
return NULL;
_variant_t RecordsAffected;
return m_pConnectionPtr->Execute((_bstr_t)lpszsql, &RecordsAffected, adCmdText);
}
//-------------------------------------------------------
//上面為歌曲表操作
//-------------------------------------------------------
//-------------------------------------------------------
//下面為用戶表操作
//-------------------------------------------------------
BOOL CAddRecord::AdminLogin(CString &strUsername, CString &strPassword)
{
if( !PointerIsNull() )
return false;
CString strsql;
strsql.Format("select * from userinfo where username='%s' and password='%s' and levels=0", \
strUsername, strPassword);
try
{
_RecordsetPtr pUserinfoPtr;
pUserinfoPtr.CreateInstance(__uuidof(Recordset));
pUserinfoPtr->Open( (_bstr_t)strsql,
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
if( pUserinfoPtr->BOF )
return false;
}
catch(_com_error e)
{
AfxMessageBox((CString)e.ErrorMessage() + "登錄出錯");
return false;
}
return true;
}
BOOL CAddRecord::UserLogin(CString strUsername, CString strPassword)
{
if( !PointerIsNull() )
return false;
CString strsql;
strsql.Format("select * from userinfo where username='%s' and password='%s'",
strUsername, strPassword);
try
{
_RecordsetPtr pUserinfoPtr;
pUserinfoPtr.CreateInstance(__uuidof(Recordset));
pUserinfoPtr->Open( (_bstr_t)strsql,
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
if( pUserinfoPtr->adoEOF )
return false;
}
catch(_com_error e)
{
AfxMessageBox((CString)e.ErrorMessage() + "登錄出錯");
return false;
}
return true;
}
BOOL CAddRecord::AddUser(CString strUsername,
CString strPassword,
CString strLevels)
{
if( !PointerIsNull() )
return false;
if(strUsername.IsEmpty() || strPassword.IsEmpty() || strLevels.IsEmpty())
{
AfxMessageBox("數據不能為空,請重新輸入.");
return false;
}
try
{
if(strLevels == "管理員")
strLevels.Format("%d", 0);
else
strLevels.Format("%d", 1);
_RecordsetPtr pUserinfoPtr;
pUserinfoPtr.CreateInstance(__uuidof(Recordset));
pUserinfoPtr->Open("select * from userinfo",
m_pConnectionPtr.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText );
pUserinfoPtr->AddNew();
pUserinfoPtr->PutCollect("username", (_variant_t)(_bstr_t)strUsername);
pUserinfoPtr->PutCollect("password", (_variant_t)(_bstr_t)strPassword);
pUserinfoPtr->PutCollect("levels", (_variant_t)(_bstr_t)strLevels);
pUserinfoPtr->Update();
}
catch(_com_error e)
{
AfxMessageBox((CString)"增加用戶出錯: " + e.ErrorMessage());
return false;
}
AfxMessageBox("增加成功", MB_OK | MB_ICONINFORMATION);
return true;
}
BOOL CAddRecord::DeleteUser(CString strUsername)
{
if( !PointerIsNull() )
return false;
if( strUsername.IsEmpty() )
{
AfxMessageBox("數據不能為空,請重新輸入.");
return false;
}
try
{
_variant_t RecordsAffected;
CString strsql;
strsql.Format("delete from userinfo where username = '%s'", strUsername);
m_pConnectionPtr->Execute((_bstr_t)strsql, &RecordsAffected, adCmdText);
}
catch(_com_error e)
{
AfxMessageBox((CString)"刪除用戶出錯: " + e.ErrorMessage());
return false;
}
AfxMessageBox("刪除成功", MB_OK | MB_ICONINFORMATION);
return true;
}
CString* CAddRecord::GetUserlist(int *nCount)
{
if( !PointerIsNull() )
return NULL;
try
{
int Count=0;
_variant_t RecordsAffected;
_bstr_t bstrsql = "select * from userinfo";
_RecordsetPtr pUserinfoPtr;
pUserinfoPtr = m_pConnectionPtr->Execute(bstrsql, &RecordsAffected, adCmdText);
if( !pUserinfoPtr->BOF )
pUserinfoPtr->MoveFirst();
else
{
AfxMessageBox("表為空.");
return NULL;
}
while( !pUserinfoPtr->adoEOF )
{
Count++;
pUserinfoPtr->MoveNext();
}
*nCount = Count;
if( m_pstrUserlist != NULL)
{
delete[] m_pstrUserlist;
m_pstrUserlist = NULL;
}
m_pstrUserlist = new CString[ Count ];
int i=0;
_variant_t username;
pUserinfoPtr->MoveFirst();
while(!pUserinfoPtr->adoEOF)
{
username = pUserinfoPtr->GetCollect("username");
m_pstrUserlist[ i++ ] = (LPCTSTR)(_bstr_t)username;
pUserinfoPtr->MoveNext();
}
}
catch(_com_error e)
{
AfxMessageBox( (CString)"取用戶列表出錯: " + e.ErrorMessage() );
return NULL;
}
return m_pstrUserlist;
}
CString CAddRecord::GetUserLevels(CString strUsername)
{
if( !PointerIsNull() )
return "";
if( strUsername.IsEmpty() )
{
AfxMessageBox("用戶名為空.");
return "";
}
_variant_t levels;
try
{
int Count=0;
CString strsql;
strsql.Format("select levels from userinfo where username = '%s'", strUsername);
_RecordsetPtr pUserinfoPtr;
_variant_t RecordsAffected;
pUserinfoPtr = m_pConnectionPtr->Execute((_bstr_t)strsql, &RecordsAffected, adCmdText);
if( !pUserinfoPtr->BOF )
pUserinfoPtr->MoveFirst();
else
{
AfxMessageBox("用戶名不存在.");
return "";
}
pUserinfoPtr->MoveFirst();
levels = pUserinfoPtr->GetCollect("levels");
}
catch(_com_error e)
{
AfxMessageBox( (CString)"出錯: " + e.ErrorMessage() );
return "";
}
return (LPCTSTR)(_bstr_t)levels;
}
//-------------------------------------------------------
//上面為用戶表操作
//-------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -