The content is messy. as a draft, the existing ado database operation function methods are summarized.
Small Functions
M_pRecordset-> RecordCount // number of records retrieved
Global Variables
# Import "msado15.dll" no_namespace rename ("EOF", "adoEOF") rename ("BOF", "adoBOF ")
_ ConnectionPtr m_pConnection;
_ RecordsetPtr m_pRecordset;
1. Connect the data source OnInitADOConn ();
// Initialize the OLE/COM library Environment
: CoInitialize (NULL );
Try {
// Create a Connection object
M_pCOnnection.CreateInstance ("ADODB. Connection ");
// Set the connection string
_ Bstr_t strConnect = "uid =; pwd =; DRIVER = {Microsoft Access Driver (* mdb)}; DBQ = database. mdb ;";
M_pConnection-> Open (strConnect, "", "", adModeUnknown );
}
Catch (_ com_error e ){
AfxMessageBox (e. Description );
}
2. Close the database connection
If (m_pRecordset! = NULL)
M_pRecordset-> Close ();
M_pConnection-> Close ();
: CoUninitialize (); // release the environment
3. Obtain Record set data
Input A _ bstr_t bstrSQL
(1) Open Method
Try {
If (m_pConnection = NULL) // if it is NULL, reconnect
OnInitADOConn ();
M_pRecordset.CreateInstance (_ uuidof (Recordset ));
M_pRecordset-> Open (bstrSQL, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext );
} // Adshorttext: bstrSQL is text. adCmdTable: bstrSQL is the table name.
(2) Execute Method
_ RecordsetPtr Execute (_ bstr_t CommandTex, VARIANT * RecordsAffeced, long Options)
CommandText is an SQL command, RecordsAffeced is the number of rows affected after the operation, and Options is the content type in CommandText.
Options: adCmdTable text, adCmdTable table name, adw.storedproc stored procedure, adCmdUnknown type unknown
>>_ Variant_t RecordsAffected;
Try {
If (m_pConnection = NULL) // whether to connect to the database
OnInitADOConn (); // reconnect again
M_pConnection-> Execute (bstrSQL, NULL, ad1_text );
}
Catch (_ com_error e)
{
E. Description ();
Return false;
}
4. Traverse Record Sets
MoveNext, MoveFirst, MoveLast, MovePrevious;
(Char *) (_ bstr_t) m_pRecordset-> GetCollect ("name"); // method 1
M_sName = (CStringW) (m_pAdoRecordset-> Fields-> Item [_ variant_t ("NAME")]-> Value); // method 2
While (m_pRecordset-> adoEOF = 0)
{M_pRecordset-> MoveNext ();}
5. add data
Try {
M_pRecordset-> AddNew (); // start adding
M_pRecordset-> PutCollect ("column name", (_ bstr_t) m_id );
M_pRecordset --> Update (); // Update a field
}
6. modify data
Try {
M_pRecordset-> Move (long) pos, vtMissing );
M_pRecordset-> PutCollect ("name", (_ bstr_t) m_name );
M_pRecordset --> Update ();
}
7. delete data
M_pRecordset-> Move (long) pos, vtMissing );
M_pAdoRecordset-> Delete (adAffectCurrent );
M_pRecordset --> Update ();
Try
{
// Assume that 10th records are deleted.
M_pRecordset-> MoveFirst (); // be sure to move to the first
M_pRecordset-> Move (9 );
M_pRecordset-> Delete (adAffectCurrent );
// The adAffectCurrent parameter is used to delete the current record.
M_pRecordset-> Update ();
}
Catch (_ com_error * e)
{
AfxMessageBox (e-> ErrorMessage ());
}
8. Save the image (refer to the next blog)
Char * m_pBuffer; // file data
DWORD m_filelen; // file length
VARIANT varblob;
SAFEARRAY * psa;
SAFEARRAYBOUND rgsabound [1];
Rgsabound [0]. lLbound = 0;
Rgsabound [0]. cElements = m_filelen;
Psa = SafeArrayCreate (VT_UI1, 1, rgsabound );
For (long I = 0; I <(long) m_filelen; I ++)
{
SafeArrayPutElement (psa, & I, m_pBuffer ++ );
}
// Record value
Varblob. vt = VT_ARRAY | VT_UI1;
Varblob. parray = psa;
M_pRecordset-> GetFields ()-> GetItem ("PHOTO_DATA")-> AppendChunk (varblob );
M_pRecordset-> Update ();
Read database voice data
Long lDataSize = m_pRecordset-> GetFields ()-> GetItem ("voice")-> ActualSize; // obtain the size of the Data Area
Char * m_pBuffer;
If (lDataSize> 0)
{
// Read data to varBLOB
_ Variant_t varBLOB;
VarBLOB = m_pRecordse-> GetFields ()-> GetItem ("voice")-> GetChunk (lDataSize );
If (varBLOB. vt = (VT_ARRAY | VT_UI1 ))
{
If (m_pBuffer = new char [lDataSize + 1])
{
Char * pBuf = 0;
SafeArrayAccessData (varBLOB. parray, (void **) & pBuf );
Memcpy (m_pBuffer, pBuf, lDataSize); // assign values to m_pBuffer
SafeArrayUnaccessData (varBLOB. parray );
}
}
}
Other Connection Methods
Open
M_pAdoRecordset = NULL;
M_pAdoConnect = NULL; // Initialization
ICurrentOne =-1;
//************************************** *****
If (FAILED (: CoInitialize (NULL) // This sentence is very important!
{
: AfxMessageBox (_ T ("fail to CInitialize (NULL )"));
PostQuitMessage (-8 );//?
}
HRESULT hr = m_pAdoConnect.CreateInstance (_ uuidof (Connection ));
If (FAILED (hr )){
: AfxMessageBox (_ T ("fail to create instance for _ ConnectPtr "));
PostQuitMessage (-8 );
}
Bstr_t strConnect = "DSN = FRDB ;\
DBQ = H :\\ FRDB. accdb ;\
DriverID = 25; FIL = MS Access; MaxBufferSize = 2048; PageTimeout = 5 ;";
Try {
M_pAdoConnect-> Open (strConnect, "clc", "", NULL );
}
Catch (_ com_error & e)
{
: AfxMessageBox (e. Description ());
PostQuitMessage (-8 );
}
M_pAdoRecordset = NULL;
Hr = m_pAdoRecordset.CreateInstance (_ uuidof (Recordset ));
If (FAILED (hr )){
: AfxMessageBox (_ T ("fail to create instance for _ RecordsetPtr "));
PostQuitMessage (-8 );
}
M_pAdoRecordset-> Open (_ variant_t ("Person"), _ variant_t (IDispatch *)
M_pAdoConnect, true), adOpenKeyset, adLockOptimistic, adCmdTable );
Close
If (m_pAdoRecordset) {if (m_pAdoRecordset-> State = adStateOpen ){
M_pAdoRecordset-> Close ();}}
If (m_pAdoConnect)
{If (m_pAdoConnect-> State = adStateOpen ){
M_pAdoConnect-> Close ();}}
: CoUninitialize (); // shut down the database under the thread
Add data
M_pAdoRecordset-> Fields-> GetItem (_ variant_t ("NAME")-> Value = _ variant_t (m_sName );
Load data
M_sName = (CStringW) (m_pAdoRecordset-> Fields-> Item [_ variant_t ("NAME")]-> Value );
Http://blog.hi.mop.com/blog/14401326/2743719.html