Writing database applications using OLE DB

Source: Internet
Author: User
Tags ole prepare
Using OLE DB
5. 1 overview
The presence of OLE DB provides a unified way for users to access all different kinds of data sources. OLE DB can be converted in a different data source. With Ole DB, a client developer needs to focus on a few details when making data access without having to understand the access protocols of a large number of different databases.
OLE DB is an ActiveX interface that accesses data through a COM interface. This OLE DB interface is quite generic enough to provide a uniform means of accessing data, regardless of the method used to store the data. At the same time, OLE DB allows developers to continue leveraging the benefits of the underlying database technology without having to move the data out to take advantage of these benefits.
5. 2 using ATL with OLE DB data use programs
Designing a database application with objects and interfaces that use OLE DB directly requires writing a large amount of code. To simplify programming, Visual C + + provides an ATL template for designing OLE DB data applications and data providers.
Using an ATL template makes it easy to combine OLE DB with MFC to simplify complex programming such as database parameter queries. MFC provides database classes that make OLE DB programming more object-oriented. The ATL templates that Viual C + + provides for OLE DB can be divided into templates for data providers and templates for data-use programs.
Creating a data application using an ATL template typically has the following steps:
1. Create Application Framework
2, join the ATL generated template class
3, the use of the resulting data access objects in the application

5. 3 Use of OLE DB data using the program without ATL
Using ATL template to produce data use program is simpler, but its applicability is not wide and can not adapt to the change of database dynamically. Here we introduce the use of MFC OLE DB classes directly to generate data usage.
Use of templates
The OLE DB data consumer templates are made up of templates, including the following templates, which are described below for some common classes.
1. Conversation class
CDataSource class
The CDataSource class corresponds to the data source object of OLE DB. This class represents the connection between the OLE DB data provider and the data source. The session object can be generated only after the connection to the data source has been established, and the Open is invoked to open the connection to the data source.
CSession class
The object created by CSession represents a separate database-access session. A data source object generated with the CDataSource class can create one or more sessions, and to generate a session object on the data source object, you need to invoke the function open () to open it. Also, session objects can be used to create transaction operations.
CEnumeratorAccessor class
The CEnumeratorAccessor class is an accessor used to access information about the available data providers in the rowset generated by the enumerator query, providing the currently available data provider and the visible accessors.
2, Accessor class
Cacessor class
The CAccessor class represents the type of the accessor. This class can be used when the user knows the type and structure of the database. It supports multiple accessors for a rowset, and the buffer that holds the data is assigned by the user.
CDynamicAccessor class
The CDynamicAccessor class is used to dynamically create accessors while the program is running. When the system is running, the column information can be dynamically obtained from the rowset, and the accessors can be created dynamically based on this information.
CManualAccessor class
CManualAccessor class to bind a column to a variable when the program is run, or to bundle arguments with a variable.
3. Row Set class
CRowset class
The CRowset class encapsulates rowset objects and corresponding interfaces, and provides methods for querying, setting data, and so on. You can use functions such as Move () to record movement, read data with the GetData () function, and update the data with insert (), Delete (), SetData ().
CBulkRowset class
The CBulkRowset class is used to fetch multiple row handles in one call or to manipulate multiple rows.
CArrayRowset class
The CArrayRowset class provides data access using an array subscript.
4. Command class
CTable class
The CTable class is used for simple access to the database, with the name of the data source to obtain the rowset, thus obtaining the data.
CCommand class
The CCommand class is used to support a command's data source. You can use the open () function to execute SQL commands, or you can prepare () functions to prepare the commands first, and you can increase the flexibility and robustness of the program for the data source that supports the command.
In the StdAfx.h header file, add the following code.
#include <atlbase.h>
extern CComModule _module;
#include <atlcom.h>
#include <atldbcli.h>
#include <atldbsch.h>//If you are using schema templates
In the Stdafx.cpp file, add the following code.
#include <atlimpl.cpp>
CComModule _module;
Determines what type of accessor and rowset to use.
Get Data
After you open the data source, the session, the rowset object, you can get the data. The data type you get depends on the access program you are using, and you may need to bind the columns. Follow these steps.
1. Open the rowset object with the correct command.
2. If you use CManualAccessor, bind to the corresponding column before using it. To bind a column, you can use the function GetColumnInfo, as follows:
Get the column information
ULONG ulcolumns = 0;
dbcolumninfo* pcolumninfo = NULL;
Lpolestr pstrings = NULL;
if (Rs. GetColumnInfo (&ulcolumns, &pcolumninfo, &pstrings)!= S_OK)
Afxthrowoledbexception (Rs.m_prowset, iid_icolumnsinfo);
struct mybind* pbind = new Mybind[ulcolumns];
Rs. CreateAccessor (Ulcolumns, &pbind[0], sizeof (mybind) *ulcolumns);
for (ULONG l=0; l<ulcolumns; l++)
Rs. AddBindEntry (l+1, Dbtype_str, sizeof (TCHAR) *40, &pbind[l].szvalue, NULL, &pbind[l].dwstatus);
Rs. Bind ();
3, use while loop to fetch data. In the loop, call MoveNext to test whether the cursor's return value is S_OK, as follows:
while (Rs. MoveNext () = = S_OK)
{
ADD code to fetch data here
If you are is not using a auto accessor, call Rs. GetData ()
}
4. Within the while loop, data can be obtained through different access programs.
1 If you are using the CAccessor class, you can access them directly by using their data members. As shown below:
2 If you are using a CDynamicAccessor or CDynamicParameterAccessor class, you can get the data by GetValue or GetColumn function. You can use GetType to get the data type you are using. As shown below:
while (Rs. MoveNext () = = S_OK)
{
Use the dynamic accessor functions to retrieve your
Data
ULONG ulcolumns = Rs. getColumnCount ();
for (ULONG i=0; i<ulcolumns; i++)
{
Rs. GetValue (i);
}
}
3 If you are using CManualAccessor, you can specify your own data members and bind them. can be accessed directly. As shown below:
while (Rs. MoveNext () = = S_OK)
{
Use the ' data members ' specified in the calls to
AddBindEntry.
wsprintf ("%s", Szfoo);
}
Determining the data type of a rowset
Determines the data type at run time, using a dynamic or manual access program. If you are using a manual access program, you can use the GetColumnInfo function to get column information for the rowset. Data types can be obtained from here.
5. 4 Summary
Since there are a variety of data sources, the only way to manage this data is through some of the same mechanisms, such as OLE DB. The Advanced OLE DB architecture is divided into two parts: the customer and the provider. The customer uses data generated by the provider.
Like most other COM-based architectures, OLE DB developers need to implement many interfaces, most of which are template files.
When you build a client object, you can create a simple customer by pointing to a data source through the ATL Object Wizard. The ATL Object Wizard examines the data source and creates a client proxy for the database. From there, you can use the standard browsing functions through the OLE DB client templates.
When you build a provider, the wizard provides a good start by simply generating a simple provider to enumerate the files in a directory. The provider template then contains the full complement of OLE DB support. With this support, users can create OLE DB providers to implement rowset positioning policies, read and write data, and create bookmarks.

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.