Home > Software engineering >  MFC using ADO how to connect to PostgreSQL database query, can't use the following method to ch
MFC using ADO how to connect to PostgreSQL database query, can't use the following method to ch

Time:09-16

CoInitialize (NULL);//at the beginning of the COM component

Retrieves the hr;
Try
{
Hr=m_pConnection. CreateInstance (" ADODB. Connection ");//create a Connection object
If (SUCCEEDED (hr))
{
Hr=m_pConnection - & gt; Open (" Driver=PostgreSQL Unicode; Server=127.0.0.1; The Database=VersionManager; UID=test; The PWD=test ", ""," ", adModeUnknown);

If (FAILED (hr))
{
M_pConnection - & gt; Close ();
AfxMessageBox (" database connection failed. ");
return FALSE;
}
}
}
Catch _com_error (e)//catch exceptions
{
Cstrings errormessage;
Errormessage. Format (" connect to database failure! \ r \ n error message: % s ", e.E rrorMessage ());
AfxMessageBox (errormessage);//display an error message
}

SQL. The Format (" SELECT col_project_version FROM table_project_info WHERE col_project_name='% s' ", m_Name);
Try
{
M_pConnection - & gt; BeginTrans ();
Hr=m_pRecordset. CreateInstance (" ADODB. You ");
If (SUCCEEDED (hr))
{
Hr=m_pRecordset - & gt; Open ((_variant_t) SQL, variant_t ((IDispatch *) m_pConnection), adOpenDynamic, adLockOptimistic, adCmdText);
}
while (! M_pRecordset - & gt; AdoEOF)
{
Number=m_pRecordset - & gt; Col_project_version GetCollect (" ");//get data
SzVersion. The Format (" % s ", (LPCTSTR) _bstr_t (number));//the transformation output

M_pRecordset - & gt; MoveNext ();
}
M_pRecordset - & gt; Close ();
M_pConnection - & gt; CommitTrans ();
}
Catch _com_error (e)
{
AfxMessageBox (" connect to database failure!" );
M_pConnection - & gt; RollbackTrans ();
}
CoUninitialize ();//release com components

CodePudding user response:

The Debug step order see which function calls failed?

CodePudding user response:

Learn to debug is essential basic programming, it is recommended to use VS

CodePudding user response:

Hr=m_pRecordset - & gt; Open ((_variant_t) SQL, variant_t ((IDispatch *) m_pConnection), adOpenDynamic, adLockOptimistic, adCmdText);

Run the words, he went directly to the database connection failed. The front at the beginning of the COM and connect to the database is normal.

Catch _com_error (e)
{
AfxMessageBox (" connect to database failure!" );
M_pConnection - & gt; RollbackTrans ();
}
CoUninitialize ();//release com components

CodePudding user response:

Access, essentially, MySQL all have no problem, do not check PostgreSQL using ADO to connect to the database value...
  • Related