void connect() { _ConnectionPtr m_pConnection; CoInitialize(NULL); m_pConnection.CreateInstance(__uuidof(Connection)); // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息, // 因为它有时会经常出现一些想不到的错误。 try { // 打开本地Access库db1.mdb m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\PipeData.mdb","","",adModeUnknown); } catch(_com_error &e) { AfxMessageBox("数据库连接失败,确认数据库PipeData.mdb是否在当前路径下!") return FALSE; } //建立数据集 //------------------------------------------------------------------------------------- _RecordsetPtr m_pRecordset; m_pRecordset.CreateInstance(__uuidof(Recordset)); // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息, // 因为它有时会经常出现一些意想不到的错误。 try { m_pRecordset->Open("SELECT * FROM DXL", m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } //-------------------------------------------------------------------------------------- //读取数据 //-------------------------------------------------------------------------------------- _variant_t var,var1,var2; AcGePoint3d startpt,endpt; char *strID,*strID1,*str1,*str2; //float X,Y; try { if(!m_pRecordset->BOF) m_pRecordset->MoveFirst(); else { AfxMessageBox("表内数据为空"); return 1; }
// 读入库中各字段 while(!m_pRecordset->adoEOF) { var = m_pRecordset->GetCollect("起始点号"); if(var.vt != VT_NULL) strID= _com_util::ConvertBSTRToString((_bstr_t)var); //_variant_t转字符串 var2 = m_pRecordset->GetCollect("终止点号"); if(var.vt != VT_NULL) strID1= _com_util::ConvertBSTRToString((_bstr_t)var2); //_variant_t转字符串 while(!m_pRecordset->adoEOF) { m_pRecordset->Open("SELECT * FROM DXP", m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针 adOpenDynamic, adLockOptimistic, adCmdText); var1=m_pRecordset->GetCollect("物探点号"); str1= _com_util::ConvertBSTRToString((_bstr_t)var); //_variant_t转字符串 if(strID==str1) { var1 = m_pRecordset->GetCollect("X"); if(var1.vt != VT_NULL) startpt[X]=_com_util::ConvertBSTRToString((_bstr_t)var1); var1 = m_pRecordset->GetCollect("Y"); if(var1.vt != VT_NULL) startpt[Y]=_com_util::ConvertBSTRToString((_bstr_t)var1); } if(strID1==str1) { var1 = m_pRecordset->GetCollect("X"); if(var1.vt != VT_NULL) endpt[X]=_com_util::ConvertBSTRToString((_bstr_t)var); var1 = m_pRecordset->GetCollect("Y"); if(var1.vt != VT_NULL) endpt[Y]=_com_util::ConvertBSTRToString((_bstr_t)var); } m_pRecordset->MoveNext(); AcDbLine *pLine = new AcDbLine(startpt, endpt); //构建直线类(AcDbLine)的对象 AcDbBlockTable *pBlockTable; //指向块表的指针 acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead); //将指针指向当前数据库中的块表 AcDbBlockTableRecord *pBlockTableRecord; //指向块表记录的指针 //将块表记录的指针指向当前块表的模型空间记录 pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, cDb::kForWrite); pBlockTable->close(); //关闭块表 AcDbObjectId lineId; //指向ID pBlockTableRecord->appendAcDbEntity(lineId, pLine); //将指向添加到块表记录 pBlockTableRecord->close(); //关闭块表记录 pLine->close(); return lineId; }
m_pRecordset->MoveNext(); } } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } //-------------------------------------------------------------------------------------- //关闭数据集 m_pRecordset->Close(); m_pRecordset = NULL; //-------------------------------------------------------------------------------------- //关闭数据库连接 //-------------------------------------------------------------------------------------- if(m_pConnection->State) m_pConnection->Close(); m_pConnection= NULL; return 0; } |