Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access the database with ADO in VC

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how VC uses ADO to access the database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Overview of ADO

ADO is designed by Microsoft for the latest and most powerful data access paradigm OLE DB, and is an easy-to-use application layer interface. ADO enables you to write applications through OLE. The DB provider accesses and manipulates data in the database server. The main advantages of ADO are easy to use, high speed, low memory cost and small disk legacy. ADO uses the least amount of network traffic in key applications and the least number of layers between the front end and the data source, all of which are designed to provide lightweight, high-performance interfaces. The reason why it is called ADO is that it uses a familiar metaphor, the OLE automation interface.

OLE DB is a group of "component object Model" (COM) interfaces, which is a new low-level database interface. It encapsulates the functions of ODBC and accesses data stored in different information sources in a unified way. OLE DB is the technical foundation of Microsoft UDA (Universal Data Access) strategy. OLE DB provides high-performance access to any data source, including relational and non-relational databases, e-mail and file systems, text and graphics, custom business objects, and more. In other words, OLE DB is not limited to ISAM, Jet, or even relational data sources, it can handle any type of data, regardless of their format and storage methods. In practical applications, this diversity means that you can access the

Excel spreadsheets, text files, e-mail / directory services and even mail servers, such as data in Microsoft Exchange. However, the purpose of the OLE DB API is to provide the best functionality for a variety of applications, and it does not meet the requirements of simplicity. The API you need should be a bridge between the application and the OLE DB, which is ActiveX Data Objects (ADO).

2. Use ADO in VC (the development steps are as follows:)

1. Introduce ADO library file

Before using ADO, you must import the ADO library file with the direct import symbol # import in the stdafx.h header file of the project, so that the compiler can compile correctly. The code is as follows:

Introduce ADO library files with # import

# import "c:\ program files\ common files\ system\ ado\ msado15.dll" no_namespaces rename ("EOF" adoEOF ")

This statement declares that ADO is used in the project, but not the namespace of ADO, and to avoid constant conflicts, rename the constant EOF to adoEOF. Now you don't need to add another header file to use the ADO interface.

2. Initialize the OLE/COM library environment

It is important to note 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 MFC applications, a better way is to initialize the OLE/COM library environment in the InitInstance member function of the main class of the application.

BOOL CMyAdoTestApp::InitInstance ()

{

If (! AfxOleInit ()) / / this is the initialization of the COM library

{

AfxMessageBox ("OLE initialization error!")

Return FALSE

}

……

}

3. Brief introduction of ADO interface

The ADO library contains three basic interfaces: the _ ConnectionPtr interface, the _ CommandPtr interface, and the _ RecordsetPtr interface.

The _ ConnectionPtr interface returns a recordset or a null pointer. It is typically used to create a data connection or to execute a SQL statement that returns no results, such as a stored procedure. It is not a good use to return a recordset using the _ ConnectionPtr interface. The operation to return the record is usually implemented with _ RecordserPtr. In order to get the number of records, you have to traverse all records with the _ ConnectionPtr operation, but not with _ RecordserPtr.

The _ CommandPtr interface returns a recordset. It provides a simple way to execute stored procedures and SQL statements that return a recordset. When using the _ CommandPtr interface, you can take advantage of 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 operations, the latter is a better choice. But if you want to access the database frequently and return many recordsets, you should use the global _ ConnectionPtr interface to create a data connection, and then use the _ CommandPtr interface to execute stored procedures and SQL statements.

_ RecordsetPtr is a recordset object. Compared with the above two objects, it provides more control functions to the recordset, such as record locking, cursor control and so on. Like the _ CommandPtr interface, it does not have to use a data connection that has been created. Instead of assigning a connection pointer to the connection member variable of _ RecordsetPtr, you can use a connection string to create its own data connection. If you are using multiple recordsets, the best way is to use the global _ ConnectionPtr interface that has created a data connection like the Command object

And then use _ RecordsetPtr to execute stored procedures and SQL statements.

4. Use the _ ConnectionPtr API

_ ConnectionPtr is mainly a connection interface to obtain a connection to the database. Its connection string can be written directly or it can point to an ODBC DSN.

_ ConnectionPtr pConn

If (FAILED (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 (3MagneAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

_ 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 (take connecting to SQL Server as an example)

_ RecordsetPtr pPtr

If (FAILED (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 from 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 the _ CommandPtr API

The _ CommandPtr interface returns a Recordset object and provides more recordset control functions. The following code illustrates how to use the _ CommandPtr interface:

Code: use the _ CommandPtr interface to get data

_ 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)

7, about data type conversion because the COM object is cross-platform, it uses a general method to deal with various types of data, so the Cstring class and COM objects are not compatible, we need a set of API to convert COM objects and C++ type data. _ vatiant_t and _ bstr_t are two such objects. They provide general methods for converting data of COM objects and C++ types.

This is the end of the content of "how VC uses ADO to access the database". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report