c++ - 从 sql 2008 表中获取 vector

标签 c++ visual-studio-2008 sql-server-2008 vector matrix

我在 SQL Server 2008 中有一个名为 test 的表,它有 6 列和 6 行:

1att  2att  3att  4att  5att  6att 
----------------------------------     
467   116   480   477   491  697  
NULL  219   481   113   488  466  
NULL  NULL  477   466   455  480  
NULL  NULL  NULL  527   483  629  
NULL  NULL  NULL  NULL  483  483  
NULL  NULL  NULL  NULL  NULL 697  

我想要一个包含所有值但按列顺序排列的 vector 。

所以我的 vector 看起来像

[ 
    467  116   480   477   491  697    //row 1
    116  219   481   113   488  466    //row 2
    480  481   477   466   455  480    //row 3
    477  113   466   527   483  629    //row 4
    491  488   455   483   483  483    //row 5
    697  466   480   629   483  697    //row 6

 ]

为此,我使用 Visual Studio 2008 和 c++,以及使用 ADO 进行连接的 nd。

// svd-conn.cpp: archivo de proyecto principal.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#define MSADO15_PATH "c:\program files\common files\system\ado\msado15.dll"
#ifdef _MSC_VER
    #import MSADO15_PATH rename ("EOF","adoEOF") no_namespace
#else
#define V_INT(X)         V_UNION(X, intVal)
#define V_UINT(X)        V_UNION(X, uintVal)
#include "msado15.tlh"
#endif

#include <comutil.h>

struct InitOle{
     InitOle()  { ::CoInitialize(NULL); }
    ~InitOle() { ::CoUninitialize();   }
} InitOle_tag;

//------------------ utility fns to simplify access to recordset fields
_bstr_t RsItem( _RecordsetPtr p, BSTR fldName ){
    // by field name
    return( p->Fields->Item[_variant_t(fldName)]->Value );
}
_bstr_t RsItem( _RecordsetPtr p, long nIdx ){
    // by field # (0 is first)
    return( p->Fields->Item[_variant_t(nIdx)]->Value );
}
//-------------------------------- The Program ----------------
int main(){
    _RecordsetPtr spRs;
    HRESULT hr;
    _bstr_t sConn= "driver={sql server};SERVER=VIRTUALPC;Database=test;UID=sa; PWD=;";
    _bstr_t sSQL= "SELECT * FROM dbo.test ;";

    try{
        hr= spRs.CreateInstance( __uuidof(Recordset) );
        if FAILED(hr)
            printf("CreateInstance failed\n");
        else
            printf("CreateInstance SUCCESS\n");

        hr= spRs->Open( sSQL, sConn, adOpenForwardOnly, adLockReadOnly, adCmdText );
        if FAILED(hr) 
            printf("Open failed\n");
        else
            printf("Open SUCCESS\n");

        printf("spRs->adoEOF %s\n",spRs->adoEOF);
        while( !(spRs->adoEOF) ) {
            printf("%s\t%s\n",(char*) RsItem( spRs, 0L ),(char*) RsItem( spRs, 1L ),(char*) RsItem( spRs, 2L ) );
            spRs->MoveNext();
        }

        spRs->Close();

    } catch( _com_error &e) {
        printf("Error:%s\n",(char*)e.Description());
    }
    return 0;
}

使用该代码,在 while 循环内我可以获得值,但我如何才能将它们以正确的方式插入到 vector 中?

那么我如何合并 vector 类:

vector <double> v;
并得到想要的 vector ??

我知道要插入 vector 我会这样做

v.push_back(467); ....

但是如何以编程方式进行,实际上 NULLS 才是真正的问题...

最佳答案

来自问题:事实上,NULLS 才是真正的问题...
确实是一个相对简单的问题。通过跟踪行和列,并且由于 vector 是按行顺序填充的,因此可以从它们的转置位置获得对角线下方单元格的值,即 vector 中数据的值。
可能类似于以下内容:

#define COLS_PER_ROW 6
int rowNum = 0;
vector <double> v(COLS_PER_ROW * COLS_PER_ROW, 0);     
while( !(spRs->adoEOF) ) {
    for (colNum = 0; colNum < COLS_PER_ROW; colNum++)
    {
       double dVal;
       bstr_t sVal = RsItem(spRs, colNum);
       if (sVal) // this test is equivalent to if (colNum >= rowNum)
       {
           dval = strtod(sVal, NULL);
       }
       else
           dVal = v[colNum * COLS_PER_ROW + rowNum];  // not an error as we want to read the transpose loc for the cell.
    }
    rowNum++;
    spRs->MoveNext();
}

关于空值测试的小记
if (sVal) 测试数据库提供的值是否为空。
一个更好的测试可能是
if (colNum >= rowNum) 即假设在下三角中找到空值,并且与我们在上三角的“镜像”/“转置”位置获取值一致。

关于c++ - 从 sql 2008 表中获取 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700270/

相关文章:

SQL Server 输出 : How should i proceed to get this output in Table?

sql - 比较两个 SQL 数据库

sql - SQL Server 2008中的时序比较

c++ - C++程序中的杂散 '\342'

c++ - 使用 Glade 和 gtkmm 对 GTK+ 对象进行内存管理

c++ - 为什么隐式转换在累积中不起作用?

visual-studio-2008 - 如何让Resharper使用Visual Studio的代码片段

临时地址的 C++ 地址不会导致构建错误

.net - VsInstr 性能分析

c++ - 如果不允许 constexpr,为什么 sfinae on?