[英]ADO Recordset Field Value to C++ vector/array (capture pointer value)
我正在尝试通过Visual C ++(快速)查询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();
}
感谢您提供的任何指导。
为什么不使用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'
。
std::vector<char*>
无效,因为所有记录都使用相同的缓冲区。 因此,当pRs->MoveNext()
,新内容将被加载到缓冲区中,从而覆盖之前的内容。
您需要复制内容。
我建议使用std::vector<std::string>
:
std::vector<std::string> tickers;
...
tickers.push_back(std::string(rs.symbol));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.