|
|
 |
在vc6.0 or .net里面使用ado功能 |
热 ★★★★ |
|
在vc6.0 or .net里面使用ado功能 |
|
作者:闵涛 文章来源:闵涛的学习笔记 点击数:888 更新时间:2009/4/23 10:48:24  |
|
|
在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++; } }
[办公软件]如何在office(PowerPoint,Word,Excel)中制作带圈的… [办公软件]批量删除Office文档(word,excle,powerpoint)中的超… [办公软件]Office(Word,Excel)密码破解软件(Office Password… [办公软件]在powerpoint中快速插入word表格 [办公软件]快速将Word文档转换为PPT演示文档 [办公软件]Word、Excel和PowerPoint文件格式兼容包(简体中文… [办公软件]在Office(word,excel)中输入各级钢筋符号的方法 [办公软件]Office文档(Word,PPT)的作者、备注、最后修改时间… [办公软件]让EXCEL,WORD时间自动更新 [网络安全]激活型触发式AutoRun.inf病毒和rose病毒的清除方案
|
|
| 教程录入:mintao 责任编辑:mintao |
|
|
上一篇教程: c++ builder中的ado使用 下一篇教程: ADO.NET 2.0 Dataset和Datatable 新功能新特性 |
|
|
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网] |
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| |
|
|
|
|
|
 |
同类栏目 |
 |
 |
赞助链接 |
 |
|
500 - 内部服务器错误。
|
|
|
|
|
|