在vc6.0或者.net里面使用ado功能必须要遵循以下原则:
1.在StdAfx.h里面首先将IDE自己加上的和数据库有关的代码注释掉,例如:#include <afxdb.h>、#include <afxdao.h>等这样的代码。
2.重新加入新的ado支持库,代码如下
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
这段代码插入的位置也是由要求的,一般放在#endif // _AFX_NO_AFXCMN_SUPPORT这段代码后面
3.在你要调用数据库的cpp文件中加入数据库的初始化代码和操作数据库的代码(一下代码仅供参考)
//初始化
void AdoDB::Initialize( )
{
// TODO: Add your specialized code here.
//初始化OLE/COM环境(ADO) i_by_phg_050923
HRESULT hr;
try
{
//m_pConnection = NULL;
AfxOleInit();
hr = m_pConnection.CreateInstance(__uuidof(Connection));///创建Connection对象
if(m_pConnection->State)
m_pConnection->Close(); ///如果已经打开了连接则关闭它
if(SUCCEEDED(hr))
{
CString strCoon;
strCoon = CommonFunc.GetDBConn();
hr = m_pConnection->Open(_bstr_t(strCoon),"","",adModeUnknown); //连接数据库
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
m_pRecordset.CreateInstance("ADODB.Recordset");//创建Recordset对象的实例
}
//往数据库指定的表中插入数据
//下面的代码是实际代码,具体的变量可以替换成你自己的
void AdoDB::InsertPRec(PipeManager * pMgr)
{
Initialize();//initialize the database envirement
CString ClientDir;//ELDesign 安装目录
CString IniFileName; //clientcfg.ini文件的路径+6
CString ProjectName; //项目的名字
CString ProjectDB; //项目数据库的名字
ProjectDB = "";
CString DefaultStr = ""; //参数
IniFileName = CommonFunc.GetClientDir() + "\\clientcfg.ini";
GetPrivateProfileString("Interface","ProjectDB","DefaultStr",ProjectName.GetBuffer(50),50,IniFileName); //从ini文件中读出当前要操作的数据库
ProjectDB = LPCTSTR(ProjectName);
CString strSql;
strSql = "select * from EL_Project_" + ProjectDB + "..TPipeData";
m_pRecordset->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
const CString ArrPLD[] = {"Size","Fluid_Service","Number","Area","Specification","Insulation_Table",
"Insulation_Index","Insulation_Condition","Paint_Code","TracingNOT","Tracing_Size",
"Tracing_Type","Project1","Project2","Project3"
};//Pipe Label Details
CString SNHandle;
PrjManager* PrjMgr = new PrjManager();
PrjMgr->SetPLABFO();
list<UnSettledPipe> pipe_list;
pipe_list = pMgr->GetStPipeList();
list<UnSettledPipe>::iterator pipe_list_ite = pipe_list.begin();
//CArray<int,int> ArrPLabFo = PrjMgr->ArrPLABFO;
list<StartNode> sn_list = pMgr->GetPsnList();
list<StartNode>::iterator sn_list_ite;
list<CString> x_data;
list<CString>::iterator x_data_ite;
//iterator the pipe list
for(int i = 0; i < pipe_list.size(); i++)
{
SNHandle = pipe_list_ite->SNHandle;
//iterator the psn list for the right x_data
sn_list_ite = sn_list.begin();
for (int j = 0; j < sn_list.size(); j++ )
{
if(SNHandle == sn_list_ite->Handle)
{
x_data.clear();
x_data = sn_list_ite->x_data;
break;
}
sn_list_ite++;
}
try
{
m_pRecordset->AddNew();
m_pRecordset->PutCollect("DwgNo",_variant_t("-1"));
m_pRecordset->PutCollect("PipeHandle",_variant_t(pipe_list_ite->Handle));//管道句柄
m_pRecordset->PutCollect("PiPeSNHandle",_variant_t(pipe_list_ite->SNHandle)); //管头句柄
m_pRecordset->PutCollect("PiPeENHandle",_variant_t(pipe_list_ite->ENHandle)); //管头句柄
int m;
x_data_ite = x_data.begin();
for( int k = 0; k < x_data.size(); k++)
{
m = PrjMgr->ArrPLABFO[k];
m_pRecordset->PutCollect(_variant_t(ArrPLD[m]),_variant_t(*x_data_ite));
x_data_ite++;
}
m_pRecordset->Update();
}
catch(_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
}
pipe_list_ite++;
}
}