在vc中采用流指针处理写入数据库的方法如下。
采用_StreamPtr处理Loadfromfile,savetofile,open来处理。
/*
测试插入流文件,
*/
void insert_flow1()
{
_StreamPtr pwStream;
_bstr_t strPath("D:\\my\\21.wav");
_bstr_t strOpen("");
_variant_t varBLOB;
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
HRESULT hr = pwStream.CreateInstance(_uuidof(Stream));
if(SUCCEEDED(hr))
{
pwStream->Type = adTypeBinary;
hr = pwStream->Open(varOptional, adModeUnknown, adOpenStreamUnspecified, strOpen, strOpen);
}
_bstr_t strSql("select (max(id) +1) as id from t_mov1");
m_pRecordset->Open(strSql, _variant_t((IDispatch*)m_pConnection,true),
adOpenDynamic, adLockOptimistic, adCmdText);
variant_t vRet = m_pRecordset->GetCollect("id");
int id = vRet.lVal;
strSql = "select * from t_mov1 where 1 = 0";
m_pRecordset->Close();
m_pRecordset->Open(strSql, _variant_t((IDispatch*)m_pConnection,true),
adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(hr))
{
pwStream->LoadFromFile(strPath);
m_pRecordset->AddNew(vtMissing, vtMissing);
m_pRecordset->PutCollect("id", (long)id);
m_pRecordset->PutCollect("b_flow", pwStream->Read(adReadAll));
m_pRecordset->Update();
pwStream->Close();
}
}
/*
从数据库中读出流数据
*/
void read_flow1(int id)
{
char *m_pBuffer;
_StreamPtr prStream;
_bstr_t strOpen("");
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
HRESULT hr;
hr = prStream.CreateInstance(_uuidof(Stream));
if(SUCCEEDED(hr))
{
prStream->Type = adTypeBinary;
hr = prStream->Open(varOptional, adModeUnknown, adOpenStreamUnspecified, strOpen, strOpen);
}
CString strSql;
strSql.Format("select * from t_mov1 where id = %d", id);
m_pRecordset->Open((_bstr_t)strSql, _variant_t((IDispatch*)m_pConnection,true),
adOpenDynamic, adLockOptimistic, adCmdText);
if(SUCCEEDED(hr))
{
prStream->Write(m_pRecordset->Fields->GetItem("b_flow")->GetValue());
long lDataSize;
lDataSize = prStream->GetSize();
_variant_t varBLOB;
//流指针指向首位
prStream->put_Position(0);
//varBLOB.vt = VT_ARRAY | VT_UI1;
//varBLOB.scode = DISP_E_PARAMNOTFOUND;
varBLOB = prStream->Read(adReadAll);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
//分配必要的存储空间
if(m_pBuffer = new char[lDataSize+1])
{
char *pBuf = NULL;
//复制数据到缓冲区m_pBuffer
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(m_pBuffer, pBuf, lDataSize);
SafeArrayUnaccessData (varBLOB.parray);
}
}
prStream->SaveToFile("d:\\1.wav", adSaveCreateOverWrite);
prStream->Close();
}
}
Powered by ScribeFire.