ADO 数据连接学习笔记
参考资料:VC知识库在线杂志:http://www.vckbase.com/vckbase/default.aspx
CodeProject网站:http://www.codeproject.com/search.aspx?q=CListCtrlEx&sbo=kw
记录一下重点步骤:
初始化ole连接
Initializing the OLE/COM Libraries
1 BOOL CADOMFC1App::InitInstance() 2 { 3 // Add this function to initialize the OLE/COM libraries 4 AfxOleInit();
引入ado组件
Changes to StdAfx.h
1 #include <comdef.h> 2 3 #import "C:\program files\common files\system\ado\msado15.dll" no_namespace \ 4 rename( "EOF", "adoEOF" )
申明数据连接变量
Changes to the Document Header File
1 class CADOMFC1Doc : public CDocument 2 { 3 // Attributes 4 public: 5 BOOL m_IsConnectionOpen; 6 _ConnectionPtr m_pConnection;
数据库连接及错误处理
OnNewDocument with Exception Handling
1 BOOL CADOMFC1Doc::OnNewDocument() 2 { 3 if (!CDocument::OnNewDocument()) 4 return FALSE; 5 6 // TODO: add reinitialization code here 7 // (SDI documents will reuse this document) 8 HRESULT hr; 9 try 10 { 11 hr = m_pConnection.CreateInstance( __uuidof( Connection ) ); 12 if (SUCCEEDED(hr)) 13 { 14 hr = m_pConnection->Open(_bstr_t(L"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tee;Data Source=SQL\\SQLSERVER2000"), 15 _bstr_t(L""), 16 _bstr_t(L""), 17 adModeUnknown); 18 19 //获取数据连接字串可以新建一个.udl的文件,执行之后可以查看。 20 if (SUCCEEDED(hr)) 21 { 22 m_IsConnectionOpen = TRUE; 23 TRACE("连接成功\r\n"); 24 } 25 } 26 27 } 28 catch (_com_error &e) 29 { 30 // Get info from _com_error 31 _bstr_t bstrSource(e.Source()); 32 _bstr_t bstrDescription(e.Description()); 33 TRACE( "Exception thrown for classes generated by #import" ); 34 TRACE( "\tCode = %08lx\n", e.Error()); 35 TRACE( "\tCode meaning = %s\n", e.ErrorMessage()); 36 TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource); 37 TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription); 38 } 39 catch(...) 40 { 41 TRACE( "*** Unhandled Exception ***" ); 42 } 43 44 return TRUE; 45 }
数据集获取及数据显示
retrieve and show data
//这里用到一个类,名字为CListCtrlEx,用于列表显示数据
1 _RecordsetPtr pRecordSet; 2 CADOMFC1Doc * pDoc; 3 pDoc = GetDocument(); 4 _bstr_t bstrQuery("SELECT * FROM Data"); 5 _variant_t vRecsAffected(0L); 6 try 7 { 8 pRecordSet = pDoc->m_pConnection->Execute(bstrQuery, &vRecsAffected, 9 adOptionUnspecified); 10 if (!pRecordSet->GetadoEOF()) 11 { 12 CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl(); 13 ctlList.DeleteAllItems(); 14 while(ctlList.DeleteColumn(0)); 15 ctlList.AddColumn(" x ",0); 16 ctlList.AddColumn(" y ",1); 17 int i = 0; 18 _variant_t vFirstName; 19 _variant_t vLastName; 20 while (!pRecordSet->GetadoEOF()) 21 { 22 vFirstName = pRecordSet->GetCollect(L"x"); 23 ctlList.AddItem(i,0,(_bstr_t) vFirstName); 24 vLastName = pRecordSet->GetCollect(L"y"); 25 ctlList.AddItem(i,1,(_bstr_t) vLastName); 26 i++; 27 pRecordSet->MoveNext(); 28 } 29 } 30 pRecordSet->Close(); 31 } 32 catch( _com_error &e ) 33 { 34 // Get info from _com_error 35 _bstr_t bstrSource(e.Source()); 36 _bstr_t bstrDescription(e.Description()); 37 TRACE( "Exception thrown for classes generated by #import" ); 38 TRACE( "\tCode = %08lx\n", e.Error()); 39 TRACE( "\tCode meaning = %s\n", e.ErrorMessage()); 40 TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource); 41 TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription); 42 } 43 catch(...) 44 { 45 TRACE( "*** Unhandled Exception ***" ); 46 }
断开连接
1 try 2 { 3 if (m_IsConnectionOpen) 4 { 5 m_IsConnectionOpen = FALSE; 6 m_pConnection->Close(); 7 TRACE("断开成功\r\n"); 8 } 9 10 } 11 catch (_com_error &e) 12 { 13 _bstr_t bstrSource(e.Source()); 14 _bstr_t bstrDescription(e.Description()); 15 TRACE( "Exception thrown for classes generated by #import" ); 16 TRACE( "\tCode = %08lx\n", e.Error()); 17 TRACE( "\tCode meaning = %s\n", e.ErrorMessage()); 18 TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource); 19 TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription); 20 } 21 catch(...) 22 { 23 TRACE( "*** Unhandled Exception ***" ); 24 }
浙公网安备 33010602011771号