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 to read .dbf files in VC++

ODBC to read .dbf files in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 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 use the ODBC driver "dBase Files" (installed on my station by microsoft application, I guess). 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=WD:" ... 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 again 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 with data type Thanks in advance DD

    D 1 Reply Last reply
    0
    • Q Qadddd

      Hello, I use the ODBC driver "dBase Files" (installed on my station by microsoft application, I guess). 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=WD:" ... 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 again 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 with data type Thanks in advance DD

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Qadddd wrote: 1) I am obliged go thru the ODBC display to give again the name of the file, I can't set it by program ... why ? It sounds like the "dBASE Files" DSN is not configured correctly. If it was, there would be no reason to specifiy filenames, database names, paths, etc in your code. Assuming the database exists, you can create the DSN programatically by using SQLConfigDataSource(NULL, ODBC_ADD_DSN, ...). Qadddd wrote: res = "SELECT DATUM,CASHT,NETROOM from ["+File+"]"; I don't use .dbf files, but I think that File should be the name of a table within the database. Qadddd wrote: If I add the clause "WHERE" to my request, no record is returned ... why ? What type of column is DATUM? It looks sort of like a date column, which means you use #02/08/2001# to compare against.


      A rich person is not the one who has the most, but the one that needs the least.

      Q 1 Reply Last reply
      0
      • D David Crow

        Qadddd wrote: 1) I am obliged go thru the ODBC display to give again the name of the file, I can't set it by program ... why ? It sounds like the "dBASE Files" DSN is not configured correctly. If it was, there would be no reason to specifiy filenames, database names, paths, etc in your code. Assuming the database exists, you can create the DSN programatically by using SQLConfigDataSource(NULL, ODBC_ADD_DSN, ...). Qadddd wrote: res = "SELECT DATUM,CASHT,NETROOM from ["+File+"]"; I don't use .dbf files, but I think that File should be the name of a table within the database. Qadddd wrote: If I add the clause "WHERE" to my request, no record is returned ... why ? What type of column is DATUM? It looks sort of like a date column, which means you use #02/08/2001# to compare against.


        A rich person is not the one who has the most, but the one that needs the least.

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

        DavidCrow wrote: It sounds like the "dBASE Files" DSN is not configured correctly. If it was, there would be no reason to specifiy filenames, database names, paths, etc in your code. yes, as I said myself before, I just discover on ODBC pannel the check saying that the current directory is the home directory for my file, I will go deeper in investigations tonight DavidCrow wrote: Assuming the database exists, you can create the DSN programatically by using SQLConfigDataSource(NULL, ODBC_ADD_DSN, ...). that's a good point ! because when I put my little applications on several stations, I always have to configure manually ODBC, I could do it in a little installation executable. The same, if you have a piece of advice to automate the installation of a new ODBC driver ... I take it . DavidCrow wrote: I don't use .dbf files, but I think that File should be the name of a table within the database. That's what my MySQL statement looks like, but for .dbf files ?? I looked at 2 or 3 examples using this syntax and it seems that I can access the file like this without too many problems ... 2 good reasons to keep it like that until someone tells me that my manner is totally wrong ;-)) DavidCrow wrote: It looks sort of like a date column, which means you use #02/08/2001# to compare against. It's probably the reason, I will test it ASAP Thanks 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