CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

请问如何保存图片到sqlserver?

楼主fengfenghuohuo(none)2003-09-02 13:40:38 在 VB / 基础类 提问

数据库是sqlserver2000,如何保存图片进去,然后怎样取出来?  
  请教。 问题点数:100、回复次数:6Top

1 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2003-09-02 13:42:58 得分 50

微软的答案:  
  http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038Top

2 楼wingchi(期待第十五个五年计划)回复于 2003-09-02 13:45:12 得分 0

'******************将图片文件保存到数据库中*************************  
  Sub   SavePicToDb(cn   As   ADODB.Connection,   table1   As   String,   field1   As   String,   file1   As   String,   id1   As   String)  
                  On   Error   Resume   Next  
          Dim   stm   As   ADODB.Stream  
          Set   stm   =   New   ADODB.Stream  
          Set   rs1   =   New   ADODB.Recordset  
          rs1.Open   "select   *   from   "   &   table1   &   "   where   id   =   "   &   id1,   cn,   adOpenKeyset,   adLockOptimistic  
          With   stm  
                  .Type   =   adTypeBinary  
                  .Open  
                  .LoadFromFile   file1     'DLG.FileName  
          End   With  
          With   rs1  
                    .Fields(field1)   =   stm.Read  
                    .Update  
          End   With  
          rs1.Close  
          Set   rs1   =   Nothing  
  End   SubTop

3 楼wingchi(期待第十五个五年计划)回复于 2003-09-02 13:45:49 得分 30

Sub   GetPicFromDB(cn   As   ADODB.Connection)  
          On   Error   Resume   Next  
          Dim   fld   As   Field  
          Dim   strTemp   As   String  
          Dim   stm   As   ADODB.Stream  
          Set   stm   =   New   ADODB.Stream  
                                                                              'strTemp   =   "c:\temp.bmp"  
          Set   rs1   =   New   ADODB.Recordset  
          rs1.Open   "select   *   from   rs_http   where   htbh='"   &   frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel,   0)   &   "'",   cn,   ,   ,   adCmdText  
                  While   Not   rs1.EOF  
                            '*********将数据库中的文件读到硬盘上*************************  
                        '   strTemp   =   App.Path   +   "\temp\"   +   rs1!Name   '`临时文件,用来保存读出的图片  
   
                          With   stm  
                                    .Type   =   adTypeBinary  
                                    .Open  
                                    .Write   rs1("tp").value  
                                    strTemp   =   App.Path   &   "\temp1\"   &   rs1!Name  
                                    .SaveToFile   strTemp,   adSaveCreateOverWrite  
                                    .Close  
                          End   With  
                          Set   itemX   =   lvwPic.ListItems.add(,   App.Path   &   "\temp1\"   &   rs1!Name,   rs1!Name,   1,   1)  
                          itemX.SubItems(1)   =   rs1!bz  
                          rs1.MoveNext  
                  Wend  
          Set   stm   =   Nothing  
          rs1.Close  
          Set   rs1   =   Nothing  
          End   SubTop

4 楼wingchi(期待第十五个五年计划)回复于 2003-09-02 13:46:43 得分 0

Sub   GetPicFromDB(cn   As   ADODB.Connection)  
          On   Error   Resume   Next  
          Dim   fld   As   Field  
          Dim   strTemp   As   String  
          Dim   stm   As   ADODB.Stream  
          Set   stm   =   New   ADODB.Stream  
                                                                              'strTemp   =   "c:\temp.bmp"  
          Set   rs1   =   New   ADODB.Recordset  
          rs1.Open   "select   *   from   rs_http   where   htbh='"   &   frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel,   0)   &   "'",   cn,   ,   ,   adCmdText  
                  While   Not   rs1.EOF  
                            '*********将数据库中的文件读到硬盘上*************************  
                        '   strTemp   =   App.Path   +   "\temp\"   +   rs1!Name   '`临时文件,用来保存读出的图片  
   
                          With   stm  
                                    .Type   =   adTypeBinary  
                                    .Open  
                                    .Write   rs1("tp").value  
                                    strTemp   =   App.Path   &   "\temp1\"   &   rs1!Name  
                                    .SaveToFile   strTemp,   adSaveCreateOverWrite  
                                    .Close  
                          End   With  
                          Set   itemX   =   lvwPic.ListItems.add(,   App.Path   &   "\temp1\"   &   rs1!Name,   rs1!Name,   1,   1)  
                          itemX.SubItems(1)   =   rs1!bz  
                          rs1.MoveNext  
                  Wend  
          Set   stm   =   Nothing  
          rs1.Close  
          Set   rs1   =   Nothing  
          End   SubTop

5 楼cuizm(射天狼 http://www.j2soft.cn/)回复于 2003-09-02 13:49:54 得分 20

Option   Explicit  
   
  Private   Sub   Command1_Click()  
          Dim   lb_Photo()   As   Byte  
          Dim   rs   As   New   ADODB.Recordset  
           
          Open   "C:\aa.jpg"   For   Input   As   #1  
          Get   #1,   ,   lb_Photo  
          Close   #1  
           
          rs.Open   "select   *   from   table",   cn,   adOpenDynamic,   adLockPessimistic  
          rs.AddNew  
          rs.Fields("PHOTO").AppendChunk   lb_Photo  
          rs.Update  
           
          rs.Close  
          Set   rs   =   Nothing  
  End   Sub  
  Top

6 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2003-09-02 13:54:05 得分 0

 
  AppendChunk方法也可以,ado   2.5以前的版本不能用Stream  
   
   
  Public   Sub   AppendChunkX()  
   
        Dim   cn   As   ADODB.Connection  
        Dim   rstPubInfo   As   ADODB.Recordset  
        Dim   strCn   As   String  
        Dim   strPubID   As   String  
        Dim   strPRInfo   As   String  
        Dim   lngOffset   As   Long  
        Dim   lngLogoSize   As   Long  
        Dim   varLogo   As   Variant  
        Dim   varChunk   As   Variant  
   
        Const   conChunkSize   =   100  
   
        '   Open   a   connection.  
        Set   cn   =   New   ADODB.Connection  
        strCn   =   "Server=srv;Database=pubs;UID=sa;Pwd=;"  
   
        cn.Provider   =   "sqloledb"  
        cn.Open   strCn  
   
        'Open   the   pub_info_x   table.  
        Set   rstPubInfo   =   New   ADODB.Recordset  
        rstPubInfo.CursorType   =   adOpenDynamic  
        rstPubInfo.LockType   =   adLockOptimistic  
        rstPubInfo.Open   "pub_info_x",   cn,   ,   ,   adCmdTable  
   
        'Prompt   for   a   logo   to   copy.  
        strMsg   =   "Available   logos   are   :   "   &   vbCr   &   vbCr  
   
        Do   While   Not   rstPubInfo.EOF  
              strMsg   =   strMsg   &   rstPubInfo!pub_id   &   vbCr   &   _    
                  Left(rstPubInfo!pr_info,  
                    InStr(rstPubInfo!pr_info,   ",")   -   1)   &   vbCr   &   vbCr  
              rstPubInfo.MoveNext  
        Loop  
   
        strMsg   =   strMsg   &   "Enter   the   ID   of   a   logo   to   copy:"  
        strPubID   =   InputBox(strMsg)  
   
        '   Copy   the   logo   to   a   variable   in   chunks.  
        rstPubInfo.Filter   =   "pub_id   =   '"   &   strPubID   &   "'"  
        lngLogoSize   =   rstPubInfo!logo.ActualSize  
        Do   While   lngOffset   <   lngLogoSize  
              varChunk   =   rstPubInfo!logo.GetChunk(conChunkSize)  
              varLogo   =   varLogo   &   varChunk  
              lngOffset   =   lngOffset   +   conChunkSize  
        Loop  
   
        '   Get   data   from   the   user.  
        strPubID   =   Trim(InputBox("Enter   a   new   pub   ID:"))  
        strPRInfo   =   Trim(InputBox("Enter   descriptive   text:"))  
   
        '   Add   a   new   record,   copying   the   logo   in   chunks.  
        rstPubInfo.AddNew  
        rstPubInfo!pub_id   =   strPubID  
        rstPubInfo!pr_info   =   strPRInfo  
        lngOffset   =   0       '   Reset   offset.  
   
        Do   While   lngOffset   <   lngLogoSize  
              varChunk   =   LeftB(RightB(varLogo,   lngLogoSize   -   _    
                  lngOffset),conChunkSize)  
              rstPubInfo!logo.AppendChunk   varChunk  
              lngOffset   =   lngOffset   +   conChunkSize  
        Loop  
   
        rstPubInfo.Update  
   
        '   Show   the   newly   added   data.  
        MsgBox   "New   record:   "   &   rstPubInfo!pub_id   &   vbCr   &   _    
            "Description:   "   &   rstPubInfo!pr_info   &   vbCr   &   _    
            "Logo   size:   "   &   rstPubInfo!logo.ActualSize  
   
        rstPubInfo.Close  
        cn.Close  
   
  End   Sub  
   
  Top

7 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2003-09-02 13:55:57 得分 0

'Use     ADODB.Stream     Method      
  'After     ADO     2.5      
  'Import     the     Image     to     SQLServer      
  Private     Sub     ImportBLOB(cn     As     ADODB.Connection)      
                     
                Dim     rs     As     New     ADODB.Recordset      
                Dim     stm     As     ADODB.Stream      
                     
                Set     stm     =     New     ADODB.Stream      
                     
                '     Skip     any     table     not     found     errors      
                On     Error     Resume     Next      
                cn.Execute     "drop     table     BinaryObject"      
                     
                On     Error     GoTo     0      
                'Create     the     BinaryObject     table      
                cn.Execute     "create     table     BinaryObject     "     &     _      
                                                                    "(blob_id     int     IDENTITY(1,1),     "     &     _      
                                                                        "blob_filename     varchar(256),     "     &     _      
                                                                        "blob_object     image)"      
                                                                     
                rs.Open     "Select     *     from     BinaryObject     where     1=2",     cn,     adOpenKeyset,     adLockOptimistic      
                'Read     the     binary     files     from     disk      
                stm.Type     =     adTypeBinary      
                stm.Open      
                stm.LoadFromFile     App.Path     &     "\BLOBsample.jpg"      
                     
                rs.AddNew      
                rs!blob_filename     =     App.Path     &     "\BLOBsample.jpg"      
                rs!blob_object     =     stm.Read      
                     
                'Insert     the     binary     object     in     the     table      
                rs.Update      
                     
                rs.Close      
                stm.Close      
                     
                Set     rs     =     Nothing      
                Set     stm     =     Nothing      
                     
  End     Sub      
   
   
  'Display     the     image     on     image     control      
  Private     Sub     DisplayBLOB(cn     As     ADODB.Connection)      
     
                Dim     rs     As     New     ADODB.Recordset      
                     
                '     Select     the     only     image     in     the     table      
                rs.Open     "Select     *     from     BinaryObject     where     blob_id     =     1",     cn      
                     
                '     Set     the     DataSource     to     the     recordset      
                Set     imgBinaryData.DataSource     =     rs      
                'Set     the     DataField     to     the     BLOB     field      
                imgBinaryData.DataField     =     rs!blob_object.Name      
                     
                'Release     the     recordset      
                rs.Close      
                Set     rs     =     Nothing      
     
  End     Sub      
  Top

相关问题

  • jsp如何保存图片到sqlserver
  • 图片如何保存到数据库中sqlserver(ado来实现)
  • 如何保存图片(buffer)???
  • 如何保存图片
  • 怎么将图片保存到SQLserver中???
  • 请教:如何保存图片数据?
  • 如何将图片保存下来!!!
  • 如何将网页保存成图片?
  • 如何把文字保存成图片?
  • 数据库如何保存图片?

关键词

  • stm
  • 保存
  • adodb
  • 图片
  • rs
  • stream
  • dim
  • field
  • cn
  • open

得分解答快速导航

  • 帖主:fengfenghuohuo
  • lihonggen0
  • wingchi
  • cuizm

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo