讀寫SQLServer數據庫中的image類型數據(簡單)


1、將double類型的數據存儲於image類型的變量中:

(1)、   
char *CManualForecastResultBll::DoubleArray2Binary(std::vector<double> &doubleArray)
{
    int len = doubleArray.size();
    char *bin = new char[len * sizeof(double)];
    unsigned __int64 *p = (unsigned __int64*)bin;
    for (int i = 0; i < len; i++)
    {
        *p = DOUBLE2UINT64(doubleArray.at(i));
        p++;
    }
    return bin;
}

unsigned __int64 CManualForecastResultBll::DOUBLE2UINT64(double v)
{
    unsigned __int64 *pu64n = NULL;
    pu64n = reinterpret_cast<unsigned __int64*>(&v);
    return *pu64n;
}

        (2)、

 

         VARIANT            varBLOB;
                        SAFEARRAY        *psa;
                        SAFEARRAYBOUND    rgsabound[1];

                        rgsabound[0].lLbound = 0;
                        rgsabound[0].cElements = (ULONG)(pData->qLen);
                        psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
                        byte * pQt = pData->QTLINE;                                //自己的數據byte類型
                        for (long i = 0; i < pData->qLen; i++)
                            SafeArrayPutElement (psa, &i, pQt++);
                        varBLOB.vt = VT_ARRAY | VT_UI1;
                        varBLOB.parray = psa;
                        pRecordset->GetFields()->GetItem(SimulateResultDataFeilds[i])->AppendChunk(varBLOB);

 

2、根據已知image類型中存儲的數據類型(例如:double、float等)取數據:

case VT_ARRAY | VT_UI1:

             Binary2DoubleArray(vtFld);
                  break;

//////////////////////////////////////////////////////////////////////////
void DBRecordset::Binary2DoubleArray(_variant_t vtFld)
{
    int len =  vtFld.parray->rgsabound->cElements / sizeof(double);
    double *pdata = new double[len];

    /*int len =  vtFld.parray->rgsabound->cElements / sizeof(float);
    float *pdata = new float[len];*/

    SafeArrayAccessData(vtFld.parray, (void**)&pdata);

    /*unsigned __int64 *pu64n = new unsigned __int64[len];
    SafeArrayAccessData(vtFld.parray, (void**)&pu64n);*/

    ofstream writetofile("image.txt");
    for (int i = 0; i < len; i++)
    {
        if (i % 30 == 0 && i != 0)
        {
            writetofile<<endl;
        }
        writetofile<<pdata[i]<<"\t";
        //writetofile<<*(reinterpret_cast<double*>(&pu64n[i]))<<"\t";
        writetofile.flush();
    }
    writetofile<<endl;
    writetofile.close();
    SafeArrayUnaccessData(vtFld.parray);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM