Normally, a database application that is given ADO, for example, uses the data from the following procedure data source
(1) Create a Connection object. Defines the connection string information. It contains the data source name, user ID, password, connection timeout, the location of the default database, and the cursor. One Connection data
One session of the source. The ability to control transactions through Connection objects, that is, to run BeginTrans, CommitTrans, and RollbackTrans methods.
(2) Open the data source. Establish a connection to the data source.
(3) executes an SQL command. Once the connection is successful, the query can be executed. The ability to execute queries asynchronously, as well as to process query results asynchronously, will notify the provider to provide data behind the scenes.
This allows the application to
Continue to deal with other things without waiting.
(4) Use the result set. After the query is complete, the result set can be used by the application. Under a different cursor type. Ability to browse and alter row data on the client or server side.
(5) Terminate the connection. The connection to the data source can be destroyed when all data operations have been completed.
We follow the above steps to analyze:
1, first in the StdAfx.h header file first import Msado.dll file
#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoeof")
Because we need to manipulate the data in the database file, here we first define the three smart pointers to use in C**app.h
_connectionptr: A pointer to the Connect object of ADO.
_RecordsetPtr: A pointer to the Recordset object for ADO.
_commandptr: A pointer to the Command object for ADO.
ADO Objects Use flow descriptive narration
the ADO library is a set of COM dynamic libraries. This means that before the application calls ADO, it initializes the Ole/com library environment, which can be initialized in the InitInstance () function in the C**app.cpp file.
Then define the HR object. As the return value of the function, theHRESULT return value describes the assumption that the function performs normally. Returns S_OK, at the same time the actual result of the function is returned by a parameter pointer.
If an exception is encountered, the COM system is inferred. The corresponding error value is returned.
Once the m_pconnection is instantiated, we can define the information used for the connection character, which we can use. UDL file to get the connection string
For more information, create a new. txt file, and then change the suffix name to. udl
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcmvuewh1aq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcmvuewh1aq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Then we open our file in Notepad, and the last line
PROVIDER=SQLOLEDB.1; Persist Security Info=false; User Id=sa; < Span style= "COLOR: #ff0000" >password=****; initial catalog= student performance management system; Data Source=hui \ \sql
Note that there is no password to join, otherwise the program will error, the following assumes the \ escape character. Note that we should add a \. The grammar of the C language. ( The red part does not have )
Of course we can also use SQL Native Client 11.0, and later can also log in as Windows. Note When Windows identity is logged on. No need to join password!
2. Open the data source and establish a connection with the data source at the same time
Open method: Use the Open method to establish a physical connection to the data source.
The open function is specifically defined:
HRESULT Open (_bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options)
Significance of the parameters:
ConnectionString: Optional, type string, including connection information, assuming that the ConnectionString property is set, the parameter can not be set
UserID: Optional. String, including the username used when establishing the connection
Password: Optional, String. Includes the password used at the time of establishment.
Option: Optional, determines whether the method is returned after the connection is established (asynchronous) or before the connection is established (synchronous), which can be such as the following two constants: Adconnectionunsepecified (default, synchronous) and Adasyncconnect (asynchronous)
Close method: Used to close the connection to the data source, after the access to the database is complete, close the connection. Frees the associated system resources. Closing an object doesn't mean deleting it from memory, changing its property settings and opening it again
BOOL Clibraryapp::initinstance () {:: CoInitialize (NULL); Initialize the ole/com library environment HRESULT hr = NULL;TRY{HR = M_pconnection.createinstance (_uuidof (Connection));//Create a Connection object instance if (SUCCEEDED ( HR) {m_pconnection->connectionstring = ("Provider = sqloledb.1; Persist Security Info = False; User ID = sa; Password=renhui; Initial Catalog = student performance management system; Data Source = Hui\\sql ");//(" provider=sqlncli11.1;integrated security=sspi; Persist Security Info=false; User id= ""; Initial catalog= student performance management system; Data source=hui\\sql;initial File name= ""; Server spn= ""), hr = M_pconnection->open ("", "", "", adconnectunspecified);//Open database if (FAILED (HR)) {AfxMessageBox (_t ("Open failed!")); return FALSE;}} Else{afxmessagebox (_t ("create instance of connection failed!")); return FALSE;}} catch (_com_error e) {CString temp;temp. Format ("Database connection error \ r \ n Error message:%s", E.errormessage ()); AfxMessageBox (temp); return FALSE;} ... ...}
At this point, our database is successfully connected to the
We have encountered any database connectivity issues, but also welcome to communicate with me, I hope we can progress together. (but limited, because of the level, you may not be able to answer)
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
vs2013 ADO Contact SQL server2012 Database