?? 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 + -