This article is welcome to reprint, please specify the source and the author blackcolor@263.net -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------
Actual comb (06) ---- Use OLE DB Connection Database in COM
First, establish a component 1) Create a process within the process, "Allow Merging of Proxy / Stub Code" in the created window "ATL COM Appwizard - Step 1 of 1", add a "Simple Object", name " SIMPLE04 "2) Add the following methods in this assembly: HRESULT CONNECTOSQL (Void); HRESULT MOVENEXT (VOID); HRESULT GETAU_ID ([OUT, RETVAL] BSTR * BAU_ID); HRESULT GETAU_LNAME ([OUT, RETVAL] BSTR * BAU_LNAME); HRESULT GETAU_FNAME ([OUT, RETVAL] BSTR * BAU_FNAME); HRESULT Disconnect (Void); 3) If you are not familiar with the above two steps, please refer to "Battle COM (02) ---- Create a Procedure Component" One article 4) The modified code is as follows:
Document: Simple04.h // Simple04.h: Declaration of the csimple04 # include
#ifndef __simple04_h_ # define __simple04_h_
#include "resource.h" // main symbols
/ / Define a new class Cauthors {public: // data-width is derived from the table authors char m_szau_id [11]; char m_szau_lname [40]; char m_szau_fname [20]; // Output binding begin_column_map (cauthors) column_entry (CAUTHHORS) Column_ENTRY 1, m_szau_id) Column_entry (2, m_szau_lname) Column_ENTRY (3, M_SZAU_FNAME) End_Column_map ()
// Parameter Bind Begin_Param_map (Cauthors) Column_Entry (1, M_SZAU_LNAME) end_param_map ()};
/// CSimple04class ATL_NO_VTABLE CSimple04: public CComObjectRootEx
DECLARE_REGISTRY_RESOURCEID (idR_SIMPLE04)
Declare_protect_final_construct ()
Begin_COM_MAP (CSIMPLE04) COM_INTERFACE_ENTRY (ISIMPLE04) COM_INTERFACE_ENTRY (Idispatch) end_com_map ()
// Add variables public: CDataSource m_Connection; CSession m_session; CCommand
#ENDIF: / / __ simple04_h_
// File: Simple04.cpp // Simple04.cpp: Implementation of csimple04 # include "stdafx.h" #include "step04.h" #include "simple04.h" // Add Comutil support #include
/// csimple04
/ / Connect to the service SQL, and retrieve the authors data stdmethodimp csimple04 :: connecttOSQL () {// If the connection has been established, then turn off the if (succeeded (m_hr)) {m_session.close (); m_connection.close () ; // Close connection M_hr = E_FAIL;}
// open the database CDBPropSet dbinit (DBPROPSET_DBINIT); dbinit.AddProperty (DBPROP_INIT_DATASOURCE, OLESTR ( "GP2000")); // server name dbinit.AddProperty (DBPROP_AUTH_USERID, OLESTR ( "sa")); // username dbinit.AddProperty ( DBPROP_AUTH_PASSWORD, OLESTR ( "123")); // password dbinit.AddProperty (DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, false); dbinit.AddProperty (DBPROP_INIT_CATALOG, OLESTR ( "pubs")); // database name dbinit.AddProperty (DBPROP_INIT_LCID, (long) 2052 ); Dbinit.addproperty (dbprop_init_prompt, (short) 4); m_hr = m_connection.open (_t ("sqloledb.1"), & dbinit); if (failed (m_hr)) return m_hr; m_hr = m_session.Open (m_connection) ; If (failed (m_hr)) {m_connection.close (); Return M_hr;} // Set Query Conditions STRCPY (M_AUTHORS.M_SZAU_LNAME, "% H%"); // au_lname contains "H" author M_Authors.open (m_session, "select au_id, au_lname, au_fname from authors where au_lname like?"); return S_OK;} // get result set STDMETHODIMP CSimple04 :: MoveNext () {if (SUCCEEDED (m_hr)) {m_hr = m_Authors.MoveNext ( } Else {// Close connection disconnect ();
Return m_hr;} // Take data stdmethodimp csimple04 :: getau_id (BSTR * BAU_ID) {i (succeeded (m_hr)) {* bau_id = _com_util :: convertstringtobstr (m_authors.m_szau_id);}
Return S_OK;
STDMETHODIMP CSIMPLE04 :: Getau_LName (BSTR * BAU_LNAME) {i (succeededed (m_hr)) {* bau_lname = _com_util :: convertstringtobstr (m_authors.m_szau_lname);}
Return S_OK;
STDMETHODIMP CSIMPLE04 :: Getau_FName (BSTR * BAU_FNAME) {i (successded (m_hr)) {* bau_fname = _com_util :: convertstringtobstr (m_authors.m_szau_fname);}
Return S_OK;
STDMETHODIMP CSIMPLE04 :: Disconnect () {if (successted (m_hr)) {m_session.close (); m_connection.close (); // Close connection M_hr = E_FAIL;}
Return S_OK;
Second, establish the client 1) The file is as follows: // Client.cpp file #include
/ / Contains the definition file of the COM, the following two files are copied from STEP04 #include "../step04/step04.h"#include" ../step04/step04_i.c "
Void main () {isimple04 * pisimple04 = null; // We define interfaces BSTR BAU_ID, BAU_LNAME, BAU_FNAME; / / Define Data Char * PAU_ID, * PAU_LNAME, * PAU_FNAME
// Initialization HRESULT HR = Coinitialize (NULL); failed (HR)) {Printf ("Coinitialize Failed! HR = 0x% x", HR); Return;}
// Create a COM object HR = COCREATEINSTANCE (CLSID_SIMPLE04, NULL, CLSCTX_ALL, IID_ID_IMPLE04, (Void **) & pisimple04); if (Failed (HR)) {Printf ("Create COM FAILED! HR = 0x% x", HR); Couninitialize (); return;} // Connect SQL HR = Pisimple04-> ConnectTOSQL (); if (FAILED (HR)) {Printf ("Pisimple04-> ConnectTOSQL () Failed! Hr = 0x% x", HR); Pisimple04 -> Release (); counitialize (); return;} // Take data printf ("AU_ID / TAU_LNAME / T / T / TAU_FNAME / N"); while (Pisimple04-> MoveNext () == S_OK) {pisimple04-> Getau_id (& BAU_ID); Pisimple04-> getau_lname; pisimple04-> getau_fname (& bau_fname);
PAU_ID = _COM_UTIL :: ConvertBSTRTOSTRING (BAU_ID); PAU_LNAME = _COM_UTIL :: ConvertBSTRTSTRING (BAU_LNAME); PAU_FNAME = _COM_UTIL :: ConvertBSTRTOSTRING (BAU_FNAME);
Printf ("% S / T% S / T / T% S / N", PAU_ID, PAU_LNAME, PAU_FNAME;
// release the allocated memory SysFreeString (bAu_id); SysFreeString (bAu_lname); SysFreeString (bAu_fname); delete pAu_id; delete pAu_lname; delete pAu_fname;} // disconnected pISimple04-> DisConnect (); pISimple04-> Release (); Couninitialize ();
Return;}