紧急求助! IB如何保存Jpeg图片?

表中有一字段为PICTURE,BLOB字段,现在想用Image装入图片并保存,怎么办?以前有人问过类似的问题,但回答得不是很具体,小弟弟做毕业设计,就要交了,希望各位不吝赐教!谢谢!

---------------------------------------------------------------

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

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

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

TemplateStream=new TBlobStream((TBlobField*)WebQuery->FieldByName
("SearchTem"),bmReadWrite);
TemplatePtr= new 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 into the database.
To use WRITETEXT in its default, nonlogged state,
//注意!!
the system administrator must use the sp_dboption system stored
procedure to set select into/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="insert into content(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.byteEditionNum);
NewsScript.m_pScript=(BYTE )GlobalLock(NewsScript.m_hScript);
if(NewsScript.m_pScript!=NULL)
{
//再将刚插入的记录读出来,使该Query与该条记录关联
strSQL1="select file_id,script from content where file_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=new 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);
---------------------------------------------------------------

//我的一个程序代码就是如下所示,在interbase中保存jpeg图片
std::auto_ptr

 1<tmemorystream> stream (new TMemoryStream);   
 2stream-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);   
 3stream-&gt;Position = 0;   
 4String sqlstr;   
 5  
 6if(RadioButton1-&gt;Checked ) //添加图片   
 7{   
 8sqlstr = "insert into pic_table (PicName, Picture) values (:PicName, :Picture)"; //Picture即是jpeg图片   
 9  
10IBQuery1-&gt;Close();   
11IBQuery1-&gt;SQL-&gt;Clear();   
12IBQuery1-&gt;SQL-&gt;Add(sqlstr);   
13IBQuery1-&gt;Params-&gt;ParamByName("PicName")-&gt;AsString = OpenPictureDialog1-   
14&gt;FileName;   
15IBQuery1-&gt;Params-&gt;ParamByName("Picture")-&gt;LoadFromStream(stream.get(), ftBlob);   
16IBQuery1-&gt;ExecSQL();   
17}</tmemorystream>
Published At
Categories with 数据库类
comments powered by Disqus