最新消息:

使用ado中的Stream对象向数据库上传图片

未分类 admin 3336浏览 0评论

try
{
CoInitialize(NULL);
_ConnectionPtr pConnection(__uuidof(Connection));
_RecordsetPtr pRecordset(__uuidof(Recordset));

pConnection->ConnectionString = “Provider=SQLOLEDB.1;Persist Security Info=False;User ID = sa;Initial Catalog = CodeTest;Data Source = 164.70.6.219″;
pConnection->Open(pConnection->ConnectionString, “”, “”, -1);//连接数据库

pRecordset->Open(“SELECT * FROM ma_q_test”,_variant_t((IDispatch*)pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
//打开记录集


_StreamPtr pStream(__uuidof(Stream));
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
pStream->PutType(adTypeBinary);//设置类型为二进制
pStream->Open(varOptional, adModeUnknown, adOpenStreamUnspecified, _bstr_t(), _bstr_t());//生成二进制流
pStream->LoadFromFile(_bstr_t(“D:\1.jpg”));//读取文件
_variant_t varBLOB = pStream->Read(adReadAll);//读取到varBLOB

pRecordset->AddNew();//曾加一条记录
pRecordset->GetFields()->GetItem(“image”)->Value = varBLOB;//保存数据对象
pRecordset->PutCollect(“id”, “1″);//设置id
pRecordset->Update();//更新~

pStream->Close();
pStream.Release();
pRecordset->Close();
pRecordset.Release();
if(pConnection->State)
pConnection->Close();
pConnection.Release();
CoUninitialize();
}
catch(_com_error e)
{
MessageBox(e.ErrorMessage());
}

转载请注明:爱开源 » 使用ado中的Stream对象向数据库上传图片

您必须 登录 才能发表评论!