ODBC读取超过访问和excel记录的末尾 [英] ODBC reading past the end of record in access and excel

查看:66
本文介绍了ODBC读取超过访问和excel记录的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在拥有ODBC代码,可以成功打开和读取Access和Excel文件中的数据。

问题是,在这两种情况下,ODBC都会继续读取超过记录的末尾,返回垃圾值。结果,该过程挂起,我无法超越第一条记录。

任何帮助都将不胜感激。



我尝试过:



在Visual Studio中尝试调试。尝试在线帮助。

解决方案

该错误可能在评论中发布的代码中:

 RETCODE rc; // ODBC返回码
SQLRETURN GetColData(int colnum,string& str)
{
SQLCHAR buf [255] = {0};
if((SQLGetData(StmtHandle,colnum,SQL_CHAR,buf,sizeof(buf),NULL))== SQL_SUCCESS)
str = reinterpret_cast< char *>(buf);
返回rc;
}

该函数返回(unitialised)gobal变量 rc 的内容,该函数在函数中从不更改。



当全局变量为零时,在返回SQL_SUCCESS时调用该函数的循环将永远不会结束。



使用使用函数返回值赋值的局部变量:

 //删除它以确保仅使用局部变量! 
// RETCODE rc; // ODBC返回码
SQLRETURN GetColData(int colnum,string& str)
{
SQLCHAR buf [255] = {0};
RETCODE rc = SQLGetData(StmtHandle,colnum,SQL_CHAR,buf,sizeof(buf),NULL);
if(rc == SQL_SUCCESS)
str = buf;
返回rc;
}

也不需要投射 buf ,因为 SQLCHAR


I now have ODBC code which successfully opens and reads data from both Access and Excel files.
The problem is, in both cases, ODBC keeps on "reading" past the end of record, returning junk values. As a result, the process hangs and I am unable to progress beyond the first record.
Any help would be appreciated.

What I have tried:

Tried debugging in Visual Studio. Tried online help.

解决方案

The error is probably in the code posted as comment:

RETCODE rc; // ODBC return code
SQLRETURN GetColData(int colnum, string& str)
{
    SQLCHAR buf[255] = { 0 };
    if ((SQLGetData(StmtHandle, colnum, SQL_CHAR, buf, sizeof(buf), NULL)) == SQL_SUCCESS)
        str = reinterpret_cast<char*>(buf);
    return rc;
}

That functions returns the content of the (unitialised) gobal variable rc which is never changed within the function.

When the global variable is zero, your loop calling that function while it returns SQL_SUCCESS will never end.

Use a local variable that is assigned with the function return value:

// Delete this to ensure that only local variables are used!
//RETCODE rc; // ODBC return code
SQLRETURN GetColData(int colnum, string& str)
{
    SQLCHAR buf[255] = { 0 };
    RETCODE rc = SQLGetData(StmtHandle, colnum, SQL_CHAR, buf, sizeof(buf), NULL);
    if (rc == SQL_SUCCESS)
        str = buf;
    return rc;
}

There should be also no need to cast buf because SQLCHAR is char.


这篇关于ODBC读取超过访问和excel记录的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆