您的位置:程序门 -> vb -> 基础类



如何查询并显示access中的图片字段值(ole型),叩谢帮忙的大虾了?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何查询并显示access中的图片字段值(ole型),叩谢帮忙的大虾了?
发表于:2007-04-09 08:51:04 楼主
设计一往access中存储图片的程序,现想对这数据库通过 "员工编号 "或 "员工姓名 "字段查询图片值,运行不过,请帮忙,谢谢了!
private   sub   command1_click()
dim   strsql   as   string
        strsql   =   "select   *   from   员工照片   where   "                 '//给定义好的字符变量赋予sql语句
        '//判断复选框有一个选中时,则
        if   chkcode.value   =   1   or   chkname.value   =   1   then
                '//判断二个复选框同时选中时,则
                if   chkcode.value   =   1   and   chkname.value   =   1   then
                        strsql   =   strsql   &   "员工编号=   ' "   &   trim(textnumber.text)   &   " ' "   &   "and "   &   "   员工姓名   =   ' "   &   trim(textname.text)   &   " ' "
                '//判断编号与进入公司时间复选框选中时,则
                        if   chkcode.value   =   1   then       '//只有编号复选框选中时,则
                                strsql   =   strsql   &   "员工编号=   ' "   &   trim(textnumber.text)   &   " ' "
                        end   if
                        if   chkname.value   =   1   then       '//只有姓名复选框选中时,则
                                strsql   =   strsql   &   "   员工姓名   =   ' "   &   trim(textname.text)   &   " ' "
                        end   if
                                      end   if
                        if   rstqueryphoto.state   =   adstateclosed   then
                            rstqueryphoto.open   strsql,   dbcon,   adopenkeyset,   adlockoptimistic,   adcmdtext//当其中一复选框选中再点击查询按钮时,次句被提示错误3709,连接无法用于此操作,在此上下文中可能被关闭或删除.
                        end   if
                  set   imgpreview.datasource   =   rsphoto.datasource
                imgpreview.refresh                 '//刷新网格
                rsphoto.close                           '//关闭记录集
                textnumber.text   =   empty                 '//请空文本框
                textname.text   =   empty
               
                rsphoto.close                           '//关闭记录集
              textnumber.text   =   empty                 '//请空文本框
                textname.text   =   empty
        else
        if   rsqueryphoto.state   =   adstateclosed   then
                rsqueryphoto.open   "员工照片 ",   dbcon,   adopenkeyset,   adlockoptimistic,   adcmdtable//当复选框没有选中再点击查询按钮时,次句也被提示错误3709,连接无法用于此操作,在此上下文中可能被关闭或删除.
        end   if
          set   imgpreview.datasource   =   rsphoto.datasource
  end   if
end   sub
发表于:2007-04-09 08:52:421楼 得分:0
使用流对象保存和显示图片  
打开vb6,新建工程。

添加两个按钮,一个image控件
注意:access中的photo字段类型为ole对象.
sqlserver中的photo字段类型为image

'**   引用   microsoft   activex   data   objects   2.5   library   及以上版本
‘2.5版本以下不支持stream对象
dim   iconcstr   as   string
dim   iconc   as   adodb.connection
 

'保存文件到数据库中
sub   s_savefile()
        dim   istm   as   adodb.stream
        dim   ire   as   adodb.recordset
        dim   iconcstr   as   string

        '读取文件到内容
        set   istm   =   new   adodb.stream
        with   istm
                .type   =   adtypebinary       '二进制模式
                .open
                .loadfromfile   app.path   +   "\test.jpg "
        end   with
     

        '打开保存文件的表
        set   ire   =   new   adodb.recordset
        with   ire
                .open   "select   *   from   img ",   iconc,   1,   3
                .addnew                   '新增一条记录
                .fields( "photo ")   =   istm.read
                .update
        end   with
     

      '完成后关闭对象
        ire.close
        istm.close
end   sub


sub   s_readfile()
        dim   istm   as   adodb.stream
        dim   ire   as   adodb.recordset
        '打开表
set   ire   =   new   adodb.recordset
‘得到最新添加的纪录
        ire.open   "select   top   1   *   from   img   order   by   id   desc ",   iconc,   adopenkeyset,   adlockreadonly
        '保存到文件
        set   istm   =   new   adodb.stream
        with   istm
                .mode   =   admodereadwrite
                .type   =   adtypebinary
                .open
                .write   ire( "photo ")
‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
                .savetofile   app.path   &   "\test1.jpg "
        end   with
     

        image1.picture   =   loadpicture(app.path   &   "\test1.jpg ")
      '关闭对象
        ire.close
        istm.close
end   sub
 

private   sub   command1_click()
call   s_readfile
end   sub


private   sub   command2_click()
call   s_savefile
end   sub


private   sub   form_load()
        '数据库连接字符串
        iconcstr   =   "provider=microsoft.jet.oledb.4.0;persist   security   info=false "   &   _
                ";data   source=f:\csdn_vb\database\保存图片\access图片\img.mdb "

‘下面的语句是连接sqlserver数据库的.
        ‘iconcstr   =   "provider=sqloledb.1;persist   security   info=true; "   &   _
‘   "user   id=sa;password=;initial   catalog=test;data   source=yang "
 

      set   iconc   =   new   adodb.connection
      iconc.open   iconcstr
end   sub
 

private   sub   form_unload(cancel   as   integer)
iconc.close
set   iconc   =   nothing
end   sub


快速检索

最新资讯
热门点击