设为首页
加入收藏夹

在VC中用OLE DB读写SQL Server中的BLOB
浏览选项:

OB字段数据时可用如下的方法:

 

BOOL SetBLOBData(WCHAR *awcname,CString strFile)

{

        DBOBJECT ObjectStruct;

    ObjectStruct.dwFlags = STGM_READ;

    ObjectStruct.iid     = IID_ISequentialStream;

 

    struct BLOBDATA

    {

        DBSTATUS            dwStatus;  

        DWORD               dwLength;

        ISequentialStream*  pISeqStream;

    };

    BLOBDATA BLOBGetData;

    BLOBDATA BLOBSetData;

 

    const ULONG cBindings = 1;

    DBBINDING rgBindings[cBindings];

 

    HRESULT hr = S_OK;

 

    IAccessor*          pIAccessor          = NULL;

    ICommandProperties* pICommandProperties = NULL;

    IRowsetChange*      pIRowsetChange      = NULL;

    IRowset*            pIRowset            = NULL;

    CSeqStream*         pMySeqStream        = NULL;

 

    ULONG cRowsObtained = 0;

    HACCESSOR hAccessor = DB_NULL_HACCESSOR;

    DBBINDSTATUS rgBindStatus[cBindings];

    HROW* rghRows = NULL;

    const ULONG cPropSets = 1;

 

    DBPROPSET   rgPropSets[cPropSets];

    const ULONG cProperties = 1;

    DBPROP      rgProperties[cProperties];

 

    rgPropSets[0].guidPropertySet = DBPROPSET_ROWSET;

    rgPropSets[0].cProperties = cProperties;

    rgPropSets[0].rgProperties = rgProperties;

 

    rgPropSets[0].rgProperties[0].dwPropertyID = DBPROP_UPDATABILITY;

    rgPropSets[0].rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;

    rgPropSets[0].rgProperties[0].dwStatus = DBPROPSTATUS_OK;

    rgPropSets[0].rgProperties[0].colid = DB_NULLID;

    rgPropSets[0].rgProperties[0].vValue.vt = VT_I4;

    V_I4(&rgPropSets[0].rgProperties[0].vValue) = DBPROPVAL_UP_CHANGE;

 

    //设置Rowset属性

    hr = g_pICommandText->QueryInterface(IID_ICommandProperties,

                            (void **)&pICommandProperties);

    if (FAILED(hr))

    {

        TRACE0("Failed to get ICommandProperties to set rowset properties. ");

        return FALSE;

    }

    hr = pICommandProperties->SetProperties(cPropSets, rgPropSets);

    if (FAILED(hr))

    {

        TRACE0("Execute failed to set rowset properties. ");

        return FALSE;

    }

 

    //执行命令

        CString strSql;

        strSql.Format("SELECT Stream FROM tMaterials WHERE MaterialID = %s ",gToQuote(awcname));

 

        int nlen=strSql.GetLength();

        wchar_t *pSql=(wchar_t*)malloc(nlen*sizeof(wchar_t));   

        mbstowcs(pSql,strSql.GetBuffer(MAX_PATH),nlen);

 

        if (FAILED(hr = g_pICommandText->SetCommandText(DBGUID_DBSQL,

                pSql)))

        {

                TRACE0("failed");

                return FALSE;

        }

 

 

        hr = g_pICommandText->Execute(NULL, IID_IRowsetChange, NULL, NULL,

                                (IUnknown**)&pIRowsetChange);

    if (FAILED(hr))

    {

        TRACE0("Failed to execute the command ");

        return FALSE;

    }

 

    rgBindings[0].iOrdinal = 1; //你的BLOB字段的位置(从1开始),改为你所需要的

    rgBindings[0].obValue = offsetof(BLOBDATA, pISeqStream);

    rgBindings[0].obLength = offsetof(BLOBDATA, dwLength);

    rgBindings[0].obStatus = offsetof(BLOBDATA, dwStatus);

    rgBindings[0].pTypeInfo = NULL;

    rgBindings[0].pObject = &ObjectStruct;

    rgBindings[0].pBindExt = NULL;

    rgBindings[0].dwPart =  DBPART_VALUE | DBPART_STATUS | DBPART_LENGTH;

    rgBindings[0].dwMemOwner = DBMEMOWNER_CLIENTOWNED;

    rgBindings[0].eParamIO = DBPARAMIO_NOTPARAM;

    rgBindings[0].cbMaxLen = 0;

    rgBindings[0].dwFlags = 0;

    rgBindings[0].wType = DBTYPE_IUNKNOWN;

    rgBindings[0].bPrecision = 0;

    rgBindings[0].bScale = 0;

 

    hr = pIRowsetChange->QueryInterface(IID_IAccessor,

                                        (void**)&pIAccessor);

    if (FAILED(hr))

    {

        TRACE0("Failed to get IAccessor interface. ");

        return FALSE;

    }

 

    hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,

                                    cBindings,

                                    rgBindings,

                                    sizeof(BLOBDATA),

                                    &hAccessor,

                                    rgBindStatus);

    if (FAILED(hr))

    {

        TRACE0("Failed to create an accessor. ");

        return FALSE;

    }

 

    hr = pIRowsetChange->QueryInterface(IID_IRowset,

                                        (void **)&pIRowset);

    if (FAILED(hr))

    {

        TRACE0("Failed to get IRowset interface. ");

        return FALSE;

    }

 

    hr = pIRowset->GetNextRows(NULL,0, 1,&cRowsObtained,&rghRows);

    hr = pIRowset->GetData(rghRows[0],

                           hAccessor,

                           &BLOBGetData);

    if (BLOBGetData.dwStatus == DBSTATUS_S_ISNULL)

        //在数据库的当前字段为NULL

        TRACE0("Provider returned a null value. ");

        else if(BLOBGetData.dwStatus == DBSTATUS_S_OK)

    {

                //在这里,从服务端为你分配的ISequentialStream接口读入BLOB数据

                //BLOBGetData.pISeqStream->Read(pBuffer,cBytes,&cBytesRead);

 

                //无论你是否有读数据,

        SAFE_RELEASE(BLOBGetData.pISeqStream);

    }

 

    //生成一个新的Stream.

    pMySeqStream = new CSeqStream();

 

        //开始从文件中读数据写入数据库

        CFile fle;

        if (fle.Open(strFile,CFile::modeRead))

        {

 

[首页]    [上一页]    [下一页]    [末页]    

Copyright © 2004 wanxu.com All Rights Reserved