stdafx.h中写入
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
rename("EOF","adoEOF") rename("BOF","adoBOF")
#define Provider
"Provider=SQLOLEDB.1;Integrated Security=SSPI;\
Persist Security Info=False;Initial Catalog=HRMS"
插入一个类 class type 选择 Generic class
比如名字为 CADO 其他默认
ADO.H
private:
HDC m_hDC;
DWORD m_CurrentRecord;
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
在构造函数那边写入
CADOOperation::CADOOperation()
{
try{
m_pConnection.CreateInstance(_uuidof(Connection));
m_pConnection->Open(_bstr_t(Provider), "", "", adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description(), MB_OK);
exit(0);
}
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_CurrentRecord = -1;
}
CADOOperation::~CADOOperation()
{
m_pConnection->Close();
}
BOOL CADOOperation::OpenTable(CString name) //打开表
{
ASSERT(!name.IsEmpty());
CString sql;
sql.Format("select * from %s" , name);
try
{
m_pRecordset->Open(_bstr_t(sql),
m_pConnection.GetInterfacePtr(),
adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error e)
{
#ifdef _DEBUG
ShowError(e.Description());
#endif
return FALSE;
}
return TRUE;
}
BOOL CADOOperation::CloseTable()
{
try
{
if(adStateOpen == m_pRecordset->GetState())
m_pRecordset->Close();
}
catch(_com_error e)
{
return FALSE;
}
return TRUE;
}
HRESULT CADOOperation::AddNew()
{
return m_pRecordset->AddNew();
}
BOOL CADOOperation::UpdateData()
{
try
{
m_pRecordset->Update();
}
catch(_com_error e)
{
#ifdef _DEBUG
ShowError(e.Description());
#endif
return FALSE;
}
return
TRUE;
}
余下的。。。。