如何使用oledb使用相应的.dbc文件从.dbf文件读取/写入数据?

时间:2016-01-14 19:21:47

标签: c++ oledb visual-foxpro dbf

我已被指示用c ++制作一个能够从视觉狐狸专业数据库中读取数据的测试程序。我只获得了文件,但我无法从这些文件中读取数据。

我在网上发现了一些能够成功打开访问文件的代码,我能够更改它以便打开我的数据库文件。

我遇到的问题是我可以使用.dbc文件获取任何.dbf文件的列信息,但是当我尝试将rs.movenext()用于存储在行中的数据时,不会显示任何数据来自数据库文件。

整个代码使用已注释掉的连接字符串来处理访问文件。出于某种原因,当我切换到.dbf和.dbc文件时,无法显示行数据。

任何见解都会很棒。我的代码如下。

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;
}

我的输出:

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 . . .

1 个答案:

答案 0 :(得分:1)

那不是%s而不是%S(不确定我的C是否生锈)?这对我有用:

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;
}