Source code: Refer to My resources "VC Access Postgre Database" The most commonly used database access technology has the following several 1. odbc--Open database interconnection ODBC provides a unified programming interface for the use of different relational databases. After you install a different database, you need to create an ODBC data source, 2. dao--data Access object 3. rdo--Remote Data Object 4. OLE db--object connection with embedded database 5. Ado--activex Data Objects Here I explain the two methods I use: ODBC and ADO. 1. odbc to create an ODBC data source in the installation database, use CDatabase and CRecordset two MFC classes to establish a connection to the database and access the database. 1.1 condition contains header file: #include <afxdb.h > Declaring member variables: CDatabase m_dbpostgre; 1.2 Connection Database int nretval; nretval = M_dbpostgre.openex (_t ("dsn=postgresql30w; Uid=postgre; Pwd=postgre "), cdatabase::openreadonly | Cdatabase::noodbcdialog); if (nretval) { AfxMessageBox ("Connect database successful!") _t } 1.3 Access Data CRecordset recordset (&m_dbpostgre); CString strSQL = _t ("SELECT * from Product"); recordset. Open (CRecordset::forwardOnly, strSQL, crecordset::readonly); cdbvariant var; WhilE (!recordset. IsEOF ()) { Tag_productinfo Tagproductinfo; //ID recordset. GetFieldValue (_t ("ID"), Var); Tagproductinfo.nproductid = var.m_ Ival; //Name recordset. GetFieldValue (_t ("Name"), tagproductinfo.strproductname); M_vecproductinfo.push_back (Tagproductinfo); recordset. MoveNext (); } recordset. Close (); 2. ADO Technology ActiveX Data object. ADO based on OLE DB, the use of ADO technology to access the database, the actual call process is: ADO client through ADO access to OLE DB provided by the program, so that the access speed will be slower. If a relational database does not have an OLE DB provider, you can use ODBC's OLE DB provider to access ODBC and then use ODBC to access the ODBC-enabled database. 2.1 conditions l header files and dynamic library loading #include <comdef.h> #include <atlbase.h> #pragma warning ( disable:4146) #import "C:/Program files/common files/system/ado/msado15.dll" named_ guids rename ("EOF", "adoeof"), rename ("BOF", "Adobof") #pragma warning ( default:4146) using namespace ADODB; l COM Component Loading if (CoInitialize (NULL)!= 0) { AfxMessageBox ("Initialize COM library failed!") (_t); return FALSE; } l Variable declaration _connectionptr m_pconnection; 2.2 Connection Database m_pconnection.createinstance (__uuidof (Connection)); m_pconnection->connectionstring = _t ("dsn=postgresql30w; Uid=postgre; Pwd=postgre "); HRESULT hr = M_pconnection->open (_t (""), _t (""), _t (""), adconnectunspecified ); if (SUCCEEDED (HR)) { & nbsp; AfxMessageBox ("Connect database successful!"); } 2.3 Access database _commandptr pcommand (Command); _recordsetptr precordset (__uuidof (Recordset)); CString strSQL = _t ("SELECT * From/" pg_getproductinfo/"()"); try { pcommand->activeconnection=m_pconnection; pcommand->commandtext=_bstr_t (strSQL); Precordset=pcommand->execute ( Null,null,adcmdtext); } &NBSP;&NBSP;&NBsp; catch (_com_error & E) { AfxMessageBox (E.description ()); return; } _variant_t var; int nrecordnum = Precordset->getrecordcount (); while (!precordset->getadoeof ()) { Tag_productinfo Tagproductinfo; //ID var = precordset->getcollect (_t ("ID")); tagproductinfo.nproductid= Var.intVal; //Name var = precordset->getcollect (_t ("Name")); Tagproductinfo.strproductname = ( char*) _bstr_t (VAR); M_vecproductinfo.push_back (Tagproductinfo); Precordset->movenext (); } precordset->close (); precordset.release (); Summary: now DAO and RDO these two technologies have been used very little, OLE DB and ADO are relatively new technologies, OLE DB is very powerful, But his support for automation is not very good. To better support Automation, Microsoft in OLE Based on DB, ADO has been developed to facilitate scripting languages such as VBScript, and languages such as vb,delphi to use ADO to Access databases easily. The above two technologies are the same for different relational databases, with different data sources, names, passwords, and how SQL statements are invoked. For example: To invoke a stored procedure as an example SQLServer2000 invocation method is "{Call Sp_addmember ('%s ', '%s ',%d)}", if not with arguments, there is no parentheses, and if you have arguments, the string's arguments need to be marked with single quotes. The Postgre invocation method is "_t" ("Select/" pg_modifyoneproduct/"(%d, '%s ')", "/" is an escape character, because if the stored procedure name is prefixed with double quotes when the stored procedure is defined, at the time of the call, Be sure to add double quotes, which is what SQL should be. There is also a stored procedure that wants to return all the records in the table, then use the selece * from stored procedure name when invoking the stored procedure. summed up so much, you are welcome to criticize correct.
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.