用ADO操作ORACLE的BLOB字段

来源:互联网 发布:apache 泛解析配置 编辑:程序博客网 时间:2024/05/29 04:20
//用ADO操作ORACLE的BLOB字段//用AppendChunk向BLOB字段插入数据try{_variant_t val;TCHAR szSql[2048] = {0};CFile file;file.Open(chFilePath, CFile::modeRead);ULONGLONG nLength = file.GetLength();BYTE* pBuf = new BYTE[nLength + 2];file.Read(pBuf, (UINT)nLength);BYTE* pBufEx = pBuf;SAFEARRAY* psa;SAFEARRAYBOUND rgsabound[1];rgsabound[0].lLbound = 0;rgsabound[0].cElements = (ULONG)nLength;psa = SafeArrayCreate(VT_UI1, 1, rgsabound);for (long i = 0; i < nLength; i++)SafeArrayPutElement(psa, &i, pBufEx++);VARIANT varBLOB;varBLOB.vt = VT_ARRAY | VT_UI1;varBLOB.parray = psa;CString strSql = "select * from tableX where ID = ";char ID[100] = {0};sprintf_s(ID, "\'%s\'", chID);strSql += ID;_RecordsetPtr rsp;rsp.CreateInstance(__uuidof(Recordset));m_pConnection->CursorLocation = adUseClient;hr = rsp->Open(_variant_t(strSql), _variant_t(m_pConnection.GetInterfacePtr()), adOpenStatic, adLockOptimistic, adCmdText);long conut = rsp->GetFields()->GetCount();rsp->GetFields()->GetItem("DATA")->AppendChunk(varBLOB);rsp->Update();::VariantClear(&varBLOB);::SafeArrayDestroyData( psa);}catch (_com_error& e){CString strError;strError.Format(_T("插入消息到数据库失败!\r\n失败原因:%s"), (LPCTSTR)e.Description());OutputDebugString(strError);return -1;}// 用GetChunk获取BLOB字段的数据try{HRESULT hr;_RecordsetPtr rsp;rsp.CreateInstance(__uuidof(Recordset));char ID[100] = {0};sprintf_s(ID, "\'%s\'", chID);CString strSql = "select * from tableX where ID =";strSql += ID;m_pConnection->CursorLocation = adUseClient;hr = rsp->Open(_variant_t(strSql), _variant_t(m_pConnection.GetInterfacePtr()), adOpenStatic, adLockOptimistic, adCmdText);long dataSize = rsp->GetFields()->GetItem("DATA")->ActualSize;if (dataSize > 0){_variant_t varBlob;varBlob = rsp->GetFields()->GetItem("DATA")->GetChunk(dataSize);if (varBlob.vt == (VT_ARRAY | VT_UI1)){BYTE* pBuf = NULL;pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED, dataSize);SafeArrayAccessData(varBlob.parray, (void**)&pBuf);CFile f(strPath, CFile::modeCreate | CFile::modeWrite);LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);f.Write(buffer, dataSize);GlobalUnlock((HGLOBAL)pBuf);f.Close();SafeArrayUnaccessData(varBlob.parray);}}}catch (_com_error& e){CString strError;strError.Format(_T("插入消息到数据库失败!\r\n失败原因:%s"), (LPCTSTR)e.Description());OutputDebugString(strError);return -1;}



原创粉丝点击