您的位置:首页 > 其它

保存图片,文本文件,word文档等等方法都是一样的

2009-08-11 09:43 357 查看
保存图片,文本文件,word文档等等方法都是一样的  
   
  保存文章的字段使用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  
   
  你使用的时候只需把  
  li_value   =   GetFileOpenName("读取照片",docname,   named,   "jpg","*.jpg,*.jpg,")  
  中'jpg'改为你要保存的文章的扩展名即可,  
  word文档改为:  
  li_value   =   GetFileOpenName("读取word文档",docname,   named,   "doc","*.doc,*.doc,")    
   
  把sql语句中的表名和字段名改为你的,适当做一下修改就可以用了   
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息