Write database applications using OLEDB

zhaozj2021-02-08  256

Use Ole DB

5.1 Overview

The presence of OLE DB provides users with a unified approach to access all kinds of data sources. OLE DB can be converted in different data sources. With OLE DB, the client's developer only needs to focus on some of some details when performing data access, without having to understand a large number of different database access protocols.

OLE DB is an ActiveX interface accessing data via COM interface. This OLE DB interface is quite common enough to provide a unified means of accessing data regardless of the method used by storing data. At the same time, OLE DB also allows developers to continue to use the advantages of underlying database technology without having to remove data in order to utilize these advantages.

5.2 Using the ATL Using OLE DB Data Usage

Due to the direct use of OLE DB objects and interface design database applications need to write a lot of code. To simplify the programming, Visual C provides an ATL template for designing OLE DB data applications and data providers.

Using the ATL template can easily combine the OLE DB with MFC to simplify complex programming of the parameter query of the database. The database class provided by the MFC makes the OLE DB programming more object-oriented features. VIUAL C provides an ATL template for OLE DB can be divided into a template for data providers and data using a data usage program.

Creating a data application using an ATL template usually has the following steps:

1. Create an application framework

2, join the template class generated by ATL

3. Use the generated data access object in the application

5.3 No ATL uses OLE DB data usage

The use of the ATL template to generate data usage is simpler, but the applicability is not wide, and it cannot be dynamically adapted to changes in the database. Below we introduce to generate data usage programs directly using the MFC OLE DB class.

Template

OLE DB Data User Templates consists of some templates, including some templates, which are described below for some common classes.

1, session

CDataSource class

The CDataSource class corresponds to the data source object of the OLE DB. This class represents the connection between the OLE DB data provider and the data source. Only when the connection of the data source is established, the session object can be generated, and open the Open to open the connection of the data source.

CSession

The object created by CSession represents a session accessed separately. A data source object generated by a CDataSource class can create one or more sessions, to generate a session object on the data source object, need to call the function open () to open. At the same time, the session object can also be used to create transaction operations.

CENUMERACCESSOR class

The CenumeratorAccessor class is an accessor for information on the available data providers generated by the enumerator queries to provide currently available data providers and visible accessors.

2, visitors

CACESSOR class

Caccessor class represents the type of accessor. This class can be used when the user knows the type and structure of the database. It supports multiple accessors to a rowset, and the buffer that stores data is assigned by the user.

CDynamicAccessor class

The CDynamicAccessor class is used to create an accessor when the program is running. When the system is run, you can dynamically obtain the information of the column, and the accessor can be dynamically created according to this information.

Cmanualaccessor class

In the CmanualAccessor class, the columns are binded to the variables or the parameters to the variables when the program is running.

3, line set

Crowset class

The Crowset class encapsulates the ranked object and the corresponding interface, and provides some methods for querying, setting data, etc. You can use the MOVE () and other functions to be recorded, read data with the getData () function, update the data with INSERT (), Delete (), setData ().

CBULKROWSET class

The CBULKROWSET class is used to retrieve multiple row seductions or operate multiple rows in a call. CarrayRowset class

The CarrayRowSet class provides data access with the array subscript.

4, command class

CTABLE class

The CTABLE is used to obtain a database with a simple access to the database, and the data source is obtained, resulting in data.

CCOMMAND class

CCOMMAND class is used to support the data source of the command. You can use the open () function to perform the SQL command, or you can prepare the command before the PREPARE () function, which can improve the flexibility and robustness of the program for the data sources of the command.

In the stdafx.h header file, add the following code.

#include

EXTERN CCOMMODULE _MODULE;

#include

#include

#include // if you are for useing schema templates

In the stdafx.cpp file, add the following code.

#include

CCOMMODULE _MODULE;

Decide what type of access procedures and rows are used.

retrieve data

After opening the data source, session, and ranking objects can get data. The acquired data type depends on the access program used, may need to be bound columns. Follow the steps below.

1. Open the rowset object with the correct command.

2. If you use CManualAccessor, bind to the corresponding column before use. To bind columns, you can use functions getColumnInfo, as shown below:

// Get the column information

Ulong ulcolumns = 0;

DBColumnInfo * pcolumninfo = null;

LpoLESTR PSTRINGS = NULL;

IF (gcolumn, & pcolumninfo, & pstrings)! = S_OK)

AFXTHROWOLEDBEXCEPTION (rs.m_prowset, iid_icolumninfo);

Struct mybind * pbind = new mybind [ulcolumn];

Rs.createAccessor (ulcolumns, & pbind [0], sizeof (mybind) * ulcolumns);

For (Ulong L = 0; L

Rs.addbindentry (L 1, DBTYPE_STR, SIZEOF (TCHAR) * 40, & PBIND [L] SzValue, Null, & Pbind [l] .dwstatus);

Rs.bind ();

3, use the While cycle to take the data. In the loop, call MOVENEXT to test whether the return value of the cursor is S_OK, as shown below:

While (rs.movenext () == s_ok)

{

// Add code to fetch data here

// if you are not using an auto accessor, call getdata ()

}

4. In the While cycle, you can obtain data through different access programs.

1) If you are using the Caccessor class, you can access direct access by using their data. As follows:

2) If you are using a CDynamicAccessor or a CDynamicParameterAccessor class, you can get data by getValue or getColumn function. You can use getType to get the data type used. 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

{

Rs.getValue (i);

}

}

3) If you are using CmanualAccessor, you can specify your own data member to bind them. You can access directly. As follows:

While (rs.movenext () == s_ok)

{

// use the data members you specified in the calls to

// addbindentry.

WSPRINTF ("% s", SZFOO);

}

Decide the data type of the row

Data types are determined at runtime, with dynamic or manual access programs. If you use a manual access program, you can use the getColumnInfo function to get the column information of the rowset. From here you can get the data type.

5.4 Summary

Since there are now a variety of data sources, the only way to access these data will be implemented by some similar mechanisms, such as OLE DB. The advanced OLE DB structure is divided into two parts: customers and providers. The customer uses the data generated by the provider.

Just like other COM-based most structures, OLE DB developers need to implement a lot of interfaces, most of which are template files.

When generating a customer object, you can create a simple customer with a data source via the ATL Object Wizard. The ATL Object Wizard will check the data source and create a client agent for the database. From there, you can use the standard browsing function through the OLE DB customer template.

When generating a provider, the wizard provides a good start that only generates a simple provider to list files in a certain directory. Then, the provider template contains full complementary content supported by OLE DB. With this support, users can create OLE DB providers to implement rowset positioning strategies, data read and write, and build bookmarks.

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

New Post(0)