Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm trying to use ODBC with an Access 2000 database in a C++ MFC application.

I use the following code to SELECT an entry (which exists) in my Database:

C++
CNoeud myNode;
myNode.Open(CRecordset::dynaset, L"SELECT * FROM Noeuds where Identifiant_Noeud=5", CRecordset::none);

AfxMessageBox(L"myNode: " + myNode.m_Identifiant_Noeud);

myNode.Close();

Note:
- Identifiant_Noeud is the primary key.
- I don't open nor close the database, I only use the CNoeud object Open() and Close() methods.

But the AfxMessageBox only shows "e:" (I don't know why !).

I'm new to all this, could you explain me how to do my simple SELECT statement? I'm probably missing something obvious...
Thanks!

What I have tried:

If I wrote the following:
CNoeud myNode;

myNode.Open(CRecordset::dynaset, L"SELECT * FROM Noeuds where Identifiant_Noeud=5", CRecordset::none);

if (myNode.IsEOF()) AfxMessageBox(L"no entry");
myNode.Close();

No message box is shown, so I suppose the CNoeud object is not empty.
Posted
Updated 13-Dec-17 1:26am
v2
Comments
W Balboos, GHB 13-Dec-17 7:12am    
Connection String?

At least from what you sent, you've no connection to the database.

1 solution

The keyword is C/C++ pointer arithmetic.

The first parameter of the AfxMessageBox() function is an LPCTSTR string pointer. You are passing the address of a constant string and adds the value "5" (myNode.m_Identifiant_Noeud is as primary key probably some kind of int). So the message box shows the string starting at the 6th character (zero based 5th).

You can't concatenate strings with the + operator in C/C++.

You have to create a formatted string and show that:
C++
CString strMsg;
strMsg.Format(_T("myNode: %d"), myNode.m_Identifiant_Noeud);
AfxMessageBox(strMsg.GetString());
 
Share this answer
 
Comments
SheepSpeech 13-Dec-17 9:19am    
Thanks !
I just finished a project in C#, my mistake. But I saw this on the internet and it seems to work, I use it for my databases exceptions: AfxMessageBox(L"Database error: " + e->m_strError);
Jochen Arndt 13-Dec-17 9:30am    
There m_strError is a CString. That supports concatening strings with the + operator (but not other types like int). The above will create a temporary CString (because one operand is a CString) which is then passed using the implicit PCXSTR() operator.

Thank you for accepting my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900