c++ - ADO Recordset Field Value to C++ vector/array(捕获指针值)

标签 c++ ado data-conversion

我正在尝试通过 Visual C++ (express) 查询 SQL Server (Express) 并将生成的数据集存储到 C++ vector 中(数组也很好)。为此,我研究了 ADO 库并在 MSDN 上找到了大量帮助。简而言之,引用 msado15.dll 库并使用这些功能(尤其是 ADO 记录绑定(bind),它需要 icrsint.h)。简而言之,我已经能够查询数据库并使用 printf() 显示字段值;但是当我尝试将字段值加载到 vector 中时遇到了麻烦。

我最初尝试通过将所有内容转换为 char* 来加载值(由于多次类型转换错误后绝望),结果发现最终结果是一个指针 vector ,它们都指向相同的内存地址。接下来(这是下面提供的代码)我试图分配内存位置的值,但最终只得到内存位置第一个字符的 vector 。简而言之,我需要帮助理解如何传递由 Recordset 字段值 (rs.symbol) 指针存储的整个值(在传递给 vector 时),而不仅仅是第一个字符?在这种情况下,从 SQL 返回的值是字符串。

#include "stdafx.h"
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include "iostream"
#include <icrsint.h>
#include <vector>
int j;
_COM_SMARTPTR_TYPEDEF(IADORecordBinding, __uuidof(IADORecordBinding));
inline void TESTHR(HRESULT _hr) { if FAILED(_hr) _com_issue_error(_hr); }
class CCustomRs : public CADORecordBinding {
    BEGIN_ADO_BINDING(CCustomRs)
        ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, symbol, sizeof(symbol), symbolStatus, false)
        END_ADO_BINDING()
public:
    CHAR symbol[6];
    ULONG symbolStatus;
};
int main() {
    ::CoInitialize(NULL);
    std::vector<char> tickers;
    try {
        char sym;
        _RecordsetPtr pRs("ADODB.Recordset");
        CCustomRs rs;
        IADORecordBindingPtr picRs(pRs);
        pRs->Open(L"SELECT symbol From Test", L"driver={sql server};SERVER=(local);Database=Securities;Trusted_Connection=Yes;", 
            adOpenForwardOnly, adLockReadOnly, adCmdText);
        TESTHR(picRs->BindToRecordset(&rs));
        while (!pRs->EndOfFile) {
            // Process data in the CCustomRs C++ instance variables.
//Try to load field value into a vector
            printf("Name = %s\n",
                (rs.symbolStatus == adFldOK ? rs.symbol: "<Error>"));


//This is likely where my mistake is
sym = *rs.symbol;//only seems to store the first character at the pointer's address


            // Move to the next row of the Recordset.   Fields in the new row will 
            // automatically be placed in the CCustomRs C++ instance variables.
//Try to load field value into a vector
            tickers.push_back (sym); //I can redefine everything as char*, but I end up with an array of a single memory location...
            pRs->MoveNext();
        }
    }
    catch (_com_error &e) {
        printf("Error:\n");
        printf("Code = %08lx\n", e.Error());
        printf("Meaning = %s\n", e.ErrorMessage());
        printf("Source = %s\n", (LPCSTR)e.Source());
        printf("Description = %s\n", (LPCSTR)e.Description());
    }
    ::CoUninitialize();
//This is me running tests to ensure the data passes as expected, which it doesn't
    std::cin.get();
    std::cout << "the vector contains: " << tickers.size() << '\n';
    std::cin.get();
    j = 0;
    while (j < tickers.size()) {
        std::cout << j << ' ' << tickers.size() << ' ' << tickers[j] << '\n';
        j++;
    }
    std::cin.get();
}

感谢您提供的任何指导。

最佳答案

A std::vector<char*>不起作用,因为相同的缓冲区用于所有记录。所以当pRs->MoveNext()被调用时,新的内容被加载到缓冲区中,覆盖之前的内容。

您需要复制内容。

我建议使用 std::vector<std::string> :

std::vector<std::string> tickers;
...

    tickers.push_back(std::string(rs.symbol));

关于c++ - ADO Recordset Field Value to C++ vector/array(捕获指针值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821369/

相关文章:

c++ - 将 mmap 偏移量的最后 12 位归零

c++ - 如何在 C++ 中唯一地表征数组?

VBA 使用 ADO 调用带参数的存储过程

excel - SSIS在导入和保存数据时将字符串转换为guid

c++ - 在 Items.ItemAdd 事件之后获取 MailItem

c++ - 从二进制文件中检索内存块

mysql - 微软 Access : Macro Button to download Table from MySQL

sql-server - 为什么带有日期/时间值的 TADOTable 中的 Locate() 不起作用

c++ - void* 数组将元素转换为另一种类型

Javascript float 混淆