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. How can I access to files with .DBF extension ?

How can I access to files with .DBF extension ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++database
14 Posts 2 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.
  • Q Offline
    Q Offline
    Qadddd
    wrote on last edited by
    #1

    Hello, I received some files (probably issued from a DBase 4 database and having a .dbf extension) and I would like to access it from a VC++ application to pick up some figures in it. How can I do that and retrieve the right column/row ? Any suggestion is welcome ... Regards, DD

    B 1 Reply Last reply
    0
    • Q Qadddd

      Hello, I received some files (probably issued from a DBase 4 database and having a .dbf extension) and I would like to access it from a VC++ application to pick up some figures in it. How can I do that and retrieve the right column/row ? Any suggestion is welcome ... Regards, DD

      B Offline
      B Offline
      BadJerry
      wrote on last edited by
      #2

      You can use ODBC. Define a DSN onto your database with a dBase driver.

      Q 1 Reply Last reply
      0
      • B BadJerry

        You can use ODBC. Define a DSN onto your database with a dBase driver.

        Q Offline
        Q Offline
        Qadddd
        wrote on last edited by
        #3

        I don't know the structure of dBase, I juste have the copy of a .dbf file (containing the field that interressed me) taken in the middle of dozen others .dbf files. With ODBC, is it possible to work on a single .dbf file, just to read some infos in it (no update to be done) without the whole dBase context or do I have to read the file in another way, like I would do it for a text file ? But, in this case, how to access the correct column/row, skip the kind of header at the beginning of the file, move from a record to another, etc ... Regards, DD

        B 1 Reply Last reply
        0
        • Q Qadddd

          I don't know the structure of dBase, I juste have the copy of a .dbf file (containing the field that interressed me) taken in the middle of dozen others .dbf files. With ODBC, is it possible to work on a single .dbf file, just to read some infos in it (no update to be done) without the whole dBase context or do I have to read the file in another way, like I would do it for a text file ? But, in this case, how to access the correct column/row, skip the kind of header at the beginning of the file, move from a record to another, etc ... Regards, DD

          B Offline
          B Offline
          BadJerry
          wrote on last edited by
          #4

          Qadddd wrote: I don't know the structure of dBase That's the beauty of ODBC... you have a common way of accessing all databases. Simply read (or write). You can select which field you want to appear, etc...

          Q 2 Replies Last reply
          0
          • B BadJerry

            Qadddd wrote: I don't know the structure of dBase That's the beauty of ODBC... you have a common way of accessing all databases. Simply read (or write). You can select which field you want to appear, etc...

            Q Offline
            Q Offline
            Qadddd
            wrote on last edited by
            #5

            yes, I agree that ODBC is not too bad for this reason and also for code compatibility with other databases. But, I would like to be sure that it allows me to access a single .dbf file without having the whole dBase context installed on my PC. By context, I mean the right access management, the DB manager itself and all these things that turn around the .dbf files... So, a .dbf file can really be read out of its dBase context via ODBC. Is the description of the table (and other needed infos) stored in the famous "header" stored at the beginning of the .dbf file and that's why ODBC can understand the content of the file ? Some other applications (i.e. Microsoft Excel) can also open these .dbf files, do they use the file header, do they use ODBC or an kind of internal interface to do the translation ? Thanks again for you answers DD

            1 Reply Last reply
            0
            • B BadJerry

              Qadddd wrote: I don't know the structure of dBase That's the beauty of ODBC... you have a common way of accessing all databases. Simply read (or write). You can select which field you want to appear, etc...

              Q Offline
              Q Offline
              Qadddd
              wrote on last edited by
              #6

              Hello, I use the ODBC driver "dBase Files" installed by microsoft. hereunder is the code I wrote to access the C:\Documents and Settings\dd\Mes documents\GSTAT.DBF file : #define DBName "DSN=dBASE Files;UID=;PWD:" ... CDatabase gDB; CRecordset rs(&gDB); CString res; CString ErrMsg; CString CASHT; CString NETROOM; CString DATUM; int NbRecord; CString Path = "C:\\Documents and Settings\\dd\\Mes documents\\" ; CString File = "GSTAT.DBF" ; ErrMsg = ""; res = "SELECT DATUM,CASHT,NETROOM from ["+File+"]"; // res = "SELECT DATUM,CASHT,NETROOM from ["+File+"] WHERE (DATUM = 20010208)"; // res = "SELECT DATUM,CASHT,NETROOM from ["+File+"] WHERE (DATUM = '20010208')"; // gDB.OpenEx(DBName,CDatabase::noOdbcDialog); gDB.OpenEx(DBName,CDatabase::forceOdbcDialog ); TRY { rs.Open( CRecordset::forwardOnly, res ); NbRecord = rs.GetRecordCount(); } CATCH(CDBException, e) { ErrMsg ="**ERR: "+e->m_strError.Left(70); } END_CATCH if (ErrMsg == "") { while(!rs.IsEOF()) { rs.GetFieldValue( "DATUM", DATUM); rs.GetFieldValue( "CASHT", CASHT); rs.GetFieldValue( "NETROOM", NETROOM); rs.MoveNext(); } } rs.Close(); gDB.Close(); Used like this, it works but I have some problems : 1) I am obliged go thru the ODBC display to give the name of the file, I can't set it by program ... why ? How can I do this and use the noOdbcDialog option (When I try to use it, ODBC tells me that it can't find GSTAT.DBF)? Furthermore, ODBC dialog is based on program directory, not on the path. If I add the Path to the name in File variable, ODBC tells me that it can't find C:\Documents and Settings\dd\Mes documents\GSTAT.DBF ... 2) If I add the clause "WHERE" to my request, no record is returned ... why ? Can't I use it ? Am I obliged to read all records and test the DATUM field by program ? 3) what is the format of the date in the file ? Excel says 08/02/2001, Word says 20010208 and DATUM = 2001-02-06 when debugging my code ... When I use a date in format yyyy-mm-dd or dd/mm/yyyy ODBC tells me that my format is not compatible Thanks in advance DD

              B 1 Reply Last reply
              0
              • Q Qadddd

                Hello, I use the ODBC driver "dBase Files" installed by microsoft. hereunder is the code I wrote to access the C:\Documents and Settings\dd\Mes documents\GSTAT.DBF file : #define DBName "DSN=dBASE Files;UID=;PWD:" ... CDatabase gDB; CRecordset rs(&gDB); CString res; CString ErrMsg; CString CASHT; CString NETROOM; CString DATUM; int NbRecord; CString Path = "C:\\Documents and Settings\\dd\\Mes documents\\" ; CString File = "GSTAT.DBF" ; ErrMsg = ""; res = "SELECT DATUM,CASHT,NETROOM from ["+File+"]"; // res = "SELECT DATUM,CASHT,NETROOM from ["+File+"] WHERE (DATUM = 20010208)"; // res = "SELECT DATUM,CASHT,NETROOM from ["+File+"] WHERE (DATUM = '20010208')"; // gDB.OpenEx(DBName,CDatabase::noOdbcDialog); gDB.OpenEx(DBName,CDatabase::forceOdbcDialog ); TRY { rs.Open( CRecordset::forwardOnly, res ); NbRecord = rs.GetRecordCount(); } CATCH(CDBException, e) { ErrMsg ="**ERR: "+e->m_strError.Left(70); } END_CATCH if (ErrMsg == "") { while(!rs.IsEOF()) { rs.GetFieldValue( "DATUM", DATUM); rs.GetFieldValue( "CASHT", CASHT); rs.GetFieldValue( "NETROOM", NETROOM); rs.MoveNext(); } } rs.Close(); gDB.Close(); Used like this, it works but I have some problems : 1) I am obliged go thru the ODBC display to give the name of the file, I can't set it by program ... why ? How can I do this and use the noOdbcDialog option (When I try to use it, ODBC tells me that it can't find GSTAT.DBF)? Furthermore, ODBC dialog is based on program directory, not on the path. If I add the Path to the name in File variable, ODBC tells me that it can't find C:\Documents and Settings\dd\Mes documents\GSTAT.DBF ... 2) If I add the clause "WHERE" to my request, no record is returned ... why ? Can't I use it ? Am I obliged to read all records and test the DATUM field by program ? 3) what is the format of the date in the file ? Excel says 08/02/2001, Word says 20010208 and DATUM = 2001-02-06 when debugging my code ... When I use a date in format yyyy-mm-dd or dd/mm/yyyy ODBC tells me that my format is not compatible Thanks in advance DD

                B Offline
                B Offline
                BadJerry
                wrote on last edited by
                #7

                I think you are almost there! 1) the database problem When you create your system DSN, you will give it a name (ex dsnMyDatabase). This name will indicate the DBF file name and the driever you are using. Then you will redefine #define DBName "DSN=dsnMyDatabase;UID=WD;" It should do the trick 2)and 3) The Where should work it is probably a problem due to the way dates are defined try this WHERE DATNUM = #mm/dd/yyyy# Good luck

                Q 1 Reply Last reply
                0
                • B BadJerry

                  I think you are almost there! 1) the database problem When you create your system DSN, you will give it a name (ex dsnMyDatabase). This name will indicate the DBF file name and the driever you are using. Then you will redefine #define DBName "DSN=dsnMyDatabase;UID=WD;" It should do the trick 2)and 3) The Where should work it is probably a problem due to the way dates are defined try this WHERE DATNUM = #mm/dd/yyyy# Good luck

                  Q Offline
                  Q Offline
                  Qadddd
                  wrote on last edited by
                  #8

                  hello, sorry to bug you again with this stuff, but I need some extra-infos (in fact, I don't use PC database stuff very much (except MySql on which I crated a little application also using ODBC and I am quite a newbie in VC++ ) 1) #define DBName "DSN=dsnMyDatabase;UID=WD;" Ok I was looking for the pannel where I set the access to the database (path, file) during the ODBC configuration and did not find it ... but looking at it a second time, there is a "Use Current directory" set, and if I deselect it, I can precise the path/file ... So the way id to Create 1 DSN per file in ODBC and done the "gDB.OpenEx(DBName,CDatabase::noOdbcDialog);" with the correct one. But, in ODBC, should I just give the path and then precise the file in "OpenValue" in the "rs.Open( CRecordset::forwardOnly, OpenValue );" command, or should I give the path and the file name, in this case should I repeat the file name ine the "OpenValue" or not ? 2) Yes, it should work. ok, I will try the WHERE format you proposed me. Thanks again DD

                  B 1 Reply Last reply
                  0
                  • Q Qadddd

                    hello, sorry to bug you again with this stuff, but I need some extra-infos (in fact, I don't use PC database stuff very much (except MySql on which I crated a little application also using ODBC and I am quite a newbie in VC++ ) 1) #define DBName "DSN=dsnMyDatabase;UID=WD;" Ok I was looking for the pannel where I set the access to the database (path, file) during the ODBC configuration and did not find it ... but looking at it a second time, there is a "Use Current directory" set, and if I deselect it, I can precise the path/file ... So the way id to Create 1 DSN per file in ODBC and done the "gDB.OpenEx(DBName,CDatabase::noOdbcDialog);" with the correct one. But, in ODBC, should I just give the path and then precise the file in "OpenValue" in the "rs.Open( CRecordset::forwardOnly, OpenValue );" command, or should I give the path and the file name, in this case should I repeat the file name ine the "OpenValue" or not ? 2) Yes, it should work. ok, I will try the WHERE format you proposed me. Thanks again DD

                    B Offline
                    B Offline
                    BadJerry
                    wrote on last edited by
                    #9

                    Salut (did not realise you were French too), Ok when you create your DSN. 1) You go into System DSN 2) Click Add 3) Select dBase, click Finish 4) Enter the Data Source Name (dsnMyDatabase) And select your database (after having unclicked use current directory) Then in ODBC, you do not need to repeat the path of your file, simply the name of the DSN. Do I make sense? Bonne chance!

                    Q 1 Reply Last reply
                    0
                    • B BadJerry

                      Salut (did not realise you were French too), Ok when you create your DSN. 1) You go into System DSN 2) Click Add 3) Select dBase, click Finish 4) Enter the Data Source Name (dsnMyDatabase) And select your database (after having unclicked use current directory) Then in ODBC, you do not need to repeat the path of your file, simply the name of the DSN. Do I make sense? Bonne chance!

                      Q Offline
                      Q Offline
                      Qadddd
                      wrote on last edited by
                      #10

                      re hello ! BadJerry wrote: did not realise you were French too my perfect english probably ... ;-))) BadJerry wrote: Then in ODBC, you do not need to repeat the path of your file, simply the name of the DSN? I aggree ! but I for step 4 : A) the DSN should be set to the directory containing the file.dbf and the I specify the name of the file.dbf in the rs.Open( CRecordset::forwardOnly, SQLCmd )(what I suppose) or B) the DSN should be set to the directory+file.dbf and then, I have no clue on what SQLCmd has to be in the rs.Open( CRecordset::forwardOnly, SQLCmd) ? DD Et merci pour le Bonne chance, je n'ai pas fini de souffrir avec ce que que je suis en train de faire ...

                      B 1 Reply Last reply
                      0
                      • Q Qadddd

                        re hello ! BadJerry wrote: did not realise you were French too my perfect english probably ... ;-))) BadJerry wrote: Then in ODBC, you do not need to repeat the path of your file, simply the name of the DSN? I aggree ! but I for step 4 : A) the DSN should be set to the directory containing the file.dbf and the I specify the name of the file.dbf in the rs.Open( CRecordset::forwardOnly, SQLCmd )(what I suppose) or B) the DSN should be set to the directory+file.dbf and then, I have no clue on what SQLCmd has to be in the rs.Open( CRecordset::forwardOnly, SQLCmd) ? DD Et merci pour le Bonne chance, je n'ai pas fini de souffrir avec ce que que je suis en train de faire ...

                        B Offline
                        B Offline
                        BadJerry
                        wrote on last edited by
                        #11

                        What you need to do is find the name of your recordset and the name of your fields as they are defined in your DSN. The easiest way would be to start MS Access and try to import the data from your DSN (Fichier/Donnees externes in an existing mdb). This way you will get the name of your table and then you can have something like "Select * from MyTable" BTW I suspect the table name is the name of the dbf file... If this works and its for a one off, leave as it is. If you plan to make this a released application, consider using ADO though... There is a good set of classes on CP for ADO. Une fois que tu auras fait ca une fois, tu verras, ca n'a rien de sorcier! A+!

                        Q 1 Reply Last reply
                        0
                        • B BadJerry

                          What you need to do is find the name of your recordset and the name of your fields as they are defined in your DSN. The easiest way would be to start MS Access and try to import the data from your DSN (Fichier/Donnees externes in an existing mdb). This way you will get the name of your table and then you can have something like "Select * from MyTable" BTW I suspect the table name is the name of the dbf file... If this works and its for a one off, leave as it is. If you plan to make this a released application, consider using ADO though... There is a good set of classes on CP for ADO. Une fois que tu auras fait ca une fois, tu verras, ca n'a rien de sorcier! A+!

                          Q Offline
                          Q Offline
                          Qadddd
                          wrote on last edited by
                          #12

                          BadJerry wrote: "Select * from MyTable" BTW I suspect the table name is the name of the dbf file... ok, this pleases me !!! BadJerry wrote: consider using ADO though... I am using these files just to pick some basic informations in, no delete, no update, no complicated SQL stuff. The dBase application producing these files will soon desappear to be replaced by something using Oracle ... So I will keep it like that. But just for my info : what is the advantage of CDaoDatabase compared to CDatabase class ? The example I looked before was using CDaoDatabase, I took the code and didn't see a lot of benefits using this class. Further more I need to import a lot of code to make it even compile, the CDaoDatabase isn't known while you just add #include "afxdb.h" for Cdatabase ... BadJerry wrote: Une fois que tu auras fait ca une fois, tu verras, ca n'a rien de sorcier! oui, c'est la premiere fois que ca fait mal ... A la prochaine DD

                          B 1 Reply Last reply
                          0
                          • Q Qadddd

                            BadJerry wrote: "Select * from MyTable" BTW I suspect the table name is the name of the dbf file... ok, this pleases me !!! BadJerry wrote: consider using ADO though... I am using these files just to pick some basic informations in, no delete, no update, no complicated SQL stuff. The dBase application producing these files will soon desappear to be replaced by something using Oracle ... So I will keep it like that. But just for my info : what is the advantage of CDaoDatabase compared to CDatabase class ? The example I looked before was using CDaoDatabase, I took the code and didn't see a lot of benefits using this class. Further more I need to import a lot of code to make it even compile, the CDaoDatabase isn't known while you just add #include "afxdb.h" for Cdatabase ... BadJerry wrote: Une fois que tu auras fait ca une fois, tu verras, ca n'a rien de sorcier! oui, c'est la premiere fois que ca fait mal ... A la prochaine DD

                            B Offline
                            B Offline
                            BadJerry
                            wrote on last edited by
                            #13

                            DAO is really for Access databases really... and it is obsolete. I still use it when I need to play with an Access database because it is blinding fast. ODBC was the MS first attempt at having a universal way to access databases. ADO is like ODBC but meant to be much faster (it actually is) So in your case, do not bother with CDaoDatabase or CDaoRecordset Qadddd wrote: oui, c'est la premiere fois que ca fait mal ... Tiens j'ai deja entendu ca mais c'etait pas sur le meme type de forum ;P

                            Q 1 Reply Last reply
                            0
                            • B BadJerry

                              DAO is really for Access databases really... and it is obsolete. I still use it when I need to play with an Access database because it is blinding fast. ODBC was the MS first attempt at having a universal way to access databases. ADO is like ODBC but meant to be much faster (it actually is) So in your case, do not bother with CDaoDatabase or CDaoRecordset Qadddd wrote: oui, c'est la premiere fois que ca fait mal ... Tiens j'ai deja entendu ca mais c'etait pas sur le meme type de forum ;P

                              Q Offline
                              Q Offline
                              Qadddd
                              wrote on last edited by
                              #14

                              Hello, my code seems to work correctly, but I have some problems : 1) SELECT in the files returns me no record but an error message: "Impossible de trouver le fichier memo xBase Demande", in english it can be traduced by "impossible to find the requested xBase memo file" .... If I open the file with Excel and launch my appli accessing the file, all records are correctly selected and returned ... Strange I know, but true. Simple, to test my appli and until I have a better solution, I just open all accessed files with Excel at the same time ... Any idea on the cause of this problem and its solution? 2) I thought I had a too old version of my ODBC drivers, so I wanted to upgrade them ... With microsoft, it is never easy to find anything, so what should I upgrade to upgrade my ODBC drivers ? Seems to be MSJet engine ... So I downloaded the lastest MSJet4.0 sp8, but when I want to install it, it refuses to do it and tells me that I should already have at least the sp3 installed to install this sp8 ... The problem is that, when I verify manually the version of the dll, the version corresponds to a sp5 ... So I don't know what to do to perform my installation ... 3) ODBC and MDAC ... are both linked ? if I upgrade ODBC, should I also upgrade MDAC ? and if yes, which version of MDAC should I install (I have the MDAC 2.5 sp2 installed at the moment)? 4) in one of my dbf table, I have to make a selection on an ID to retrieve a record. This ID seems to be numeric but must not be declared as a standard numeric or even text value (I am thinking to an auto incremented value or style like that) because, in my clause WHERE, when I don't use the quotes, ODBC send me a error message saying that my format is not correct, and when I use the quotes, I don't receive the error, but no record is returned (even if my file is opened with Excel ...) is the any other way to write my request ? Sorry to bug you again with this but I am a little bit stuck Regards DD

                              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