Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ODBC, SQL and MFC

ODBC, SQL and MFC

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestionc++help
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MFC is the Best

    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 mfc

    A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #2

    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 the CRecordset 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 to CDBException object. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

    M 1 Reply Last reply
    0
    • A Alexandru Savescu

      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 the CRecordset 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 to CDBException object. Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      M Offline
      M Offline
      MFC is the Best
      wrote on last edited by
      #3

      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

      A 1 Reply Last reply
      0
      • M MFC is the Best

        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

        A Offline
        A Offline
        Alexandru Savescu
        wrote on last edited by
        #4

        afxdb is a standard header, so you should use angle brackets:

        #include

        Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

        M 1 Reply Last reply
        0
        • A Alexandru Savescu

          afxdb is a standard header, so you should use angle brackets:

          #include

          Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

          M Offline
          M Offline
          MFC is the Best
          wrote on last edited by
          #5

          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

          A 1 Reply Last reply
          0
          • M MFC is the Best

            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

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #6

            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!

            M 1 Reply Last reply
            0
            • A Alexandru Savescu

              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!

              M Offline
              M Offline
              MFC is the Best
              wrote on last edited by
              #7

              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

              A 1 Reply Last reply
              0
              • M MFC is the Best

                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

                A Offline
                A Offline
                Alexandru Savescu
                wrote on last edited by
                #8

                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 the CDatabase 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!

                M 1 Reply Last reply
                0
                • A Alexandru Savescu

                  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 the CDatabase 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!

                  M Offline
                  M Offline
                  MFC is the Best
                  wrote on last edited by
                  #9

                  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 from CRecordset? Because i ever use CRecordset....:~ thank you very much :rose: mfc

                  A 1 Reply Last reply
                  0
                  • M MFC is the Best

                    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 from CRecordset? Because i ever use CRecordset....:~ thank you very much :rose: mfc

                    A Offline
                    A Offline
                    Alexandru Savescu
                    wrote on last edited by
                    #10

                    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!

                    M 1 Reply Last reply
                    0
                    • M MFC is the Best

                      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 mfc

                      T Offline
                      T Offline
                      Tony Fontenot
                      wrote on last edited by
                      #11

                      Let 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 ***********************

                      1 Reply Last reply
                      0
                      • A Alexandru Savescu

                        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!

                        M Offline
                        M Offline
                        MFC is the Best
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups