ADO Recordset字段值到C ++向量/数组(捕获指针值)

时间:2016-03-05 23:51:52

标签: c++ ado data-conversion

我试图通过Visual C ++(express)查询SQL Server(Express)并将生成的数据集存储到C ++向量中(数组也很棒)。为此,我研究了ADO库,并在MSDN上找到了很多帮助。简而言之,引用msado15.dll库并使用这些功能(特别是ADO Record Binding,它需要icrsint.h)。简而言之,我已经能够查询数据库并使用printf()显示字段值;但是当我尝试将字段值加载到矢量中时,我感到磕磕绊绊。

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

感谢您提供任何指导。

2 个答案:

答案 0 :(得分:0)

为什么您没有使用std::string代替std::vector? 要添加字符,请使用以下成员函数之一: basic_string& append( const CharT* s ); - 对于cstrings, basic_string& append( const CharT* s,size_type count ); - 否则。 阅读更多信息:http://en.cppreference.com/w/cpp/string/basic_string/append

如果您想换行,只需将'\n'追加到您想要的地方即可。

答案 1 :(得分:0)

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

您需要制作内容的副本。

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

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

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