blob字段:BCB实现BLOB字段的读写



对于SQL中TEXT、IMAGE、MEMO字段存取可以采用下列:
BLOB字段读取:
TBlobSTream* TemplateStream;
char* TempPlatePtr;

TemplateStream=TBlobStream((TBlobField*)WebQuery->FieldByName
(\"SearchTem\"),bmReadWrite);
TemplatePtr= char[TemplateStream->Size];
TemplateStream->Read(TemplatePtr, TemplateStream->Size);

BLOB字段写入:
TBlobSTream* TemplateStream;
char* TempPlatePtr;

TemplateStream=TBlobStream((TBlobField*)WebQuery->FieldByName
(\"SearchTem\"),bmReadWrite);
TemplatePtr= char[TemplateStream->Size];
TemplateStream->Write(TemplatePtr, TemplateStream->Size);

________________________________________________________________
补充:
获得字段大小用datalength

SQL Server端要作点设置:
By default, WRITETEXT is a nonlogged operation. This means that
text or image data is not logged when it is written o the database.
To use WRITETEXT in its default, nonlogged state,
//注意!!
the system administrator must use the sp_dboption system stored
procedure to select o/bulkcopy,
//
which allows nonlogged data to be inserted.

做了试验,直接写SQL语句好象不行.
____________________________________________________________________
些注意事项和个例子
在写入时:
(1)如果使用是TTable,则要将其ReadOnly属性先置为false,然后Edit;
(2)如果使用是TQuery,则要将其RequestLive属性先置为true,然后Edit;
使得TTable(TQuery)是可写.

下面是个使用TQuery往content表(主键file_id)中插入条记录例子,
script为个BLOB字段:
TBlobStream *pScriptStream;
//插入条记录
strSQL1=\"insertocontent(file_id,script,key_image) values(\'\";
strSQL1=strSQL1+m_szFileID+\"\',null,null)\";
dmStoryEditor->qryExec->SQL->Clear;
dmStoryEditor->qryExec->SQL->Add(strSQL1);
dmStoryEditor->qryExec->ExecSQL;
dmStoryEditor->qryExec->Close;
//整理要写入Blob数据
LockMemories(NewsScript);
NewsScript.GetEdition(NewsScript.m_ScriptHead.EditionNum);
NewsScript.m_pScript=(BYTE *)GlobalLock(NewsScript.m_hScript);
(NewsScript.m_pScript!=NULL)
{
//再将刚插入记录读出来,使该Query和该条记录关联
strSQL1=\"selectfile_id,scriptfromcontentwherefile_id=\'\"+
m_szFileID+\"\'\";
//允许该Query写
dmStoryEditor->qryExec->RequestLive=true;
dmStoryEditor->qryExec->SQL->Clear;
dmStoryEditor->qryExec->SQL->Add(strSQL1);
dmStoryEditor->qryExec->Open;
dmStoryEditor->qryExec->First;
//将该Query置为可写
dmStoryEditor->qryExec->Edit;
pScriptStream=TBlobStream((TBlobField*)dmStoryEditor->
qryExec->FieldByName(\"script\"),bmReadWrite);
pScriptStream->Write(NewsScript.m_pScript,


NewsScript.m_lScriptRealSize);
dmStoryEditor->qryExec->Post;
dmStoryEditor->qryExec->RequestLive=false;
delete pScriptStream;
}
GlobalUnlock(NewsScript.m_hScript);
UnLockMemories(NewsScript);
Tags:  javablob oracleblob oracleblob字段 blob字段

延伸阅读

最新评论

发表评论