VC uses ADO access to the database Raiders

xiaoxiao2021-03-06  77

Text 1, ADO Overview

ADO is Microsoft designed for the latest and most powerful data access example OLE DB, which is an application layer interface that is easy to use. ADO allows you to write applications to access and operate data in the database server via OLE. DB provider. The most important advantages of ADO are easy to use, fast, and less memory spending and small disk remains. ADO uses minimal network traffic in a critical application, and uses the least number of layers between the front and data sources, all of which are to provide lightweight, high-performance interfaces. The reason is called ADO, which has used a familiar metaphor, OLE automation interface.

OLE DB is a set of "Component Object Model" (COM) interfaces, a new database low-level interface, which encapsulates the function of ODBC and access data stored in different information in a unified manner. OLE DB is the technical foundation of the Microsoft UDA policy. OLE DB provides high performance access for any data source, including relationships and non-relational databases, email, and file systems, text, and graphics, custom business objects, and more. That is, OLE DB is not limited to ISAM, JET, even relational data sources, which can handle any type of data regardless of their format and storage methods. In practical applications, this diversity means accessing data residing in an Excel electronic data sheet, a text file, an email / directory service, and even a mail server, such as Microsoft Exchange. However, the purpose of the OLE DB application programming interface is to provide the best features for a variety of applications, which do not meet simplified requirements. The API you need should be a bridge that connects to applications and OLE DB, which is ActiveX Data Objects (ADO).

Second, use ADO in VC (Development steps :)

1. Introduce the ADO library file

Before using ADO, you must introduce the STDAFX.H header file to introduce the ADO library file in direct introduction symbol #import to enable the compiler to compile correctly. The code is as follows:

Introducing an ADO library file with #import

#import "c: / program files / commit files / system / ado / msado15.dll" no_namespaces rename ")

This list of statements declares that use ADO in the project, but does not use the namespace of ADO, and rename constant EOFs to AdoEOF in order to avoid constant conflicts. Now you don't need to add additional header files, you can use the ADO interface.

2, initialize the OLE / COM library environment

It must be noted that the ADO library is a set of COM dynamic libraries, which means that the application must initialize the OLE / COM library environment before calling ADO. In the MFC application, a better way is to initialize the OLE / COM library environment in the initInstance member function of the application primary class.

Bool CmyadotestApp :: InitInstance ()

{

IF (! Afxoleinit ()) // This is to initialize the COM library

{

AFXMessageBox ("OLE initialization error!");

Return False;

}

......

}

3. Introduction to the ADO interface

The ADO library contains three basic interfaces: _ConnectionPTR interface, _commandptr interface and _RecordSetPtr interface.

_ConnectionPTR interface returns a recordset or an empty pointer. It is usually used to create a data connection or perform a SQL statement that does not return any result, such as a stored procedure. Returning a recordset using the _ConnectionPTR interface is not a good method. For operations to return records, it is usually implemented with _RecordSerptr. When using _ConnectionPTR operation, you should want to get the number of records to traverse all records, and use _RecordSerptr.

_CommandPTR interface returns a recordset. It provides a simple way to perform the stored procedures and SQL statements that returns the record set. When using the _CommandPTR interface, you can use the global _connectionptr interface, or you can use the connection string directly in the _commandptr interface. If you only perform one or more data access, the latter is a better choice. But if you want to access the database frequently and return a lot of records, you should use the global _connectionptr interface to create a data connection, then use the _CommandPTR interface to perform the stored procedure and SQL statements.

_RecordSetPtr is a recordset object. Compared with the above two objects, it provides more control functions to records, such as record lock, cursor control, and the like. Like the _CommandPTR interface, it does not have to use a data connection that has been created, you can use a connection string instead of the Connection member variable assigned to _RecordSetptr, let it create data connections yourself. If you want to use multiple record sets, the best way is to use the global _connectionPTR interface that has created a data connection as the Command object.

, Then use _recordsetptr to perform stored procedures and SQL statements.

4, use the _connectionPTR interface

_ConnectionPTR is mainly a connection interface that acquires the connection with the database. Its connection string can be written directly, or you can point to an ODBC DSN. .

_ConnectionPtr PConn;

IF (PConn.createInstance ("AdoDb.Connection"))))

{

AfxMessageBox ("Create Instance Failed!");

Return;

}

CString strsrc;

strsrc = "driver = SQL Server; Server =";

STRSRC = "Suppersoft";

STRSRC = "; Database =";

strsrc = "MYDB";

STRSRC = "; UID = SA; PWD =";

CString strsql = "INSERT INTO Student (No, Name, SEX, ADDRESS) VALUES (3, 'AAA', 'Male', 'beijing')

_VARIANT_T VARSRC (strsrc);

_variant_t varsql (strsql);

_BSTR_T BSTRSRC (STRSRC);

IF (Failed (PConn-> Open (BSTRSRC, "," ", - 1))))

{

AfxMessageBox ("Can NOT Open Database!");

PConn.release ();

Return;

}

Colevariant VTOPTIONAL ((long) DISP_E_PARAMNOTFOUND, VT_ERROR);

PCONN-> EXECUTE (_BSTR_T (STRSQL), & VtOPTIONAL, -1);

PConn.release ();

AfxMessageBox ("OK!");

5, use the _recordsetPTR interface (to connect SQL Server as an example)

_RecordSetPtr PPTR;

IF (Pptr.createInstance ("AdoDb.Recordset"))))

{

AFXMessageBox ("Create Instance Failed!"); Return False;

}

CString strsrc;

strsrc = "driver = SQL Server; Server =";

STRSRC = "210.46.141.145";

STRSRC = "; Database =";

strsrc = "MYDB";

STRSRC = "; UID = SA; PWD =";

STRSRC = "SA";

CString strsql = "SELECT ID, NAME, GENDER, ADDRESS ADM PERSONAL";

_VARIANT_T VARSRC (strsrc);

_variant_t varsql (strsql);

IF (Failed (PPTR-> Open (Varsql, Varsrc, AdopenStatic, AdlockOptimistic, AdcmdText)))

{

AFXMessageBox ("Open Table Failed!");

Pptr.release ();

Return False;

}

While (! pptr-> getadoeof ())

{

_VARIANT_T VARNO;

_variant_t varname;

_variant_t varsex;

_variant_t varaddress;

Varno = pptr-> getCollect ("id");

Varname = pptr-> getCollect ("name");

VARSEX = PPTR-> getCollect ("gender");

Varaddress = pptr-> getCollect ("address");

CString Strno = (char *) _BSTR_T (Varno);

CString Strname = (char *) _BSTR_T (varName);

CString strsEx = (char *) _ BSTR_T (VARSEX);

CString straddress = (char *) _BSTR_T (VARADDRESS);

Strno.trimright ();

Strname.trimright ();

STRSEX.TRIMRIGHT ();

straddress.trimright ();

INT ncount = m_list.getItemcount ();

INT NITEM = m_list.insertitem (ncount, _t ("));

M_List.SetItemText (NITEM, 0, STRNO);

M_List.SetItemText (NITEM, 1, STRNAME);

M_List.SetItemText (NITEM, 2, STRSEX);

M_List.SetItemText (NITEM, 3, Straddress);

PPTR-> MOVENEXT ();

}

PPTR-> Close ();

Pptr.release ();

6, use _commandptr

_CommandPTR interface returns a RecordSet object, and provides more recordset control functions, the following code samples use the _CommandPTR interface:

Code 11: Get data using _commandptr

_Commandptr pCommand;

_RecordSetPtr PRS;

PCOMMAND.CREATEINSTANCE (__ uuidof (command));

PCOMMAND-> ActiveConnection = PCONN;

PCOMMAND-> CommandText = "Select * from student";

PCOMMAND-> CommandType = AdcmdText;

PCOMMAND-> Parameters-> refresh ();

PRS = PCOMMAND-> Execute (NULL, NULL, ADCMDUNKNOWN);

_VARIANT_T VARVALUE = PRS-> GetCollect ("name");

CString Strvalue = (char *) _BSTR_T (VARVALUE);

6. About data type conversion Since the COM object is a cross-platform, it uses a generic method to handle various types of data.

Therefore, the CSTRING class and COM object are incompatible, we need a set of APIs to convert COM objects and C types of data. _vatiant_t and _bstr_t are the other objects. They provide a general method to convert COM objects and C types of data.

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

New Post(0)