Common methods for adjusting ADO in VC ++

zhaozj2021-02-16  55

Common methods for adjusting ADO in VC

Overview

For programmers that write database programs on Windows, ActiveX Data Objects (ADO) is the most commonly used technology, which can make database connections and data access via ADO. However, when using ADO in VC , since it is called using COM, some systems often have problems that cannot be compiled, or use the program illegal errors, and I want to introduce the common method of calling ADO in VC ..

1. Import the COM file Msado15.dll with Import into ADO

E.g:

#import "c: / program files / compon files / system / ado / msado15.dll" /

NO_NAMESPACE

2, the initialization of COM use

HRESULT COMINIT ()

{

HRESULT HR = S_OK; / / Default return value

IF failed (coinitialize) // COM initialization call

{

Couninitialize ();

HR = e_unexpected;

}

Return HR;

}

3, establish a database connection

HRESULT ConnectToDB (LPSTR PUSERID, // User Name

LPSTR PCONNSTRING, // Connecting strings

LPSTR PUSERPASSWORD, // User Password

ConnectOptionNum ConnectOption) // Connection parameters

{

HRESULT HR = S_OK; / / Default return value

_ConnectionPtr PtrConn; / / Define Connection Objects

Try

{

// Create a connection entity

HR = ptrconn.createInstance (__ uuidof (connection));

/ / Set the maximum number of seconds waiting to wait, default is 15 seconds

PtrConn-> ConnectionTimeout = 20

// Open connection

HR = Ptrconn-> Open (PConnString,

PUSERID,

PUSERPASSWORD,

ConnectOption;

Return HR;

}

Catch (_COM_ERROR & PCOMERROR)

{

... // Error handling

Return E_UNEXPECTED;

}

}

4. Execute a SQL query to get a data set (Recordset)

_RecordSetPtr getRecordset (LPSTR strsql, _connectionptr ptronn)

{

Try

{

RecordSetPtr Ptrrs; // Recordset object

// Create a RecordSet object entity

PTRRS.CREATEINSTANCE (__ uuidof (recordset);

Ptrrs-> Open (strsql,

PtrConn.GetInterfacePtr (),

AdopenForwardonly,

AdlockunSpecified,

AdcmdText);

or

PTRRS = PTRCONN -> EXECUTE (M_ strsql, null, adcmdtext);

Return PTRRS;

}

Catch (_COM_ERROR & A_PCOMERROR)

{

....// Error handling

Return NULL;

}

}

5. Get column name HRESULT GETCOLUMNNAMES by the data set

_RecordSetPtr Ptrrs, // Recordset object

CHAR STRCOLNAMES [] [255],

DatatyPeenum iColtypes [])

{

Try

{// parameter variable

_VARIANT_T L_VAINDEX;

l_vaindex.vt = vt_i2;

// columns total

Long LCOLCOUNT;

LCOLCOUNT = PTRRS -> Fields-> Count;

// Cycle the properties and names of the column

For (int IINDEX = 0; IIndex

{

l_vaindex.ival = iINDex; // Setting the loop index

/ / Get a field name

Sprintf (StrColnames [IIndex], "% S",

(Lpstr) PTRRS -> Fields-> GetItem (l_vaindex) -> name);

// get field properties

Icoltypes = Ptrrs -> Fields-> GetItem (l_vaindex) -> Type;

}

}

Return S_OK;

}

Catch (_COM_ERROR & A_PCOMERROR)

{

.... // Error handling

Return E_UNEXPECTED;

}

Catch (...)

{

.... // Error handling

Return E_UNEXPECTED;

}

}

6. Get the current line record through the data set (RECORDSET)

HRESULT GETONERECORD

_RecordSetPtr PTRRS,

Const long lnofcolumns,

_variant_t varvalue [])

{

Try

{

// Parameter variable

_VARIANT_T L_VAINDEX;

l_vaindex.vt = vt_i2;

// loop to get the value of the column

For (long lindex = 0; lindex

{

l_vaindex.ival = lindex;

// Take a field value

VARVALUE [LINDEX] =

Ptrrs-> Fields-> GetItem (l_vaindex) -> value;

}

Return S_OK;

}

Catch (_COM_ERROR & A_PCOMERROR)

{

.... // Error handling

Return E_UNEXPECTED;

}

Catch (...)

{

.... // Error handling

Return E_UNEXPECTED;

}

}

7. Error information

Void Errorfunc (_COM_ERROR & PCOMERROR, _CONNECTIONPTR PTRCONN);

{

// COM error

// When performing a COM function, if an error, you can capture the abnormality of _COM_ERROR.

Char lpcomerrorstr512];

Sprintf (lpcomerroSTR512,

"ErrorCode =% 08LX /

Error Message =% S /

Source =% s /

Description =% s ",

Pcomerror.Error (), // error number

pcomerror.ErrorMessage (), // error message

(Lpcstr) pcomerror.source (), // Error source (LPCSTR) pcomerror.description ()); // Error Description

// We can see the above code, and you can get all the error of COM in the _COM_ERROR object.

// ADO error

Errorptr Perr = NULL;

IF ((ptrconn -> errors-> count)> 0)

{

Long ncount = ptrconn -> errors-> count;

For (long i = 0; i

{

Perr = a_pconnptr-> errors-> GetItem (i);

Char l_pcherrorstring [512];

Sprintf (l_pcherrorstring, "error: / n error number:% x / t% s",

Perr-> number, // error number

Perr-> description); // Error Description

}

}

// ADO handle, there is a record in the Connection object, you can access

// Connection object acquires error numbers and error messages.

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

New Post(0)