如何將圖片存到數據庫中?
2024-07-21 02:10:53
供稿:網友
如果你用的是sql server數據庫!你不想用后臺操作你可以看看這個
下面是對text的操作你可以看看
1. 寫操作(writetext)
這里一般要用到的函數有textptr獲得文本字段的指針,和textvaild檢驗指針的有效性,@@rowcount判斷返回記錄的條數。
其基本方法是:用textptr函數得到指針,判斷其有效性,用writetext寫數據
函數說明:textptr(字段名)。writetext tablename。fieldname @textptr(指針) [with log] data(數據)
例如:
begin tran
declare @mytextptr varbinary(16)
select @mytextptr=textptr(pr_info)
from pub_info (updlock)
where pud_id=’9999’
if @mytextptr is not null
writetext pub_info.pr_info @mytextptr with log ‘data’
commit tran
2. 讀操作
常用函數
patindex(‘%exp%’,var|fieldname。。)
datalength()
@@textsize 文本大小
settextsize n 設置文本大小
readtext {tablename。fieldname} {@textptr} offet size [holdlock]
例如:
begin tran
declare @mytextptr varbinary(16),@totalsize int,@readsize int,@lastread int
set textsize 100
select @mytextptr=textptr(pr_info), @totalsize=datalength(pr_info)
@lastread=0,
@readsize= case when (textsize<datalength(pr_info) then textsize
eles datalength(pr_info)
end
from pub_info
where pub_id=’1622’
if @mytextptr is not null and @readsize>0
while (@lastread<@totalsize)
readtext pub_info.pr_info @mytextptr @lastread @readsize holdlock
if (@@error<>0)
break
select @[email protected][email protected]
if ((@[email protected])>@totalsize)
select @[email protected]@lastread
end
commit tran
3.數據更新updatetext
更新數據代替了寫操作,其基本語法是:
updatetext table_name.col_name text_ptr offest(偏移量) deleted_length
[with log] [inserted_data|table_name.scr_column_name str_text_ptr]
說明:
offest:0說明從開頭開始,null表示你向當前內容追加數據。
deleted_length:0表示不刪除任何內容,null表示刪除所有內容。
例如1(完全代替):
declare @mytextptr varbinary(16)
begin tran
select @mytextptr=textptr(pr_infro) from pub_info(uplock) where pub_id=’9999’
if @mytextptr is not null
updatetext pub_info.pr_infro @mytextptr 0 null with log “you are right”
commit
例如2:
declare @mytextptr varbinary(16) ,@offest int
begin tran
select @mytextptr=textptr(pr_infro),@offest=patindex(‘%d.c%’,pr_infro)-1+4
/*減一是因為有一個矯正的偏移量,加4是因為d.c.是4*/
from pub_info(unlock) where pub_id=’0877’
if @mytextptr is not null and @offest>=0
updatetext pub_info.pr_infro @mytextptr @offest null with log
commit tran
例如3:
文本追加的問題
將出版商pub_id=9952的內容追加到出版商pub_id=0877d的文本中。
delcare @source_textptr varbinary(16),@target_textptr varbinary(16)
begin tran
select @source_textptr=textptr(pr_infro) from pub_info(uplock) where pub_id=’0877’
select @target_textptr=textptr(pr_infro) from pub_info(uplock) where pub_id=’9952’
if @source_textptr is not null and @target i s not null
updatetext pub_info.pr_infro @target_textptr null null
with log pub_info.pr_infro @source_textptr