- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
[摘自 水木清华站]
- Y" U/ S( y% _9 J" t * x8 h2 t* V, _( @0 k
6 `# K' H7 L3 [ n
对于SQL中的TEXT、IMAGE、MEMO字段的存取,可以采用下列程序:
5 q3 B5 ?: T! d9 b q9 TBLOB字段的读取: * T0 O& q0 _1 h% [0 z
TBlobSTream* TemplateStream; 7 N4 ~' Z4 c7 H# s) Y3 e
char* TempPlatePtr; p) n, _: q' D
. L/ C& a3 m# a" H
TemplateStream=new TBlobStream((TBlobField*)WebQuery->FieldByName
. r& w. F0 W+ F ("SearchTem"),bmReadWrite);
: x. w2 r, \; ? TemplatePtr= new char[TemplateStream->Size]; 8 n) I' W6 S6 u% T* V/ l
TemplateStream->Read(TemplatePtr, TemplateStream->Size);
# A! w. N' a5 V
# n/ V) r: x; P6 Z' A7 qBLOB字段的写入: / X m7 m3 m7 g$ i9 ^
TBlobSTream* TemplateStream;
* C% E: L; M4 Y: j" w! a char* TempPlatePtr;
# H6 g4 ]& `9 C0 p( q $ ] B% _" A3 r
TemplateStream=new TBlobStream((TBlobField*)WebQuery->FieldByName
" ?& g8 t' H1 d. Q. U% ? ("SearchTem"),bmReadWrite); ! z! w. U# Y* [8 |( o( y
TemplatePtr= new char[TemplateStream->Size]; + {" A/ O& g( T
TemplateStream->Write(TemplatePtr, TemplateStream->Size);
]$ z( I; k; W* g A5 e: i , m+ ?0 G* |- g& r
________________________________________________________________
. t: W/ P. q( L) [! i0 L9 T6 b补充:
: C9 H, ?1 k7 \2 k- x% S获得字段的大小用函数datalength
( P7 J9 ]( _- m' M( E9 }# ?
- e& }; D6 y9 a, H; r6 PSQL Server端要作一点设置: / s" V( H7 `4 o" a7 v0 x+ {
By default, WRITETEXT is a nonlogged operation. This means that
. M* ]$ [, i- i4 e* atext or image data is not logged when it is written into the database.
0 Z3 ]; U4 w5 }6 O+ U' ~% zTo use WRITETEXT in its default, nonlogged state, 6 Q5 {3 Z. q& y0 X/ m+ i1 t
//注意!!
' a* P, t' ^9 a5 p6 ^the system administrator must use the sp_dboption system stored
2 R9 `0 G7 Y; ]6 r+ P3 [procedure to set select into/bulkcopy, ; @' s- e* k+ [0 g6 g% w
//
7 f9 K! T0 `8 a6 l3 ]4 \which allows nonlogged data to be inserted. + g* v, o. E9 Q0 I) l/ X4 Z5 ?! `
3 S9 |% p$ t/ ~: i- D: l做了试验,直接写SQL语句好象不行. 6 i5 h4 r9 p( G. b) I2 \4 n
____________________________________________________________________ 1 H* d, T7 t* Y4 \2 }9 r/ I5 l5 ]
一些注意事项和一个例子
/ B* W. {" g5 |6 X2 w在写入时: ) `! K" o, x6 b& y
(1)如果使用的是TTable,则要将其ReadOnly属性先置为false,然后调用Edit函数;
# [) a# `/ x$ G9 W0 n* l( |6 M, o(2)如果使用的是TQuery,则要将其RequestLive属性先置为true,然后调用Edit函数; k" ]5 ]0 {0 k: p0 f2 o$ p
使得TTable(TQuery)是可写的.
" K' d; p2 h: ?
& C/ e/ x' p, i6 i V/ ~, L9 @下面是一个使用TQuery往content表(主键file_id)中插入一条记录的例子, . A. T6 {* G0 p! {
script为一个BLOB字段: ( U& C. P8 w( _) N( M! [
TBlobStream *pScriptStream;
- B# F& A& g3 e c1 J: I" }; Q //插入一条记录 0 u2 u* k/ @$ G; _2 X
strSQL1="insert into content(file_id,script,key_image) values('"; 9 z6 r6 z! p7 o8 G& i
strSQL1=strSQL1+m_szFileID+"',null,null)"; 1 Z4 ?5 k' H& G% M
dmStoryEditor->qryExec->SQL->Clear();
" b3 Z1 P; p# }1 _: P dmStoryEditor->qryExec->SQL->Add(strSQL1); ! ^+ l6 s, c$ A$ L/ Z
dmStoryEditor->qryExec->ExecSQL(); ! G2 X# Z; i+ |! `% g: N
dmStoryEditor->qryExec->Close();
2 {$ d1 m9 B" ?- i( o) j' g" {* e //整理要写入的Blob数据
" b" g& @3 Z* h2 Q4 C) ` LockMemories(NewsScript); : M$ o/ r) I8 x. ~6 k# M6 F
NewsScript.GetEdition(NewsScript.m_ScriptHead.byteEditionNum);
# ~' V3 }- l2 u& B4 y0 ] NewsScript.m_pScript=(BYTE *)GlobalLock(NewsScript.m_hScript);
7 V- r6 X5 {* |+ w if(NewsScript.m_pScript!=NULL)
, S8 F* Z; n; K. E% q { - ]( r* } m) W
//再将刚插入的记录读出来,使该Query与该条记录关联 : X& ~2 {; R" p/ {
strSQL1="select file_id,script from content where file_id='"+
# d8 {; A2 K! W) K8 {7 F m_szFileID+"'";
: j7 d3 N$ ^' D6 Z$ w //允许该Query写
# s* G+ R6 J" n' p. X4 e dmStoryEditor->qryExec->RequestLive=true; 9 }% v y/ w7 q
dmStoryEditor->qryExec->SQL->Clear(); ) [1 g$ m# s9 J! O5 l8 b* _
dmStoryEditor->qryExec->SQL->Add(strSQL1); / g* K9 y) |8 d2 z! S3 [! v2 a8 h
dmStoryEditor->qryExec->Open();
8 \; }9 M. B- {; ]+ c4 h+ | dmStoryEditor->qryExec->First(); ; _# ?# `' D# v H' R
//将该Query置为可写 ! Y) J3 q5 _( L+ m
dmStoryEditor->qryExec->Edit();
0 a* V+ p$ P; X' n" }% h pScriptStream=new TBlobStream((TBlobField*)dmStoryEditor->
2 X0 {+ n s! ]7 f qryExec->FieldByName("script"),bmReadWrite);
# f# Y& \. r4 [$ h/ y' C pScriptStream->Write(NewsScript.m_pScript,
% l2 _1 j, M& F; K NewsScript.m_lScriptRealSize);
- K$ Y) V( Z1 n2 M8 s% N+ @ dmStoryEditor->qryExec-> ost();
" Y# U6 E$ D% \" G, n dmStoryEditor->qryExec->RequestLive=false; & w% ?: Y& r! C' U$ }! Y- ~
delete pScriptStream; 5 b, c" `( _+ z* {' e
} 8 D% x4 x9 ^( y$ E W/ S
GlobalUnlock(NewsScript.m_hScript); A7 D: j# t) e4 M7 A) J
UnLockMemories(NewsScript); |
zan
|