Ⅰ pb中怎樣把圖像數據插入SQL表
一、可以存圖片的相對地址到資料庫中(如:\pic\A10020.jpg),顯示的時候將列的display as picture屬性勾選上。
這種方法顯示的時候查詢數據會快些。
二、將圖片數據存入資料庫中,如果存入資料庫中,很快會讓你的資料庫增大,所以要盡量控制圖片的大小,存和讀取的方法在下面都有。
1、選擇圖片
string ls_pathname, ls_filename
integer li_value
li_value = GetFileOpenName("選擇照片", &
+ ls_pathname, ls_filename, "圖片文件","jpg File (*.jpg),*.jpg,bmp file(*.bmp),*.bmp,gif file(*.gif),*.gif")
IF li_value = 1 THEN
P_1.picturename=ls_pathname;
Elseif li_value=-1 then
MessageBox("選擇照片","打開文件錯誤!")
End If
2、獲取圖片的二進制數據
//處理照片文件數據開始
string ls_photofile
integer li_filenum
integer li_loops
integer li_counter
blob lb_picture
long ll_filelen
blob ll_read
ls_photofile=p_1.picturename;
ll_filelen=filelength(ls_photofile)
li_filenum=fileopen(ls_photofile,streammode!,read!,lockread!)
//*fileread()函數不支持讀取大於32k的文本,計算將使用fileread函數的次數 */
if ll_filelen>32765 then
li_loops=((ll_filelen - 1)/32765)+1
else
li_loops=1
end if
//讀文件
for li_counter=1 to li_loops
fileread(li_filenum,ll_read)
lb_picture=lb_picture+ll_read
next
fileclose(li_filenum)
//顯示圖片
p_1.setpicture(lb_picture)
//處理照片文件結束
3、存入資料庫
//存照片入資料庫
updateblob t_teacher set pic=:lb_picture where code = :ls_code;
if sqlca.sqlcode<>0 then
messagebox("系統提示","更新數據錯誤",stopsign!,ok!)
return
end if
4、顯示圖片
blob lb_pic
string ls_code
ls_code="01007";
selectblob pic into :lb_pic from t_teacher where code=:ls_code;
if sqlca.sqlcode<>0 then
messagebox("系統提示","載入數據錯誤",stopsign!,ok!)
return
end if
setpicture(p_1,lb_pic)