ADO Programming Applications (2)

Source: Internet
Author: User
Tags dsn include connect ole wrapper
Ado| programming Below is a simple example code based on ADO application using the #import method:
#include <windows.h>
#import <msado15.dll> Rename ("EOF", "adoeof")
void Main ()
{
HRESULT hr = S_OK;
Because the no_namespace is not specified in the #import, ADODB:: This form is used to define the variable type
Adodb::_recordsetptr Rs1 = NULL;
Creating an ADO connection through ODBC
_bstr_t Connect ("Dsn=adodemo; Uid=sa; pwd=; ");
_bstr_t Source ("SELECT * from Authors");
CoInitialize ();
Initializing the Rs1 object
hr = Rs1.createinstance (__uuidof (Adodb::recordset));
Omit the judgment of the return value HR
Rs1->open (Source,
Connect,
Adodb::adopenforwardonly,
Adodb::adlockreadonly,
-1);
Here you can add code to manipulate the recordset Rs1
Rs1->close ();
RS1 = NULL;
:: MessageBox (NULL, "success!", "" ", MB_OK);
CoUninitialize ();
}
2. Creating an ADO Application with MFC OLE
MFC OLE can also encapsulate (wrapper) a type library, but unlike #import, it cannot produce an enumeration type from a type library. MFC classes CString and COleVariant hide the details of BSTRs and variants. Classes generated by MFC OLE inherit the class COleDispatchDriver, and the failed HRESULTs generated by ADO is encapsulated in the class coledispatchexception.
The steps to create an ADO application with MFC OLE ClassWizard are as follows:
From the Tools menu, select the Directories tab entry in the Options option to add a path C:\program Files\Common Files\System in the library files in Show directories \ado, sets the path that contains the ADO type library.
From the View menu, activate ClassWizard, click the Add Class button and select the "from A Type Library ..." option, and then in the Type Library dialog Box dialog, from C:\program files\ Common Files\System\ado Select File Msado15.dll, in the Confirm Classes dialog box, select all listed classes and press the OK button to exit ClassWizard. In this way, ClassWizard generates two files Msado15.h and msado15.cpp.
Here is the sample code to implement the ADO application:
Initializing COM objects
AfxOleInit ();
...
Defining a DataSet Object
_recordset Rs1;
COleException e;
COleVariant Connect ("Dsn=adodemo; Uid=sa; pwd=; ");
COleVariant Source ("SELECT * from Authors");
Create DataSet Object
Rs1.createdispatch ("ADODB.") recordset.2.0 ", &E);
Rs1.open (VARIANT) Source,
(VARIANT) Connect,
0, 1,-1);
Here you can add code to process the result set RS1
Rs1.close ();
Rs1.releasedispatch ();
AfxMessageBox ("success!");
3. Create ADO project with COM API
Both #import and MFC OLE generate a wrapper class around a given automation object that inherits from _com_ptr_t and COleDispatchDriver, respectively. You can actually initialize an ADO object directly by using Windows API functions. To directly use ADO and COM objects, you need to add two header files Adoid.h and adoint.h, which define the CLSIDs, interface definitions, and the enumeration types required to manipulate the ADO type library. In addition, you will need to add header file INITGUID.H.
To be able to compile ADO engineering files created with COM APIs, you also need to install the OLE DB SDK or the MSDASDK tool in the machine. Here's a simple sample code that uses the API to create ADO:
#include <windows.h>
#include <initguid.h>
#include "adoid.h"//ADO GUID ' s
#include "adoint.h"//ADO classes, enumerations, etc.
void Main ()
{
HRESULT hr = S_OK;
Adorecordset is defined in adoint.h.
adorecordset* Rs1 = NULL;
VARIANT Source;
VARIANT Connect;
VariantInit (&source);
VariantInit (&connect);
SOURCE.VT = VT_BSTR;
Source.bstrval =:: SysAllocString (L "select * from Authors");
CONNECT.VT = VT_BSTR;
Connect.bstrval =:: SysAllocString (L "Dsn=adodemo); Uid=sa; pwd=; ");
hr = CoCreateInstance (Clsid_cadorecordset,
Null
Clsctx_inproc_server,
Iid_iadorecordset,
(LPVOID *) &rs1);
if (SUCCEEDED (hr)) hr = Rs1->open
(Source,
Connect,
adOpenForwardOnly,
adLockReadOnly,
-1);
To process a recordset Rs1
if (SUCCEEDED (hr)) hr = Rs1->close ();
if (SUCCEEDED (HR)) {rs1->release (); RS1 = NULL; }
if (SUCCEEDED (HR)):: MessageBox (NULL, "success!", "", MB_OK);
}
C + + Extensions
If you use C + + for ADO application development, you should use the ADO C + + Extensions. We know that using VB or VBScript to operate ADO is very convenient, but if you use C + + or Java, you have to deal with a data structure such as variants to achieve and C + + data structure conversion, and this process is undoubtedly all C + + developers have a headache. However, if you use C + + extensions, ADO does not need to get column information from the data provider, but instead uses the column information provided by the developer at design time. The following is a simple example:
To create a class that corresponds to a specific record
Class Cauthor:public CADORecordBinding
{
Begin_ado_binding (CCUSTOMRS1)
Ado_variable_length_entry4 (1,
adVarChar, m_szau_id, sizeof (m_szau_id), FALSE)
Ado_variable_length_entry4 (2,
Advarchar,m_szau_fname,sizeof (M_szau_fname), FALSE)
Ado_variable_length_entry4 (3,
Advarchar,m_szau_lname,sizeof (M_szau_lname), FALSE)
End_ado_binding ()
Protected
Char m_szau_id[12];
Char m_szau_fname[21];
Char m_szau_lname[41];
};
void Fetchauthordata ()
{
Cauthor author;
Recordset object
_RecordsetPtr PRs;
IADORecordBinding *piadorecordbinding;
Get COM Object interface pointer
Prs.createinstance (__uuidof (Recordset));
Get the set of records you need
Prs->open ("Select Au_id,au_fname,au_lname from Employees", "Provider=sqloledb;data source=sureshk1;database=pubs ; User Id=sa; password=; ",
adOpenForwardOnly,
adLockReadOnly,
adCmdText);
Query Interface IADORecordBinding
Prs->queryinterface (__uuidof (iadorecordbinding), (lpvoid*) &piadorecordbinding);
Bound object
Piadorecordbinding->bindtorecordset (&autho



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.