如何在VB中读取 sql中image字段的内容

前端使用vb,数据库用sql2000,imgage字段存储的是图片信息,如何在前台进行读取和写入呢?  
   
  分不够再加

 

  • 推荐阅读:
  • 迎战微软“护照” SUN推出新商用软件
  • 英国牛配护照 WAP养殖真热闹
  • 惠氏营养品公司全国客户关系总监杨胜超先生
  • 中国客户关系管理研究中心首席顾问王广宇
  • 考验客户关系:银行向小额储户收取管理费
  • 浙江天台烟草客户关系管理探索
  • Sun欲改写在华客户关系 Q3推出Sun连接项目
  • 创智:PowerCRM银行客户关系管理解决方案

     

    发表者:zjcxc

    *************************************************************************  
      **  
      **   使用   adodb.stream   保存/读取文件到数据库  
      **   引用   microsoft   activex   data   objects   2.5   library   及以上版本  
      **  
      **   -----   数据库连接字符串模板   ---------------------------------------  
      **   access数据库  
      **   iconcstr   =   "provider=microsoft.jet.oledb.4.0;persist   security   info=false"   &   _  
      **           ";data   source=数据库名"  
      **  
      **   sql数据库  
      **   iconcstr   =   "provider=sqloledb.1;persist   security   info=true;"   &   _  
      **           "user   id=用户名;password=密码;initial   catalog=数据库名;data   source=sql服务器名"  
      **  
      *************************************************************************  
       
      保存文件到数据库中  
      sub   s_savefile()  
              dim   istm   as   adodb.stream  
              dim   ire   as   adodb.recordset  
              dim   iconcstr   as   string  
               
              数据库连接字符串  
              iconcstr   =   "provider=microsoft.jet.oledb.4.0;persist   security   info=false"   &   _  
                      ";data   source=f:/my   documents/客户资料1.mdb"  
               
              读取文件到内容  
              set   istm   =   new   adodb.stream  
              with   istm  
                      .type   =   adtypebinary         二进制模式  
                      .open  
                      .loadfromfile   "c:/test.doc"  
              end   with  
               
              打开保存文件的表  
              set   ire   =   new   adodb.recordset  
              with   ire  
                      .open   "表",   iconc,   adopenkeyset,   adlockoptimistic  
                      .addnew                   新增一条记录  
                      .fields("保存文件内容的字段")   =   istm.read  
                      .update  
              end   with  
               
              完成后关闭对象  
              ire.close  
              istm.close  
      end   sub  
       
      从数据库中读取数据,保存成文件  
      sub   s_readfile()  
              dim   istm   as   adodb.stream  
              dim   ire   as   adodb.recordset  
              dim   iconc   as   string  
               
              数据库连接字符串  
              iconc   =   "provider=microsoft.jet.oledb.4.0;persist   security   info=false"   &   _  
                      ";data   source=//xz/c$/inetpub/zj/zj/zj.mdb"  
               
              打开表  
              set   ire   =   new   adodb.recordset  
              ire.open   "tb_img",   iconc,   adopenkeyset,   adlockreadonly  
              ire.filter   =   "id=64"  
               
              保存到文件  
              set   istm   =   new   adodb.stream  
              with   istm  
                      .mode   =   admodereadwrite  
                      .type   =   adtypebinary  
                      .open  
                      .write   ire("img")  
                      .savetofile   "c:/test.doc"  
              end   with  
               
              关闭对象  
              ire.close  
              istm.close  
      end   sub  
     

    发表者:1ssp

    我想说的up说清楚了。同意。

    发表者:yoki

    这种例子随便抓都一大把,给你几个  
       
      dim   cn   as   new   adodb.connection  
      dim   rs   as   new   adodb.recordset  
      dim   stm   as   adodb.stream  
       
      private   sub   savepicturetodb(cn   as   adodb.connection)  
      将bmp图片存入数据库  
      on   error   goto   eh  
              set   stm   =   new   adodb.stream  
              rs.open   "select   imagepath,imagevalue   from   tbl_image",   cn,   adopenkeyset,   adlockoptimistic  
              commondialog1.showopen  
              text1.text   =   commondialog1.filename  
               
              with   stm  
                        .type   =   adtypebinary  
                        .open  
                        .loadfromfile   commondialog1.filename  
              end   with  
              with   rs  
                        .addnew  
                        .fields("imagepath")   =   text1.text  
                        .fields("imagevalue")   =   stm.read  
                        .update  
              end   with  
              rs.close  
              set   rs   =   nothing  
      exit   sub  
      eh:   msgbox   err.description,   vbinformation,   "error"  
      end   sub  
       
       
      private   sub   loadpicturefromdb(cn   as   adodb.connection)  
      载数据库中读出bmp图片  
      on   error   goto   eh  
              dim   strtemp   as   string  
              set   stm   =   new   adodb.stream  
              strtemp   =   "c:/temp.tmp"   临时文件,用来保存读出的图片  
              rs.open   "select   imagepath,imagevalue     from   tbl_image",   cn,   ,   ,   adcmdtext  
              with   stm  
                      .type   =   adtypebinary  
                      .open  
                      .write   rs("imagevalue")  
                      .savetofile   strtemp,   adsavecreateoverwrite  
                      .close  
              end   with  
              image1.picture   =   loadpicture(strtemp)  
              set   stm   =   nothing  
              rs.close  
              set   rs   =   nothing  
      exit   sub  
      eh:   msgbox   err.description,   vbinformation,   "error"  
      end   sub  
       
       
      image类型  
      用picture显示  
      以下两个函数是从数据库中读出图片的核心程序  
       
      public   function   getimage(optional   filename   as   string)   as   variant  
      on   error   goto   procerr  
       
          dim   objrs   as   adodb.recordset  
          dim   strsql   as   string  
          dim   chunk()   as   byte  
         
          set   objrs   =   new   adodb.recordset  
           
          strsql   =   "select   thumb   from   tblpictures   where   idpict="   &   tblid(thumbindex)   &   ""  
          strsql   =   "select   thumb   from   tblpictures   where   idpict=   "   &   thumb  
          strsql   =   "select   thumb   from   tblpictures   where   idpict=387"  
          db.execute   strsql  
          objrs.open   strsql,   db,   adopenforwardonly,   adlockreadonly  
           
          if   objrs.bof   and   objrs.eof   then  
              getimage   =   0  
              goto   procexit  
          elseif   isnull(objrs.fields(0))   then  
              errnumber   =   1001  
              errdesc   =   "字段为空"  
              goto   procexit  
          end   if  
           
          chunk()   =   objrs.fields(0).getchunk(objrs.fields(0).actualsize)  
          set   getimage   =   chunk2image(chunk(),   filename)  
       
      procexit:  
          on   error   resume   next  
          objrs.close  
              chunk()   =   objrs.fields(0).getchunk(0)  
              set   getimage   =   chunk2image(chunk(),   filename)  
          set   objrs   =   nothing  
       
          exit   function  
       
      procerr:  
          getimage   =   0  
          resume   procexit  
      end   function  
       
       
      private   function   chunk2image(chunk()   as   byte,   optional   filename   as   string)   as   variant  
      on   error   goto   procerr  
      dim   keepfile   as   boolean  
      dim   datafile   as   integer  
       
              keepfile   =   true  
              if   trim(filename)   =   ""   then  
                  filename   =   "c:/tmpxxdb.fil"  
                  keepfile   =   false  
              end   if  
       
              datafile   =   freefile  
              open   filename   for   binary   access   write   as   datafile  
                  put   datafile,   ,   chunk()  
              close   datafile  
       
      procexit:  
          set   chunk2image   =   loadpicture(filename)  
          on   error   resume   next  
          if   not   keepfile   then   kill   filename  
          exit   function  
       
      procerr:  
          on   error   resume   next  
          kill   filename  
          chunk2image   =   0  
      end   function  
       
       
      public   function   getfromfile(strtable   as   string,   strfield   as   string,   strfilter   as   string,   objfilename   as   string)   as   boolean    
       
      ============================================================    
       过程函数名:   commmodule.getfromfile 类型:function    
       参数:    
             strtable   (string)  :准备保存图形数据的表名称    
             strfield   (string)  :准备保存图形数据的字段名称    
             strfilter   (string)    :打开表的过滤字符串,用于定位并确保被打开的表的数据的唯一性    
             objfilename   (string)   :准备输入到表里边的图象文件名称    
       返回:如果保存成功,返回true,如果失败,返回false    
      -------------------------------------------------------------    
       说明:把图象文件的数据保存到表里边    
      -------------------------------------------------------------    
       修订历史:    
      =============================================================    
      dim   recset     as   adodb.recordset,   filedata()   as   byte,   fileno   as   long,   filesize   as   long,   strsql   as   string    
       
        strsql   =   "select   "   &   strfield   &   "   from   "   &   strtable   &   "   where   "   &   strfilter   &   ";"    
        set   recset   =   new   adodb.recordset    
        recset.open   strsql,   currentproject.connection,   adopendynamic,   adlockoptimistic    
        getfromfile   =   true    
        if   recset(strfield).type   <>   db_ole   or   not   isfilename(objfilename)   then    
          getfromfile   =   false       如果字段不是ole字段,或者文件不存在,返回错误    
          goto   endgetfromfile    
        end   if    
        if   recset.eof   then       如果记录不存在,返回错误    
          getfromfile   =   false    
          goto   endgetfromfile    
        end   if    
        filesize   =   getfilesize(objfilename)   如果被打开的文件大小为零,返回错误    
        if   filesize   <=   0   then    
          getfromfile   =   false    
          goto   endgetfromfile    
        end   if    
        redim   filedata(filesize)      重新初始化数组    
        fileno   =   freefile            获取一个空闲的文件号    
        open   objfilename   for   binary   as   #fileno 打开文件    
        get   #fileno,   ,   filedata()        读取文件内容到数组    
        close   #fileno              关闭文件    
        recset(strfield).value   =   filedata()   保存数据    
        recset.update              更新数据    
        erase   filedata           释放内存    
      endgetfromfile:    
        recset.close            关闭recordset    
        set   recset   =   nothing        释放内存    
      end   function    
       
      public   function   savetofile(strtable   as   string,   strfield   as   string,   strfilter   as   string,   strfilename   as   string)   as   boolean    
      ============================================================    
       过程函数名:   commmodule.savetofile   类型:function    
       参数:    
             strtable   (string)  :保存图形数据的表名称    
             strfield   (string)  :保存图形数据的字段名称    
             strfilter   (string)    :打开表的过滤字符串,用于定位并确保被打开的表的纪录的唯一性    
             strfilename   (string)   :准备保存的图象的文件名称    
       返回:如果保存成功,返回true,如果失败,返回false    
      -------------------------------------------------------------    
       说明:把由getfromfile函数保存到表中ole字段的数据还原到文件    
      -------------------------------------------------------------    
       修订历史:    
      =============================================================    
      dim   recset     as   adodb.recordset,   filedata()   as   byte,   fileno   as   long,   filesize   as   long,   strsql   as   string    
       
        strsql   =   "select   "   &   strfield   &   "   from   "   &   strtable   &   "   where   "   &   strfilter   &   ";"    
        set   recset   =   new   adodb.recordset    
        recset.open   strsql,   currentproject.connection,   adopendynamic,   adlockoptimistic    
        savetofile   =   true    
        if   recset(strfield).type   <>   db_ole   then    
          savetofile   =   false       如果字段不是ole字段,返回错误    
          goto   endsavetofile    
        end   if    
        if   recset.eof   then       如果记录不存在,返回错误    
          savetofile   =   false    
          goto   endsavetofile    
        end   if    
        fileno   =   freefile    
        open   strfilename   for   binary   as   #fileno    
        redim   filedata(recset(strfield).actualsize)   重新初始化数组    
        filedata()   =   recset(strfield).getchunk(recset(strfield).actualsize)   把ole字段的内容保存到数组    
        put   #fileno,   ,   filedata()    把数组内容保存到文件    
        close   #fileno    
        erase   filedata    
      endsavetofile:    
        recset.close    
        set   recset   =   nothing    
      end   function 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB.NET 读取 SQL 数据库的布尔值,您可以使用 SqlDataReader 对象的 GetBoolean 方法。 以下是一个示例代码片段,它从 SQL 数据库读取一个布尔值: ```vb Dim connString As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" Dim queryString As String = "SELECT MyBooleanColumn FROM MyTable WHERE ID = @ID;" Using connection As New SqlConnection(connString) Dim command As New SqlCommand(queryString, connection) command.Parameters.AddWithValue("@ID", 1) connection.Open() Dim reader As SqlDataReader = command.ExecuteReader() If reader.Read() Then Dim myBooleanValue As Boolean = reader.GetBoolean(0) '在这里使用您读取到的布尔值 End If reader.Close() End Using ``` 在此代码,我们首先建立了一个 SQL 连接,并设置了一个查询字符串,该字符串从名为 MyTable 的表选择 MyBooleanColumn 列,并仅选择具有 ID = 1 的行。 然后,我们使用 SqlCommand 对象创建一个命令对象,并向其添加一个名为 @ID 的参数。 接下来,我们打开连接,并使用 ExecuteReader 方法执行查询,并将结果存储在 SqlDataReader 对象。 最后,我们使用 GetBoolean 方法从 SqlDataReader 对象读取布尔值,并将其存储在名为 myBooleanValue 的变量。 请注意,GetBoolean 方法的参数是从零开始的列索引。因此,如果您要读取的布尔值是查询结果的第一列,则应将参数设置为 0。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值