一个完整的学生管理系统很好

源代码在线查看: ado.cpp

软件大小: 614 K
上传用户: zhangyuntong
关键词: 管理系统
下载地址: 免注册下载 普通下载 VIP

相关代码

				// tr.cpp: implementation of the tr class.
				//
				//////////////////////////////////////////////////////////////////////
				
				#include "stdafx.h"
				
				#ifdef _DEBUG
				#undef THIS_FILE
				static char THIS_FILE[]=__FILE__;
				#define new DEBUG_NEW
				#endif
				
				//////////////////////////////////////////////////////////////////////
				// Construction/Destruction
				//////////////////////////////////////////////////////////////////////
				
				
				
				#include "ado.h"
				
				#define ChunkSize 100
				
				///////////////////////////////////////////////////////
				//
				// CADODatabase Class
				//
				
				DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
				{
					DWORD numRows = 0;
					
					numRows = m_pRs->GetRecordCount();
				
					if(numRows == -1)
					{
						if(m_pRs->EndOfFile != VARIANT_TRUE)
							m_pRs->MoveFirst();
				
						while(m_pRs->EndOfFile != VARIANT_TRUE)
						{
							numRows++;
							m_pRs->MoveNext();
						}
						if(numRows > 0)
							m_pRs->MoveFirst();
					}
					return numRows;
				}
				
				BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
				{
					HRESULT hr = S_OK;
				
					if(IsOpen())
						Close();
				
					if(strcmp(lpstrConnection, _T("")) != 0)
						m_strConnection = lpstrConnection;
				
					ASSERT(!m_strConnection.IsEmpty());
				
					try
					{
						if(m_nConnectionTimeout != 0)
							m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
						hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
						return hr == S_OK;
					}
					catch(_com_error &e)
					{
						dump_com_error(e);
						return FALSE;
					}
					
				}
				
				void CADODatabase::dump_com_error(_com_error &e)
				{
					CString ErrorStr;
					
					_bstr_t bstrSource(e.Source());
					_bstr_t bstrDescription(e.Description());
					ErrorStr.Format( "CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
						e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription);
					m_strErrorDescription = (LPCSTR)bstrDescription ;
					m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);
					m_dwLastError = e.Error(); 
					#ifdef _DEBUG
						AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR );
					#endif
				}
				
				BOOL CADODatabase::IsOpen()
				{
					if(m_pConnection )
						return m_pConnection->GetState() != adStateClosed;
					return FALSE;
				}
				
				void CADODatabase::Close()
				{
					if(IsOpen())
						m_pConnection->Close();
				}
				
				
				///////////////////////////////////////////////////////
				//
				// CADORecordset Class
				//
				
				CADORecordset::CADORecordset()
				{
					m_pRecordset = NULL;
					m_pCmd = NULL;
					m_strQuery = _T("");
					m_strLastError = _T("");
					m_dwLastError = 0;
					m_pRecBinding = NULL;
					m_pRecordset.CreateInstance(__uuidof(Recordset));
					m_pCmd.CreateInstance(__uuidof(Command));
					m_nEditStatus = CADORecordset::dbEditNone;
					m_nSearchDirection = CADORecordset::searchForward;
				}
				
				CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
				{
					m_pRecordset = NULL;
					m_pCmd = NULL;
					m_strQuery = _T("");
					m_strLastError = _T("");
					m_dwLastError = 0;
					m_pRecBinding = NULL;
					m_pRecordset.CreateInstance(__uuidof(Recordset));
					m_pCmd.CreateInstance(__uuidof(Command));
					m_nEditStatus = CADORecordset::dbEditNone;
					m_nSearchDirection = CADORecordset::searchForward;
				
					m_pConnection = pAdoDatabase->GetActiveConnection();
				}
				
				BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, int nOption)
				{	
					Close();
					
					if(strcmp(lpstrExec, _T("")) != 0)
						m_strQuery = lpstrExec;
				
					ASSERT(!m_strQuery.IsEmpty());
				
					if(m_pConnection == NULL)
						m_pConnection = mpdb;
				
					m_strQuery.TrimLeft();
					BOOL bIsSelect = m_strQuery.Mid(0, strlen("Select ")).CompareNoCase("select ") == 0 && nOption == openUnknown;
				
					try
					{
						m_pRecordset->CursorType = adOpenStatic;
						m_pRecordset->CursorLocation = adUseClient;
						if(bIsSelect || nOption == openQuery || nOption == openUnknown)
							m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE), 
											adOpenStatic, adLockOptimistic, adCmdUnknown);
						else if(nOption == openTable)
							m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE), 
											adOpenKeyset, adLockOptimistic, adCmdTable);
						else if(nOption == openStoredProc)
						{
							m_pCmd->ActiveConnection = mpdb;
							m_pCmd->CommandText = _bstr_t(m_strQuery);
							m_pCmd->CommandType = adCmdStoredProc;
							m_pConnection->CursorLocation = adUseClient;
							
							m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
						}
						else
						{
							TRACE( "Unknown parameter. %d", nOption);
							return FALSE;
						}
					}
					catch(_com_error &e)
					{
						dump_com_error(e);
						return FALSE;
					}
				
					return m_pRecordset != NULL;
				}
				
				BOOL CADORecordset::Open(LPCTSTR lpstrExec, int nOption)
				{
					ASSERT(m_pConnection != NULL);
					ASSERT(m_pConnection->GetState() != adStateClosed);
					return Open(m_pConnection, lpstrExec, nOption);
				}
				
				BOOL CADORecordset::OpenSchema(int nSchema, LPCTSTR SchemaID)
				{
					try
					{
						_variant_t vtSchemaID = vtMissing;
				
						if(strlen(SchemaID) != 0)
							vtSchemaID = SchemaID;
							
						m_pRecordset = m_pConnection->OpenSchema((enum SchemaEnum)nSchema, vtMissing, vtSchemaID);
						return TRUE;
					}
					catch(_com_error &e)
					{
						dump_com_error(e);
						return FALSE;
					}
				}
				
				BOOL CADORecordset::Requery()
				{
					if(IsOpen())
					{
						try
						{
							// 如果提示adExecuteRecord无定义,请导入WIN2000下的msado15.dll
							m_pRecordset->Requery(adExecuteRecord);
						}
						catch(_com_error &e)
						{
							dump_com_error(e);
							return FALSE;
						}
					}
					return TRUE;
				}
				
				
				BOOL CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
				{	
					double val = (double)NULL;
					_variant_t vtFld;
					
					try
					{
						vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
						switch(vtFld.vt)
						{
						case VT_R4:
							val = vtFld.fltVal;
							break;
						case VT_R8:
							val = vtFld.dblVal;
							break;
						case VT_DECIMAL:
							//Corrected by Jos			

相关资源