程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> BCB實現BLOB字段的讀寫

BCB實現BLOB字段的讀寫

編輯:關於C++

對於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);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved