These days have been learning ADO database connections, which feel more complicated, so they learn, while doing, do some notes, below, some things I have summed up about ADO database connection, because it is notes, so I don't necessarily Very orderly, the meaning of the stickers, I can give you a reference. I also hope that everyone will improve together, and the mistakes and deficiencies, I hope everyone can inform me in time, when I came, I have An idea, because I am in the process of use, I always encounter some Ming's wonderful mistakes, so I think everyone will meet, so I gave an error summary, of course, you need everyone here. Let's be complete, if you have any experiences and insights, I hope to leave a message in the comments, I will accept everyone's opinion and add them in time (of course, it is allowed to get your permission)
1, import library file
Before using ADO, you must introduce the ADO library file with the direct introduction symbol #import with the direct introduction symbol #import so that the compiler can be compiled 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 definition of the ADO class is to be stored in ADO DLL (MSADO15.DLL) as a resource in its inside. The type library describes the autonomous interface, and the COM VTable interface used by C . When using the #import command, Visual C needs to be read from the ADO DLL when running, and create a set of C header files. These header files have .TLI and .TLH extensions, readers can find these two files in the project's directory. The ADO class called in the C program code is defined in these files. The third line of the program indicates that the ADO object does not use the namespace. In some applications, naming conflicts may occur due to objects in the application in the application, there is a need to use the namespace. If you want to use a namespace, you can modify the third line program to: Rename_NameSpace ("Adons"). The fourth line of code is renamed the EOF in the ADO (end) to AdoEOF to avoid conflicts with other library defined for your EOF.
2, initialize the COM environment (1) :: Coinitialize (null); // Initialize OLE / COM library environment :: Couninitialize (); // Since initializing the environment, there is of course necessary to release him (2) can also call MFC Global Function Afxoleinit ();
3, the definition of the three major objects and the creation of instances
(1) _ConnectionPtr PConnection; _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 (", admodeunknown); /////-Connecting the connection string Connectionstring on the database, according to the different data sources, respectively correspond to the Access 2000" Provider, respectively. "Provider = Microsoft.jet.Oledb.4.0; data source = database; user id = username; password = userpassword
2) data access ODBC "Provider = MADASQL; DSN = dsnName; UID = userName; PWD = userPassword;" 3) access the Oracle database "Provider = MSDAORA; Data Sourse = serverName; User ID = userName; Password = userPassword;"
3) Access MS SQL Database "Provider = SQLOLEDB, DATA SOURCE = ServerName; Initial Catalog = DatabaseName; user ID = username; password = userpassword;"
4. Execute the SQL command SQL command is more, but don't consider the details, here only say the general method cstring strsql; // Define the SQL command string to save the SQL statement
STRSQL.FORMAT ("SQL Statement");
Then use strsql.allocsystring () to make type conversion using strsql.allocsystring in methods to use the SQL command string.
5, COM's dedicated data type Variant, BSTR, SAFEARRAY VARIANT variable ranges, using _variant_t to manage BSTR is a string variable, use _bstr_t to manage 6, turn off connection if (m_pconnection-> state) // Can't close multiple times, otherwise error M_PConnection-> Close ();
7. Structured abnormality processing ADO encapsulates the COM interface, so error handling is required: 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 ///-above The provider in the string is for the Access2000 environment. For Access 97, it needs to be changed to: provider = microsoft.jet.Oledb.3.51;}} catch (_ERROR E) /// Capture anomalous {cString ErrorMessage; errorMAGE.Format ("Connect Database Failure! / R / n error message:% s ", E.ErrorMessage ()); afxMessageBox (ErrorMessage); /// Display Error message} 8, analysis of error causes (1) Do not support interface, maybe not insert empty value
Ok, I hope everyone can improve this summary with me!