Summary of knowledge about ADO database connection

Source: Internet
Author: User
ado| Data | database | database connection

have been learning the ADO database connection these days, feeling more complex, so while learning, while using, while doing some notes, some of the following things I summarized some of the ADO database connection things, because it is notes, so not necessarily very orderly, posted out meaning on the one hand can give you a reference , while there is hope that everyone together to improve the meaning of which mistakes and deficiencies of course, I hope you can promptly notify me, in the post, I have an idea, because I use in the process, will always encounter some of the mistakes, so I think we may also encounter, so I gave a wrong summary , of course, we need everyone to improve together, if you have any experience and insights, I hope in the comments, I will promptly accept the views of everyone and add them in (of course, in the case of your permission)

1, guide the warehousing document

Before using ADO, the StdAfx.h file must be introduced into the ADO library file with the direct introduction of symbolic #import, so that the compiler can compile correctly. The code is as follows:
#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename ("EOF", "Endoffile") Rename ("BOF", " Firstoffile ")

The ADO class is defined as a resource stored in an ADO DLL (Msado15.dll), within which it is called a type library. The type library describes the autonomous interface and the COM vtable interface used by C + +. When using the #import directive, Visual C + + needs to read this type library from the ADO DLL at run time, creating a set of C + + header files. These header files have the. Tli and. TLH extensions, and readers can find the two files in the project's directory. The ADO classes that are called in C + + program code are defined in these files.
The third line of the program indicates that the ADO object does not use namespaces. In some applications, namespaces are necessary because there may be naming conflicts between objects in your application and objects in ADO. If you want to use namespaces, you can modify the third line of programs to: Rename_namespace ("Adons"). The four-line code renames the EOF (end of file) in ADO to adoeof to avoid conflicts with other libraries that define their own EOF.


2. Initializing COM environment
(1):: CoInitialize (NULL); Initializing the Ole/com library environment
:: CoUninitialize (); Since the environment is initialized, it is necessary to release him.
(2) MFC global functions can also be called
AfxOleInit ();

3, the definition and creation of three major objects instance

(1) _connectionptr pconnection ("ADODB"). Connection ");
_RecordsetPtr precordset ("ADODB"). Recordset ");
_commandptr pcommand ("Adodn.command");

(2) _connectionptr pconnection;
_RecordsetPtr precordset;
_commandptr Pcommand;

Pconnection.createinstance (__uuidof (Connection));
Precordset.createinstance (__uuidof (Recordset));
Pcommand.createinstance (__uuidof (Command));

(3) _connectionptr pconnection;
_RecordsetPtr precordset;
_commandptr Pcommand;

Pconnection.createinstance ("ADODB.") Connection ");
Precordset.createinstance ("ADODB.") Recordset ");
Pcommand.createinstance ("Adodb.command");

4. Open a connection
Pconnection->open (ConnectionString, "", "", adModeUnknown);///Connection Database

The above connection string connectionstring according to different data sources, respectively, corresponding to different writing
1) Access 2000
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=databasename; User Id=username; Password=userpassword "

2 Access ODBC data
"Provider=madasql;dsn=dsnname; Uid=username; Pwd=userpassword; "

3) access to Oracle database
"Provider=msdaora;data Sourse=servername; User Id=username; Password=userpassword; "

3) Access to MS SQL database
"Provider=sqloledb,data source=servername;initial catalog=databasename; User Id=username; Password=userpassword; "

4. Execute SQL command
SQL commands are more, but not to consider the details, here is only the common way to say
CString strsql;//defines a SQL command string that is used to save SQL statements

Strsql.format ("SQL statement");

Then, in each method that you want to use the SQL command string, type conversion using the Strsql.allocsysstring () method

5, COM's private data type

Variant, BSTR, SafeArray

The range of Variant variables includes many, using _variant_t for Management
BSTR is a string variable that is managed using _bstr_t

6, close the connection

if (m_pconnection->state)//cannot be closed multiple times, an error will occur
M_pconnection->close ();

7. Structured Exception handling
ADO encapsulates COM interfaces, so error handling is required
The following example:
HRESULT hr;
Try
{
hr = M_pconnection.createinstance ("ADODB.") Connection ");///Create Connection Object
if (SUCCEEDED (HR))
{
hr = M_pconnection->open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb", "", "", adModeUnknown);///Connection Database
The Provider in the connection string in the above sentence is aimed at the ACCESS2000 environment, for ACCESS97, it needs to be changed to: provider=microsoft.jet.oledb.3.51; }
}
catch (_com_error e)///catch exceptions
{
CString errormessage;
ErrorMessage. Format ("Connection database failure!\r\n error message:%s", E.errormessage ());
AfxMessageBox (errormessage);///Display error message
}


8. Analysis of the causes of errors
(1) Does not support interface, may be unable to insert null value

Well, I hope you can work with me to improve this summary!



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.