ADO macro in VC ++

xiaoxiao2021-03-06  38

The ADO macros in VC generally use ADO (Active Database Object) in VC for database programming. Due to the set of many parameters in function calls, it is not as simple as VB. Most parameters in actual programming are actually set, and the processing method is the same. The author encapsulates the common usage of the ADO object (XopenRecordset, XConnect, XDisconnect, Xexecute, Xcmdexecute). Simply add the following files in STDAFX.H to greatly reduce programming workload.

// File name: db.h

// ADO database macro

/ / Date: 08-10-2003

#if! defined (_db_h)

#define _db_h

#include "icrsint.h"

/ / Modify according to the actual path

#import "c: / program files / commit files / system / ado / msado15.dll" no_namespace rename ("EOF", "endoffile")

// Database connection parameter structure

Struct Database

{

CString strusername; // user's id

CString strpassword; // user's password

CString strserver; // name of database server

CString strdbname; // name of database

}

TypedEf Database * pdatabase;

// Open record set

#define xopenrecordset (/ * _ recordsetptr * / rs, / * _ connectionptr * / cn, / * _ bstr_t * / bt) /

HRESULT HR_RS = S_OK; /

HR_RS = rs.createInstance (__ uuidof (recordset)); /

Assert (succeedededed (hr_rs)); /

HR_RS = RS-> Open (BT, cn.getInterfacePtr (), AdoPENDYNAMIC, ADLOCKTIMISTIC, ADCMDTEXT); /

AskEERT (SUCCEEDED (HR_RS));

/ / Execute SQL statement

#define Xexecute (/ * _CNECTIONPTR * / CN, / * _BSTR_T * / BT) /

CN-> Execute (BT, NULL, Adexecutenorecords);

//Connect to the database

// If you are a non-SQL Server database, please modify the connection string yourself.

#define Xconnect (/ * _CNECTIONPTR * / CN, / * DATABASE * / DATABASE) /

_BSTR_T BT_CN; /

HRESULT HR_CN = S_OK; /

bt_cn = (_ bstr_t) "Provider = SQLOLEDB.1; Persist Security Info = False; User ID =" (_ bstr_t) database.strUserName (_ bstr_t) "; Password =" (_ bstr_t) database.strPassword (_ bstr_t) "; Initial Catalog = " (_ bstr_t) Database.StrDBName (_ BSTR_T)"; DATA SOURCE = " (_ bstr_t) Database.strserver; / Coinitialize (NULL); /

HR_CN = cn.createInstance (__ uuidof (connection)); /

AskEERT (ac_cn)); /

HR_CN = CN-> Open (bt_cn, _bstr_t ("), _ bstr_t (" "), admodeunknown; /

Assert (ac_cn));

// Discover the database

#define xdisconnect (/ * _ _ connectionsptr * / cn) /

CN-> Close (); /

Couninitialize ();

/ / Execute a command command

#define xcmdexecute (/ * _ commandptr * / cmd, / * _ recordsetptr * / rs, / * _ connectionptr * / cn, / * _ bSTR_T * / BT) /

HRESULT HR_CMD = S_OK; /

HR_CMD = cmd.createInstance (__ uuidof (command)); /

Assert (ac_cmd)); /

HR_cmd = rs.createInstance (__ uuidof (recordset)); /

Assert (ac_cmd)); /

CMD-> CommandText = BT; /

CMD-> CommandType = adcmdtext; /

CMD-> ActiveConnection = CN; /

RS = cmd-> Execute (NULL, NULL, AdcmdText);

// end of file

#ENDIF

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

New Post(0)