4,011
社区成员
发帖
与我相关
我的任务
分享
/ HRecordset 所有数据集的基类 实现
// 代码生成在 2012年2月29日
#include "stdafx.h"
//#include "Resource.h"
#include "hData.h"
#include "HRecordset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNAMIC(HRecordset, CRecordset)
//CString HRecordset::connectstring = _T("DSN=TTM MySQL;");
//CString HRecordset::connectstring = _T("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=ttmdb; User=root; Password=bioyear; Option=3;");
HRecordset::HRecordset(CDatabase* pdb)
: CRecordset(pdb)
{
m_nDefaultType = snapshot; //用动态集时dynaset,打开MySql数据库时,提示数据库不支持动态集。
m_bRecordsetDb2 = false;
}
HRecordset::~HRecordset()
{
if( IsOpen() )
Close();
if( m_bRecordsetDb2 ) // 从Recodset 打开数据库
{
m_pDatabase->Close();
delete m_pDatabase;
m_pDatabase = NULL;
}
}
CString HRecordset::GetDefaultConnect()
{
return hData::connectstring;
}
// 显示等待框
/*BOOL HRecordset::Open( UINT nOpenType, LPCTSTR lpszSQL, DWORD dwOptions )
{
DlgWait dw; // 等待框
//dw.Create( IDD_WAIT, AfxGetMainWnd() );
//dw.ShowWindow(SW_SHOW); //?? 改用线程方式,过一个再弹出,改善视觉效果。
//dw.UpdateWindow();
BOOL result = Open2( nOpenType, lpszSQL, dwOptions );
if( dw.GetSafeHwnd() )
dw.EndDialog(0);
return result;
}*/
// 有问题时不显示对话框
BOOL HRecordset::Open( UINT nOpenType, LPCTSTR lpszSQL, DWORD dwOptions )
{
try{
if( !m_pDatabase ){ //调用!CDatabase::OpenEx不显示问题对话框。
m_bRecordsetDb2 = TRUE; // 从Recodset 打开数据库
m_pDatabase = new CDatabase();
if ( !m_pDatabase->OpenEx( hData::connectstring, CDatabase::noOdbcDialog ) )
{
return FALSE;
}
}
ASSERT( m_pDatabase->IsOpen() );
CRecordset::Open(nOpenType, lpszSQL, dwOptions );
if( !IsOpen() )
{
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
catch(CDBException* e)
{
if( e->m_strError.IsEmpty() )
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
else
AfxMessageBox(e->m_strError, MB_ICONEXCLAMATION);
// Delete the incomplete recordset object
e->Delete();
return FALSE;
}
catch(...)
{
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
return FALSE;
}
}
// 有问题时不显示对话框
BOOL HRecordset::Open2( LPCTSTR lpszFilter, DWORD dwOptions, UINT nOpenType )
{
CString str = GetDefaultSQL();
CString strsql;
if( str.IsEmpty() )
strsql = lpszFilter;
else if( lpszFilter && (wcslen(lpszFilter) > 0) )
strsql.Format( _T("%s %s"), GetDefaultSQL(), lpszFilter );
else
strsql = GetDefaultSQL();
return Open( AFX_DB_USE_DEFAULT_TYPE, strsql, dwOptions );
}
//打开access数据库, filename数据库文件名,strsql SQL语句
bool HRecordset::openmdb( LPCTSTR filename, LPCTSTR strsql )
{
m_nDefaultType = dynaset; //用snapshot,打开ACCESS数据库时,提示数据集只读。
try{
if( !m_pDatabase ){ //调用!CDatabase::OpenEx不显示问题对话框。
CString constr;
// 在win7x64中用C:\Windows\SysWOW64\odbcad32.exe配置TTMv7数据源
constr.Format( _T("DSN=TTM MDB;DBQ=%s;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;PWD=bioyearttmv60;UID=admin;"), filename ); // access 连接字符串
m_bRecordsetDb2 = TRUE; // 从Recodset 打开数据库
m_pDatabase = new CDatabase();
//if ( !m_pDatabase->OpenEx( constr, CDatabase::noOdbcDialog ) )
if ( !m_pDatabase->Open( 0, FALSE, FALSE, constr, FALSE ) )
{
return FALSE;
}
}
ASSERT( m_pDatabase->IsOpen() );
CRecordset::Open(AFX_DB_USE_DEFAULT_TYPE, strsql);
if( !IsOpen() )
{
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
catch(CDBException* e)
{
if( e->m_strError.IsEmpty() )
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
else
AfxMessageBox(e->m_strError, MB_ICONEXCLAMATION);
// Delete the incomplete recordset object
e->Delete();
return FALSE;
}
catch(...)
{
AfxMessageBox(L"数据库错误。", MB_ICONEXCLAMATION);
return FALSE;
}
}
void HRecordset::GetSingle( LPCTSTR strsql, CString & result )
{
if( Open( AFX_DB_USE_DEFAULT_TYPE, strsql ) == FALSE )
{
return;
}
if( GetRecordCount() )
GetFieldValue( (short)0, result );
}
#ifdef _DEBUG
void HRecordset::AssertValid() const
{
CRecordset::AssertValid();
}
void HRecordset::Dump(CDumpContext& dc) const
{
CRecordset::Dump(dc);
}
#endif //_DEBUG