Vc ++ Database Programming

Source: Internet
Author: User
Tags dsn
Welcome to the CC ++ programming Community Forum and interact with 3 million of technical staff. Open databaseconnectivity is Microsoft's open service architecture (WOSA, WindowsOpenServicesArchitecture) it establishes a set of specifications and provides a set

Welcome to the C/C ++ programming community forum, and interact with 3 million technical staff> Open Database Connectivity is Microsoft's Open service structure (WOSA, windows Open Services Architecture) is a component of the database. It establishes a set of specifications and provides a set

Welcome to the C/C ++ programming Community Forum and interact with 3 million technical staff> enter

ODBC Open Database Connectivity is an integral part of Microsoft's WOSA (Windows Open Services Architecture) Database. It establishes a set of specifications, it also provides a set of standard APIs for database access (application programming interfaces ). These APIs use SQL to complete most of their tasks. ODBC also provides support for the SQL language. You can directly send SQL statements to ODBC.

ADO (ActiveX Data Objects) is a COM component used by Microsoft to access Data sources. It provides an intermediate layer for programming languages and Unified Data Access to ole db. Allow developers to write data access code without having to worry about how the database is implemented, rather than the connection to the database. When accessing a database, the knowledge about SQL is not necessary, but the SQL commands supported by a specific database can still be executed through the command objects in ADO. ADO was designed to inherit from Microsoft's early Data Access Object layers, including RDO (Remote Data Objects) and DAO (Data Access Objects ).

Use the # import method to operate ADO

In # import, you need to provide the path and name of the included Type Library. It can automatically generate a definition of GUIDs and encapsulate ADO objects.

You can also list the types it can find in the Type Library. For any type library you reference, VC ++ will automatically generate two files during compilation:

A header file (. tlh) that contains the enumerated types and definitions of objects in the Type Library.

An implementation file (. tli) encapsulates methods in the object model of the Type Library.

Example

# Import "c: \ Program Files \ common files \ system \ ado \ msado15.dll" no_namespace rename ("EOF", "adoEOF ")

/* VC ++ automatically generates two files, msado15.tlh and msado15.tli. No_namespace means that you do not need to reference the namespace when initializing the variable. It is necessary to name EOF, because a typical VC ++ application has defined EOF as the constant-1 */

CoInitialize (NULL );

/* CoInitialize is an API function provided by Windows to tell Windows to create a com object in a single thread mode. The parameter is retained and must be NULL. CoInitialize does not load the COM library. It is only used to initialize the suite used by the current thread. After using this function, the thread establishes a correspondence with a suite, and the thread runs in this suite. CoInitialize and CoUninitialize must be used in pairs. */

_ ConnectionPtr m_pConnection (_ uuidof (Connection); // use a smart pointer to generate a Connection pointer

_ RecordsetPtr m_pRecordset (_ uuidof (Recordset); // use a smart pointer to generate a record set pointer

Try {

M_pConnection-> Open ("DSN = Student", "", "", 0); // create a connection. DSN (Data Source Name) is the Name of the ODBC Data Source to be connected.

M_pRecordset = m_pConnection-> Execute ("select * from Student", NULL, ad1_text); // Execute the query statement

While (! M_pRecordset-> adoEOF)

{

_ Variant_t TheValue; // _ variant_t encapsulate and manage the VARIANT data type. It is the data type used in COM, and COM is the Component Object Model (Component Object Model)

TheValue = m_pRecordset-> GetCollect ("Sname"); // obtain the value of the field "Sname" in the table.

M_pRecordset-> MoveNext (); // move to the next record

}

} Catch (_ com_error e)

{

AfxMessageBox (e. ErrorMessage ());

}

M_pRecordset-> Close ();

M_pConnection-> Close ();

M_pRecordset = NULL;

M_pConnection = NULL;

CoUninitialize ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.