sql server2000数据库
保存图片的字段使用image类型
在pb中用fileread()函数把图片读到一个blob型变量中,然后使用
updateblob语句把保存图片的blob型变量存到数据库中
保存image类型的变量要注意两点
1.autocommit属性要设者为true (sqlca.autocommit=true )
2.保存image型数据的表中一定要有主键,即表中要现有记录存在,而且该条记录
是唯一的,别且拥有主键。
一个保存图片的例子:
integer li_fh, li_ret, li_value
blob blob_reader_pic,blob_temp
string ls_pathname,ls_iccard_id, ls_no
string docname, named, ls_path
long ll_length,ll_size, ll_i, ll_rowcount,ll_row
long ll_len_docname, ll_len_named
sqlca.autocommit = true
li_value = GetFileOpenName("读取照片",docname, named, "jpg","*.jpg,*.jpg,")
if li_value <> 1 then return
ll_len_docname = len(docname)
ll_len_named = len(named)
ls_path = left(docname, ll_len_docname - ll_len_named)
ll_rowcount = dw_2.rowcount()
for ll_row=1 to ll_rowcount
sle_1.text=string(ll_row)+"/"+string(ll_rowcount)
ls_no=trim(dw_2.object.vc_reader_no[ll_row])
ls_pathname= ls_path +ls_no+ ".jpg"
ll_length=filelength(ls_pathname) //检查图片是否大于32765,因为fileread一次只能读32765
if ll_length>0 then //文件存在
if ll_length<32765 then //如果小于32765,就可以马上读出来
li_fh = FileOpen(ls_pathname, StreamMode!, read!, Shared!, Replace!)
if li_fh <> -1 and not isnull(li_fh) then
FileRead(li_fh, blob_reader_pic)
FileClose(li_fh)
end if
else //当文件大于32765时
ll_size=ll_length/32765+1 //计算出fileread要花几次读出
li_fh = FileOpen(ls_pathname, StreamMode!, read!, Shared!, Replace!)
if li_fh <> -1 then
for ll_i=1 to ll_size
FileRead(li_fh, blob_temp) //每次读出32765字节
blob_reader_pic=blob_reader_pic+blob_temp //将读出的内容累加到另一个二进制文件中
next
FileClose(li_fh)
else
messagebox('提示','错误')
return
end if
end if
ls_iccard_id=dw_2.object.vc_iccard_id[ll_row]
Updateblob readers
Set blb_photo=:blob_reader_pic
Where vc_iccard_id =:ls_iccard_id;
if sqlca.sqlcode = 0 then
commit;
else
MessageBox("提示","更新照片出错")
Rollback;
END IF
blob_reader_pic = blob("")
else
MessageBox("提示","文件<"+ls_pathname+">不存在!")
end if
next
你使用的时候只需把
把sql语句中的表名和字段名改为你的就行了
这是写入数据库的方法:
Table1->Edit();
TBlobField * pField=(TBlobField *)Table1->FieldByName("存图像的字段名" );
TBlobStream * pmem=new TBlobStream (pField,bmWrite);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->Assign(Image1->Picture->Graphic);
pBitmap->SaveToStream(pmem);
delete pBitmap;
delete pmem;
Table1->Post();
这是从数据库中读出并显示在Image上的代码:
TBlobField * pField=(TBlobField *)Table1->FieldByName("存图像的字段名");
TBlobStream* pmem=new TBlobStream (pField,bmRead);
pmem->Seek(0,soFromBeginning);
Graphics::TBitmap * pBitmap=new Graphics::TBitmap();
pBitmap->LoadFromStream(pmem);
Image1->Picture->Assign(pBitmap);
delete pBitmap;
delete pmem;