Search code examples
c++oledbvisual-foxprodbf

How do you read/write data from a .dbf file using its respective .dbc file using oledb?


Ive been instructed to make a test program in c++ that is able to read data from a visual fox pro database. Ive only been given the files and I am having trouble reading the data from these files.

I found some code online that was able to successfully open access files and i was able to change it so that it would open my database files.

The problem I am having is that I can get the column information of any .dbf file using the .dbc file, but when i try to use rs.movenext() to the data stored in the rows, no data is displayed from the database file.

The code as a whole works for access files using the connection string that is commented out. For some reason when i switch over to .dbf and .dbc files, the row data is unable to be displayed.

Any insight would be awesome. My code is below.

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

CDataSource ds;
CSession ss;
CCommand<CDynamicAccessor> rs;
DBTYPE dbt;
int i;


if (ds.OpenFromInitializationString(_T("Provider=vfpoledb.1;Data Source=C:\\Users\\xx\\xx\\xx\\appdata.dbc;Collating Sequence=general;"))) {
    //if (ds.OpenFromInitializationString(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Testdata.mdb"))) {
    printf("CDataSource Error\n");
    return 1;
}



if (ss.Open(ds)) {
    printf("CSession Error\n");
    return 1;
}
if (rs.Open(ss, _T("Select * From AppReg01.dbf"))) {
    printf("CCommand Error\n");
    return 1;
}  
int count = 0;
for (i = 1; i <= (int)rs.GetColumnCount(); i++) {
    rs.GetColumnType(i, &dbt);
    printf("Column number = %d, Column name = %S, Column type = %d\n", i, rs.GetColumnName(i), dbt);


}


while (!rs.MoveNext()) {
    printf_s("blah: %S, %S \n \n", rs.GetValue(1), rs.GetValue(2)); \
    count++;
}
rs.Close();
ss.Close();
ds.Close();



CoUninitialize();
return 0;
}

My output:

Column number = 1, Column name = kname, Column type = 128 
Column number = 2, Column name = lname, Column type = 129
Column number = 3, Column name = key, Column type = 128
Column number = 4, Column name = rtype, Column type = 131
Column number = 5, Column name = type, Column type = 131
Column number = 6, Column name = access, Column type = 131
Column number = 7, Column name = shortvalue, Column type = 128
Column number = 8, Column name = value, Column type = 13
Column number = 9, Column name = props, Column type = 13
Column number = 10, Column name = comment, Column type = 13
blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah:    
 blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: 
 blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah:
 blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: 
blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah:
 blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah: blah:
 Press any key to continue . . .

Solution

  • Wouldn't that be %s and not %S (not sure really my C is rusty)? This works for me:

    int _tmain(int argc, _TCHAR* argv[])
    {
    CoInitialize(NULL);
    
    CDataSource ds;
    CSession ss;
    CCommand<CDynamicAccessor> rs;
    DBTYPE dbt;
    int i;
    
    
    if (ds.OpenFromInitializationString(_T("Provider=vfpoledb;Data Source=C:\\Program Files (x86)\\Microsoft Visual FoxPro 9\\Samples\\Northwind\\northwind.dbc;"))) {
    //    if (ds.OpenFromInitializationString(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\data\\Northwind.mdb"))) {
        printf("CDataSource Error\n");
        return 1;
    }
    
    
    
    if (ss.Open(ds)) {
        printf("CSession Error\n");
        return 1;
    }
    if (rs.Open(ss, _T("Select * From Customers"))) {
        printf("CCommand Error\n");
        return 1;
    }  
    int count = 0;
    for (i = 1; i <= (int)rs.GetColumnCount(); i++) {
        rs.GetColumnType(i, &dbt);
        printf("Column number = %d, Column name = %S, Column type = %d\n", i, rs.GetColumnName(i), dbt);
    
    
    }
    
    while (!rs.MoveNext()) {
        printf_s("blah: %s, %s \n \n", rs.GetValue(1), rs.GetValue(2) ); 
        count++;
    }
    rs.Close();
    ss.Close();
    ds.Close();
    
    
    
    CoUninitialize();
    return 0;
    }