📄 adodatabase.cpp
字号:
#include "stdafx.h"
#include "AdoDatabase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CAdoDatabase::CAdoDatabase()
{
::CoInitialize(NULL);
}
CAdoDatabase::~CAdoDatabase()
{
}
CAdoDatabase::CAdoDatabase(CString PP)
{
// m_connstr ="Provider=Microsoft.Jet.OLEDB.4.0;Integrated Security=SSPI;Persist Security Info=False;+"";Data Source=PP";
m_connstr="Provider=Microsoft.JET.OLEDB.4.0; Data source=" + PP;
::CoInitialize(NULL);
}
/**//******************************************************************************
Function : CAdoDatabase::ExecuteQuery
Author :
Purpose : 执行select语句
CString strSQL -
Return : _RecordsetPtr
History : Date 2006-9-12
******************************************************************************/
_RecordsetPtr CAdoDatabase::ExecuteQuery(CString strSQL)
{
HRESULT hRet = 0;
_variant_t RecordsAffected;
//AfxMessageBox("执行了查询");
try
{
hRet = m_conn.CreateInstance("ADODB.Connection");
if(FAILED(hRet))
{
return NULL;
}
hRet = m_conn->Open((_bstr_t)m_connstr, "", "", adModeUnknown);
if(FAILED(hRet))
{
return NULL;
}
m_rs = m_conn->Execute((_bstr_t)strSQL, &RecordsAffected, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox("数据库错误!");
}
return m_rs;
}
/**//******************************************************************************
Function : CAdoDatabase::ExecuteUpdate
Author :
Purpose : 执行insert,update等不需要返回结果的SQL语句
CString strSQL -
Return : HRESULT
History : Date 2006-9-12
******************************************************************************/
HRESULT CAdoDatabase::ExecuteUpdate(CString strSQL)
{
HRESULT hRet = 0;
_variant_t RecordsAffected;
try
{
hRet = m_conn.CreateInstance("ADODB.Connection");
if(FAILED(hRet))
{
return hRet;
}
hRet = m_conn->Open((_bstr_t)m_connstr, "", "", adModeUnknown);
if(FAILED(hRet))
{
return hRet;
}
m_conn->Execute((_bstr_t)strSQL, &RecordsAffected, adCmdText);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);/**////显示错误信息
}
return hRet;
}
/**//******************************************************************************
Function : CAdoDatabase::Close
Author :
Purpose : 关闭数据库
-
Return : void
History : Date 2006-9-12
******************************************************************************/
void CAdoDatabase::Close()
{
m_conn->Close();
}
CString CAdoDatabase::GetFieldValue(CString Field)
{
CString sValue;
_variant_t value;
value=m_rs->GetCollect((_bstr_t)Field);
if(value.vt==VT_EMPTY ||value.vt==VT_NULL)
sValue="";
else
{
sValue=(char*)(_bstr_t)value;
sValue.TrimRight();
sValue.TrimLeft();
}
return sValue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -