ODBC, SQL and MFC
-
hello, i have an mfc program and an access database. i connected them with odbc. how can i make the following sql statement in my mfc programm? select distinct xy from mytable i want to read out 'xy' from a table 'mytable' and put these CString into a ComboBox: Now i tried this:
CMyDatabase db; db.Open( CRecordset::dynaset, _T( "Select distinct xy from mytable" ) ); db.MoveFirst(); while(!db.IsEOF()) { m_ctrlCombo.AddString(db.m_xy); db.MoveNext(); } db.Close(); UpdateData(false);
i get the error, when i want to use this funktion. the error: error with the call of a data record what´s wrong?????:~ thanks mfcYou need to follow these steps: 1. Open the database (using most likely a DSN, or a connection string). 2. Create a
CRecordset
object that uses the above open datastring. 3. Open theCRecordse
t object with a SQL query ("SELECT disintct...") 4. Once the recordset is open, iterate through recordset via MoveNext, MoveFirst etc. Also, is wise to include everything in a try/catch block and you will most likely catch a pointer toCDBException
object. Best regards, Alexandru Savescu P.S. Interested in art? Visit this! -
You need to follow these steps: 1. Open the database (using most likely a DSN, or a connection string). 2. Create a
CRecordset
object that uses the above open datastring. 3. Open theCRecordse
t object with a SQL query ("SELECT disintct...") 4. Once the recordset is open, iterate through recordset via MoveNext, MoveFirst etc. Also, is wise to include everything in a try/catch block and you will most likely catch a pointer toCDBException
object. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!thanks for reply. i tried your idea:
CMyDatabase db; db.Open(); CRecordset rs (&db);
but i get an error. must i include something (i included#include "afxdb.h"
)? thanks mfc -
thanks for reply. i tried your idea:
CMyDatabase db; db.Open(); CRecordset rs (&db);
but i get an error. must i include something (i included#include "afxdb.h"
)? thanks mfcafxdb is a standard header, so you should use angle brackets:
#include
Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
afxdb is a standard header, so you should use angle brackets:
#include
Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
yes, i use angle brackets (< >).....but it does not work. why? can you help me???? the error is here
CRecordset rs(&db);
the program has problems with &db why? mfc -
yes, i use angle brackets (< >).....but it does not work. why? can you help me???? the error is here
CRecordset rs(&db);
the program has problems with &db why? mfcPlease tell us what your error is. Apparently afxdb is enough standard to include. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
Please tell us what your error is. Apparently afxdb is enough standard to include. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
okay...first the code:
CMyDatabase db; db.Open(); CRecordset rs (&db);
the error: 'CRecordset::CRecordset':Conversion parameter 1 from 'class CMyDatabase *' in 'class CDatabase *' not possible. what´s wrong? thanks mfc -
okay...first the code:
CMyDatabase db; db.Open(); CRecordset rs (&db);
the error: 'CRecordset::CRecordset':Conversion parameter 1 from 'class CMyDatabase *' in 'class CDatabase *' not possible. what´s wrong? thanks mfcHere's what's going on: 1. It has nothing to do with any header file. 2. Why do you use a
CMyDatabase
object? Are you overriding some functionality of theCDatabase
class? 3. If you are using a custom database object (CMyDatabase) make sure it is derived from CDatabase. Voila! Best regards, Alexandru Savescu P.S. Interested in art? Visit this! -
Here's what's going on: 1. It has nothing to do with any header file. 2. Why do you use a
CMyDatabase
object? Are you overriding some functionality of theCDatabase
class? 3. If you are using a custom database object (CMyDatabase) make sure it is derived from CDatabase. Voila! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!i understand. my database object is derived from
CRecordset
. isn´t there a possiblity to make the sql statement: select distinct xy from mytable with a database object fromCRecordset
? Because i ever useCRecordset
....:~ thank you very much :rose: mfc -
i understand. my database object is derived from
CRecordset
. isn´t there a possiblity to make the sql statement: select distinct xy from mytable with a database object fromCRecordset
? Because i ever useCRecordset
....:~ thank you very much :rose: mfcNo. You are making a fundamental confustion. A database consists of tables and when you retrieve a record you read data from one/or more table(s) from the database. MFC ODBC classes are: CDatabase that wrapps around a database and CRecordset the wrapps around a table (or more tables). If you want to make a SELECT DISTINCT xy FROM MyTable you have to create a database object that opens the database MyTable table is in, then you create a CRecordset object that takes a pointer to a CDatabase and you pass the SQL statement to that CRecordset (or CRecordset-derived) object. Then you can navigate via MoveNext, MoveLast etc. through the database. You can, however, execut SQL commands to the database objects. These are generally DELETE or INSERT statements (but not SELECT):
CDatabase db;
db.Open (...);
CString MyDelStr = _T ("DELETE FROM MyTable");
db.ExecuteSQL (MyDelStr);CString MyInsertStr = _T ("INSERT INTO MyTable Values.....");
db.ExecuteSQL (MyInsertStr);I hope I've been helpful! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
-
hello, i have an mfc program and an access database. i connected them with odbc. how can i make the following sql statement in my mfc programm? select distinct xy from mytable i want to read out 'xy' from a table 'mytable' and put these CString into a ComboBox: Now i tried this:
CMyDatabase db; db.Open( CRecordset::dynaset, _T( "Select distinct xy from mytable" ) ); db.MoveFirst(); while(!db.IsEOF()) { m_ctrlCombo.AddString(db.m_xy); db.MoveNext(); } db.Close(); UpdateData(false);
i get the error, when i want to use this funktion. the error: error with the call of a data record what´s wrong?????:~ thanks mfcLet me also suggest CODBCRecordset. This class encapsulates CRecordset and makes things much more accesiable. IMO that is. Cheers *********************** Tony Fontenot Recreational Solutions tony@recsolutions.com ***********************
-
No. You are making a fundamental confustion. A database consists of tables and when you retrieve a record you read data from one/or more table(s) from the database. MFC ODBC classes are: CDatabase that wrapps around a database and CRecordset the wrapps around a table (or more tables). If you want to make a SELECT DISTINCT xy FROM MyTable you have to create a database object that opens the database MyTable table is in, then you create a CRecordset object that takes a pointer to a CDatabase and you pass the SQL statement to that CRecordset (or CRecordset-derived) object. Then you can navigate via MoveNext, MoveLast etc. through the database. You can, however, execut SQL commands to the database objects. These are generally DELETE or INSERT statements (but not SELECT):
CDatabase db;
db.Open (...);
CString MyDelStr = _T ("DELETE FROM MyTable");
db.ExecuteSQL (MyDelStr);CString MyInsertStr = _T ("INSERT INTO MyTable Values.....");
db.ExecuteSQL (MyInsertStr);I hope I've been helpful! Best regards, Alexandru Savescu P.S. Interested in art? Visit this!
thank you very very much.....:rose::rose::rose: i tried your idea....
CDatabase db; db.Open(_T( "Datenbank" ), FALSE,FALSE, _T( "ODBC")); CRecordset rs(&db); rs.Open(CRecordset::forwardOnly ,"Select distinct xy from Mytable");
i connectet access and vc++ with ODBC. the ODBC driver from my database i namend: 'Datenbank'. in my database i have different tables and one is called xy. in my code (above) must be a mistake....it doesnt work...why? :~ how can i get the strings which i read out of my database table? thank you. mfc