PowerBuilder读取硬盘图片显示出来并保存到数据库中

PowerBuilder读取硬盘图片显示出来并保存到数据库中


数据库是SQL SERVER

表:blobtab

列:

id,int,主键自增

blobdata,image,二进制内容可空


注:PB中存储二进制数据和读取二进制数据只能用updateblob语句和selectblob语句



显示图片到控件里的代码:

//显示图片 p_1是图片控件,lb_tot_b 是二进制图片内容,获取方式在下面的代码里
setpicture(p_1,lb_tot_b)


存入数据库的代码:

setpointer(hourglass!) //设置鼠标开头为沙漏
int li_filenum,li_loops,li_counter
long ll_filelen,ll_bytes_read,ll_new_pos
blob lb_our_blob,lb_tot_b
ll_filelen = filelength(sle_1.text) //获取文件长度
//指定该文件为只读,li_filenum为这个文件的句柄
li_filenum = fileopen(sle_1.text,STREAMMODE!,READ!,LOCKREAD!)
//fileread()函数不支持读取大于32K的文本,计算将使用fileread函数的次数
if ll_filelen>32766 then
	li_loops = ((ll_filelen - 1)/32766)+1
else
	li_loops = 1
end if

//读文件
for li_counter=1 to li_loops
	ll_bytes_read = fileread(li_filenum,lb_our_blob)
	lb_tot_b += lb_our_blob
	ll_new_pos += ll_bytes_read
	fileseek(li_filenum,ll_new_pos,FROMBEGINNING!)
next
//关闭文件句柄
fileclose(li_filenum)

//存入数据库 lb_tot_b 里的内容就是最后得到的二进制的东西
updateblob blobtab set blobdata=:lb_tot_b where id=1 using sqlca;
if sqlca.sqlcode<>0  then
	messagebox('插入数据失败',sqlca.sqlerrtext)
else
	messagebox('','插入数据成功')
end if


显示数据库中图片的代码:

blob lb_blob_var
selectblob blobdata into :lb_blob_var from blobtab where id=1 using sqlca;
if sqlca.sqlcode<>0  then
	messagebox('取数据失败',sqlca.sqlerrtext)  
end if
setpicture(p_1,lb_blob_var)