VC Development Database Based ADO (1)

zhaozj2021-02-16  66

VC Development Database Basite ADO Part 1, ADO Introduction ADO (ActiveX Data Object) is a new interface developed by Microsoft Database Application. It is a high-level database access technology built on OLE DB. Please don't have to worry about it, even for OLE DB, COM does not understand the ADO, because it is very easy to use, even more likely to use than the ODBC API, DAO, RDO you have contacted in the past, and does not lose flexibility. This article will detail how to use ADO to develop database applications in VC, and give sample code. Sample code

Second, the basic process is difficult to start, any new technology is the most important thing for beginners or "Getting Started", mastering its points. Let's take a look at the basic procedures developed by the ADO database! (1) Initialize the COM library, introduce the ADO library definition file (2) Connect the database (3) with the Connection object to use the established connection, execute the SQL command via the Connection, Command object, or use the Recordset object to obtain the results record set for querying, processing . (4) Turn off the connection release object after use.

Preparation: For everyone to test the examples provided herein, we use the Access database, you can also find this Test.mdb directly in the sample code we provide. Below we will introduce the above steps and give relevant code. [1] Initialization of the COM library We can use Afxoleinit () to initialize the COM library, which is usually done in the overload function of cwinapp :: initInstance (), please see the following code:

Bool Cadotest1App :: InitInstance () {AFXOLINIT (); ......

[2] To introduce the ADO type library with a #import command We add the following statement in stdafx.h: (STDAFX.H This file can you find? You can find it in the Header Files in FileView) #import "C: / Program Files / Common files / system / ado / msado15.dll "No_namespace rename" What is the role of this statement? Its ultimate role is similar to our familiar #include. When compiling, the system will generate msado15.tlh, ado15.tli two C header files to define the ADO library.

Some descriptions: (1) Msado15.dll in your environment is not necessarily in this directory, please modify (2) When compiling, it will appear as follows, and the Microsoft is in MSDN. And it is recommended that we don't pay attention to this warning. Msado15.tlh (405): Warning C4146: Unary Minus Operator Applied To Unsigned Type, Resound STILL

[3] Create a Connection object and connect the database First we need to add a pointer to the Connection object: _ConnectionPtr M_PConnection; the following code demonstrates how to create a Connection object instance and how to connect the database and perform an exception capture.

BOOL CADOTest1Dlg :: OnInitDialog () {CDialog :: OnInitDialog (); HRESULT hr; try {hr = m_pConnection.CreateInstance ( "ADODB.Connection"); /// create Connection objects if (SUCCEEDED (hr)) {hr = m_pConnection -> Open ("provider = microsoft.jet.Oledb.4.0; data source = test.mdb", ",", ", admodeunknown); //// Connect the database // The top of the connection string in the connection string is For Access2000 environments, for Access97, it is necessary to change to: provider = microsoft.jet.Oledb.3.51;}} catch (_COM_ERROR E) /// capture exception {cString ErrorMessage; errorMAGE.Format ("Connection database failed! / R / n Error Information:% S ", E.ErrorMessage (); AFXMessageBox; /// Display Error Information} In this code we are connecting the database through the open method of the Connection object, the following is the method Prototype HRESULT Connection15 :: Open (_bstr_t userid, _bstr_t password, long option "Connectionstring is a connection string, userid is a username, Password is a login password, Options is a connection option, which is used to specify the Connection object to update the data. Permissions, Options can be as follows: AdmodeunkNown: Default. The current license is not adModeRead settings: read-only adModeWrite: write only adModeReadWrite: can read and write adModeShareDenyRead: Connection object to prevent other read permissions to open a connection adModeShareDenyWrite: Connection object to prevent other write permissions to open a connection adModeShareExclusive: Connection object to prevent other open connection adModeShareDenyNone : Allow other programs or objects to establish connections in any permission

We give some commonly used ways to provide you with reference: (1) Connection to Access2000 database via Jet database engine

m_pConnection-> Open ("provider = microsoft.jet.Oledb.4.0; data source = c: //test.mdb", ",", admodeunknown);

(2) Connection to any support ODBC's database via the DSN data source: m_pConnection-> Open ("data source = adotest; UID = SA; PWD =;", ",", ",", ",", ",", ",",

(3) Connection to the SQL Server database without DSN: m_pconnection-> open ("driver = {SQL Server}; server = 127.0.0.1; database = vckbase; uid = sa; pwd = 139", "," " Admodeunknown;

Where Server is the name of the SQL server, Database is the name of the library.

In addition to the Open method, we will first introduce two useful attributes in the Connection object ConnectionTimeout and StateConnectionTimeout to set the timeout time, you need to call before Open, for example: m_pConnection-> ConnectionTIMEOUT = 5; // / Set the timeout time of 5 second m_pConnection-> Open ("Data Source = adotest;", ",", "" "" ",", ",", ",", ",", ",", Read this property to make a corresponding process, for example: if (m_pconnection-> state) m_pconnection-> close (); // If the connection has been opened, turn it off.

[4] Execute the SQL command and acquire the result record set to obtain the result record set, we define a pointer to the Recordset object: _RecordSetPtr m_precordset; and create an instance of the Recordset object for it: m_precordset.createInstance ("adodb.recordset"); SQL The execution of the command can be used in a variety of forms, and we will explain it.

(1) using the Connection object's Execute method executes the SQL command prototype Execute method is as follows: _RecordsetPtr Connection15 :: Execute (_bstr_t CommandText, VARIANT * RecordsAffected, long Options) wherein CommandText is a command string, usually SQL commands. Parameter recordsaffected is the number of rows affected after the operation is completed. The parameter Options represents the type of content in CommandText. Options can take one of the values: AdcmdText: Indications CommandText is a text command adcmdtable: indicate that CommandText is a table name AdcmdProc: indicating that CommandText is a table name AdcmdProc: Store procedure adcmdunknown: unknown

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

New Post(0)