ADO

xiaoxiao2021-03-06  47

#import "c: / program files / common files / system / ado / msdao15.dll" no_namespaces rename "," adoEof ")

_ConnectionPtr, this is the connection interface. It is similar to CDatabase or CDaoDatabase. It basicly works the same way. You create an instance of it, point it to a database either through ODBC or a provider, and you open it. Look how similar IT is to cdaodatabase:

Cdaodatabase mydb = new cdaodatabase (); m_daoserverdb.open (null, false, false, "ODBC; DSN = SAMS_SVR; UID = admin; pwd = admin");

Now Using Ado: _ConnectionPtr MyDb; Mydb.createInstance (__ uuidof (connection)); mydb-> open ("DSN = SAMS_SVR; UID = Admin; PWD = Admin", ",", - 1); _ RecordSetptr, this IS . the recordset interface It is similar to CDaoRecordset Again when you see how similar it is to CDaoRecordset you will wonder why you did not use it sooner Lets see how they work:.. (We will use the database and the connection above in our example .)

CDAORECORDSET MySet = New CDAORECORDSET (MYDB); MySet-> Open (AFX_DAO_USE_DEFAULT_TYPE, "Select * from Some_Table"); Now Using ADO:

_RecordsetPtr MySet; MySet.CreateInstance (__ uuidof (Recordset)); MySet-> Open ( "SELECT * FROM some_table", MyDb.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adCmdText);. ADO is slightly more involved But with some of the added Benifits you get with ado (Most of Which Are Beyond The Scope of this Article) Ado IS Worth The Extra Effort Here.

Now That We Have A Connection and A Recordset, Lets Get Some Data Out of It. In Both Cases We Are Going to Fill A List Box with Information In The RecordSet (Assuming We Have A ListBox Called M_List)

DAO: VARIANT * vFieldValue; COleVariant covFieldValue; CString Holder; while {MySet-> GetFieldValue ( "FIELD_1", covFieldValue); vFieldValue = (LPVARIANT) covFieldValue; if (vFieldValue-> vt = (MySet-> IsEOF ()!)! Vt_null) {Holder.Format ("% s", vfieldvalue-> pbval); m_list.addstring (Holder);} myset.movenext ();} ado: _variant_t HolderWhile (! Myset-> adoEof) {Holder = MySet-> GetCollect ("Field_1"); if (Holder.Vt! = VT_NULL) M_List.Addstring ((char *) _ bstr_t (Holder)); MySet-> MoveNext ();} a special note. There is no documentation for the getCollect Method . I have search everywhere, and no one mentionsed it. The Other method of Retrieving Data Would Be:

Holder = MySet-> getfields-> field -> (_ variant_t (fieldnumber)) -> value; i like the getcollect better.

转载请注明原文地址:https://www.9cbs.com/read-80041.html

New Post(0)