ODBC, ADO

Source: Internet
Author: User
Tags odbc ole

First, the VC access to the database technology

1. ODBC--open Database Connectivity Microsoft Open Data Interconnect, a set of APIs for accessing and manipulating databases, access to different database products, but only to relational databases. MFC encapsulates this set of API functions into an ODBC class that requires different databases to be set to an ODBC data source before use.

2, DAO-based on ODBC, has now been eliminated

3, OLE DB-based on COM technology, provides a set of interfaces for accessing and manipulating databases. You can access both relational and non-relational data, and the performance is greatly improved. The disadvantage is that learning is difficult, high demand for programmers, not popular.

4, ADO-based on OLE DB, the OLE DB is also encapsulated, providing a simple set of interfaces, and gradually become popular as one of the most widely used technology.

5, ADO-based on the. NET platform, applicable to the platform on the VB, VC, C # and other languages. is a set of classes for accessing and manipulating databases.

Ii. use of ODBC classes

1. Related Categories

CDatabase class-Provides the connection and shutdown capabilities of the database, plus the ability to execute SQL statements.

CRecordset class-Provides operations on data in a data table.

The above two classes need to include header files #include <afxdb.h>

2, the use of the steps:

2.1 Setting up an ODBC data source

Data source ODBC, management tools, control Panel

      

2.2 Using the ODBC class

Connect ODBC data source CDatabase::Open

2--Execute SQL statement cdatabase::executesql

3--Open Data Sheet CRecordset::Open

4--gets the number of fields Crecordset::getodbcfieldcount

5--gets the information for the field Crecordset::getodbcfieldinfo

6--gets the value of the field CRecordset::GetFieldValue

7--recorded pointer movement crecordset::movefirst/movelast/move/movenext/moveprevious

CRECORDSET::ISBOF//Whether to move to the beginning

Crecordset::iseof//Whether to move to the end of the

8--Closing the recordset CRecordset::Close

9--Close the data source Cdatabase::close

Understanding CRecordView-Displaying a view of records in a database

Third, use ADO to access the database

1. ado File: Msado15.dll

Location path-c:/program Files/common Files/system/ado/msado15.dll

2. Importing ADO Components

#import "Component File path" No_namespace rename ("EOF", "adoeof")

Add the above import command in the project header file StdAfx.h, compile, generate MSADO15.TLH, Msado15.tli under the debug file, these two files are similar to the wrapper class of the control.

3. When using any COM component, you must first initialize the COM library.

3.1 Initialize the COM library CoInitialize (NULL) before using the component;

3.2 After using the component, uninstall COM library couninitialize ();

4, the use of ADO components

The advantage of using C + + classes to encapsulate the interfaces of ADO components is to facilitate the use of ADO by C + + programs.

4.1 Connection interface (using Cadodatabase class encapsulation)

Functions similar to the CDatabase of ODBC classes

The Open () function to connect to the database

HRESULT Open (

_bstr_t ConnectionString,//connection string

_bstr_t UserID,//login name, or "" in the connection string where it is set

_bstr_t Password,//Login password, set this place in the connection string is ""

Long Options//connection method, direct write-1

);

Because different database "connection strings" vary, the connection string is automatically generated by creating a new *.udl file:

On the desktop new text document, rename to 1.udl-> Open the file, select the [Providers] tab, select the corresponding database driver, click "Next", enter the relevant information, test the connection, after successful, OK ; The 1.udl is reopened in Notepad, which is a visible connection string, such as:

provider=microsoft.jet.oledb.4.0;

Data Source=d:\ado.mdb;

Or

provider=microsoft.jet.oledb.4.0;

password=123; User Id=zwq;

Data Source=d:\ado.mdb;

4.2 Recordset interface (using Cadorecordset class encapsulation)

Functions similar to the CRecordset of ODBC classes

The open () function executes the SQL statement, opens the table, executes the stored procedure

HRESULT Open (

Const _variant_t & Source,//sql statement, table name, stored procedure

Const _variant_t & ActiveConnection,//Active connection

Enum CursorTypeEnum CursorType,//cursor type

Enum LockTypeEnum LockType,//Lock type

Long Options//identifies the first parameter, SQL statement-adcmdtext/table name-adcmdtable/stored procedure-adcmdstoredproc

);

Cursor Type:

Enum cursortypeenum{

adOpenForwardOnly = 0,//one-way static cursors

adOpenKeyset = 1,//Key set cursor, dynamic cursor

adOpenDynamic = 2,//dynamic cursor

adOpenStatic = 3//bidirectional static cursor

};

If the recordset is using a dynamic cursor, the recordset changes as the user modifies the table, and if the static cursor is used, the data is not changed after it is fetched from the table.

Lock type (when multiple users operate on the same database):

Enum locktypeenum{

adLockReadOnly = 1,//read-only recordset

adLockPessimistic = 2,//Pessimistic lock

adLockOptimistic = 3,//Optimistic lock

adLockBatchOptimistic = 4//Optimistic lock for batch processing mode

};

2--gets the number of fields (field) Fields->getcount ()

3--get field title Fields->getitem (nIndex)->getname ()

4--gets the value of the field Fields->getitem (NIndex)->value

5--pointer Operation Movenext/moveprevious/movefirst/movelast/move (long nnums) for recordset

IsEOF-whether to move to the end/IsBOF-whether to move to the very beginning

6--Way one: Through the record set of the way to increase, delete, change

6.1 Adding records

Take data from a data table to a recordset OpenTable ()

2--adding a new record in the Recordset AddNew ()

3--sets the value of each field in the record SetFieldValue

4--Update to database update ()

5--re-display data ShowData ()

6.2 Deleting Records

Take data from a data table to a recordset OpenTable ()

2--move the record pointer to the record you want to delete MoveLast ()

3--performing a delete operation deletes ()

4--Update to database update ()

5--re-display data ShowData ()

6.3 Modifying Records

Take data from a data table to a recordset OpenTable ()

2--move the record pointer to the record you want to modify MoveFirst ()

3--perform the Modify Operation SetFieldValue

4--Update to database update ()

5--re-display ShowData ()

7--mode Two: Add, delete, change operations by executing SQL statements

1. Connection interface Execute () function

Returns a recordset that cannot set the cursor type and lock type of a recordset by parameter, is a read-only, cursor type is a one-way static cursor, so it is commonly used to perform increment, delete, and change operations

2. Recordset interface open () function

Commonly used open () function to perform query operations, can be combined with the record set of the increase, delete, change the data table data in the increase, delete, change

5. Transaction processing

The connection interface provides the functionality of transaction processing

Start Transaction BeginTrans ()

End Transaction Endtrans (FALSE)--The transaction ends with the execution transaction CommitTrans and the rollback transaction RollbackTrans

6, how to save video, picture data in the data table?

The path to the file where the video and picture are usually saved in the database

ODBC, ADO

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.