gpt4 book ai didi

c++ - 如何使用 oledb 使用其各自的 .dbc 文件从 .dbf 文件读取/写入数据?

转载 作者:行者123 更新时间:2023-11-28 05:53:06 30 4
gpt4 key购买 nike

我被指示用 c++ 编写一个测试程序,该程序能够从 visual fox pro 数据库中读取数据。我只获得了文件,但无法从这些文件中读取数据。

我在网上找到了一些能够成功打开访问文件的代码,并且我能够对其进行更改,以便它可以打开我的数据库文件。

我遇到的问题是我可以使用 .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 . . .

最佳答案

不会是 %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;
}

关于c++ - 如何使用 oledb 使用其各自的 .dbc 文件从 .dbf 文件读取/写入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797944/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com