1 / 12

Advance topic : Write your program with ADO

Advance topic : Write your program with ADO. Jing Min Quan( 井民全 ) National Chiao Tung University. Introduction. ADO = ActiveX Data Objects Access and manipulate data from a database server Benefits: ease of use high speed low memory key features building client/server

ondrea
Télécharger la présentation

Advance topic : Write your program with ADO

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advance topic :Write your program with ADO Jing Min Quan(井民全) National Chiao Tung University

  2. Introduction • ADO = ActiveX Data Objects • Access and manipulate data from a database server • Benefits: • ease of use • high speed • low memory • key features • building client/server • Web-based applications.

  3. Prerequisites • Tool->Options->Directories-> 1. [Show Directories for]: Library files 2. [Directories]: C:\program files\common files\system\ado

  4. Setup the Data Source [系統管理工具]->資料來源 (ODBC) [系統資料來源名稱]-> 新增; Access mdb [選取資料庫]->指定檔案路徑

  5. How to program– Read the Data from Database • 1. Import the ADO library #import <msado15.dll> rename( "EOF", "adoEOF" ) • 2. Initial OLE struct InitOle { InitOle() { ::CoInitialize(NULL); } ~InitOle() { ::CoUninitialize(); } } _init_InitOle_;

  6. How to program– Read the Data from Database • Connect to the Database ADODB::_ConnectionPtr Conn1 = NULL; CREATEINSTANCE(Conn1,ADODB::Connection) _bstr_t bstrAccessConnect ( L"DSN=AdoDemo;UID=;PWD=;" ); Conn1->ConnectionString = bstrAccessConnect; Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 ); 設定系統資料庫名稱, username,passwd 開啟一個 synchronously 連線

  7. How to program– Read the Data from Database • Create a Record Object ADODB::_RecordsetPtr rs("ADODB.Recordset"); rs->Open("Authors", //開啟Authors資料表 _variant_t((IDispatch *) Conn1, true), // ADO is a IDispatch interface ADODB::adOpenStatic, // 指定records為原資料的static copy. ADODB::adLockReadOnly, // 無法更改record 資料 ADODB::adCmdTable); // 指定Authors是資料表

  8. How to program– Read the Data from Database rs->MoveFirst(); while ( rs->adoEOF == VARIANT_FALSE ) { long int xx= rs->Fields->GetItem( _variant_t( 0L ) )->Value; _bstr_t KK=rs->Fields->Item[1L]->Value; char *cString=(char*) KK; rs->MoveNext(); } 取得Au_ID欄位 取得Name欄位 Move to next record

  9. How to program– Read the Data from Database • Close the Connection rs->Close(); Conn1->Close(); Ex:RecordSet.cpp

  10. How to program– Append the Data into Database • Create a Recordset which can be updated. ADODB::_RecordsetPtr rs("ADODB.Recordset"); rs->Open("Authors", _variant_t((IDispatch *) Conn1, true), ADODB::adOpenStatic, ADODB::adLockReadOnly, // 無法更改record 資料 ADODB::adCmdTable); ADODB::_RecordsetPtr rs("ADODB.Recordset"); rs->Open("Authors", //開啟Authors資料表 _variant_t((IDispatch *) Conn1, true), // ADO is a IDispatch interface ADODB::adOpenStatic, // 指定records為原資料的static copy. ADODB::adLockOptimistic, ADODB::adCmdTable); // 指定Authors是資料表

  11. How to program– Append the Data into Database • Insert the data 看看這個Recordset 是否支援AddNew rs->MoveFirst(); if( rs->Supports(ADODB::adAddNew)) { // 若rs 支援AddNew這個功能 rs->AddNew(); rs->Fields->Item["Au_ID"]->Value=(long)1234; rs->Fields->Item[1L]->Value="自強基金會"; rs->Update(); } rs->Close(); Conn1->Close(); 把資料update到資料庫中 實際的寫入資料庫

  12. Reference • Get the Help • http://www.microsoft.com/data/ado/ • Download the MADC 2.6 SDK • http://www.microsoft.com/data/download.htm

More Related