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

*********100分,如何用vb连接sql存取图片!************

楼主sxhv998(^_^)Dorian(^_^)2003-11-01 15:37:05 在 VB / 基础类 提问

如题 问题点数:100、回复次数:5Top

1 楼TechnoFantasy((VB MVP)www.applevb.com)回复于 2003-11-01 15:50:42 得分 100

http://www.china-askpro.com/msg1/qa99.shtml  
   
  http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q153/2/38.asp&NoWebContent=1  
   
  http://www.china-askpro.com/msg41/qa82.shtml  
  Top

2 楼ZWmain(呆瓜)回复于 2003-11-01 17:25:37 得分 0

你可以在SQL里只存储图片的URLTop

3 楼yoki(小马哥--鬓微霜,又何妨)回复于 2003-11-01 17:49:54 得分 0

Example   1   :   Saving   the   Data   in   a   SQL   Server   Image   Column   to   a   File   on   the   Hard   Disk  
  The   code   in   this   example   opens   a   recordset   on   the   pub_info   table   in   the   pubs   database   and   saves   the   binary   image   data   stored   in   the   logo   column   of   the   first   record   to   a   file   on   the   hard   disk,   as   follows:    
  Open   a   new   Standard   EXE   Visual   Basic   project.  
  On   the   Project   menu,   click   to   select   References,   and   then   set   a   reference   to   the   Microsoft   ActiveX   Data   Objects   2.5   Object   Library.  
  Place   a   CommandButton   control   on   Form1.  
  Make   the   following   declarations   in   the   form's   General   declarations   section:   Dim   cn   As   ADODB.Connection  
  Dim   rs   As   ADODB.Recordset  
  Dim   mstream   As   ADODB.Stream  
   
  Cut   and   paste   the   following   code   into   the   Click   event   of   the   CommandButton   that   you   added   to   the   form:   Set   cn   =   New   ADODB.Connection  
  cn.Open   "Provider=SQLOLEDB;data   Source=<name   of   your   SQL   Server>;  
  Initial   Catalog=pubs;User   Id=<Your   Userid>;Password=<Your   Password>"  
   
  Set   rs   =   New   ADODB.Recordset  
  rs.Open   "Select   *   from   pub_info",   cn,   adOpenKeyset,   adLockOptimistic  
   
  Set   mstream   =   New   ADODB.Stream  
  mstream.Type   =   adTypeBinary  
  mstream.Open  
  mstream.Write   rs.Fields("logo").Value  
  mstream.SaveToFile   "c:\publogo.gif",   adSaveCreateOverWrite  
   
  rs.Close  
  cn.Close  
   
  Save   and   run   the   Visual   Basic   project.  
  Click   the   CommandButton   to   save   the   binary   data   in   the   logo   column   of   the   first   record   to   the   file   c:\publogo.gid.   Look   for   this   file   in   Windows   Explorer   and   open   it   to   view   the   saved   image.  
   
  The   code   in   this   example   declares   an   ADODB   Stream   object   and   sets   its   Type   property   to   adTypeBinary   to   reflect   that   this   object   will   be   used   to   work   with   Binary   data.   Following   this,   the   binary   data   stored   in   the   logo   column   of   the   first   record   in   the   pub_info   table   is   written   out   to   the   Stream   object   by   calling   its   Write   method.   The   Stream   object   now   contains   the   binary   data   that   is   saved   to   the   file   by   calling   its   SaveToFile   method   and   passing   in   the   path   to   the   file.   The   adSaveCreateOverWrite   constant   passed   in   as   the   second   parameter   causes   the   SaveToFile   method   to   overwrite   the   specified   file   if   it   already   exists.  
  Example   2   :   Transfer   the   Image   Stored   in   a   .gif   File   to   an   Image   Column   in   a   SQL   Server   Table  
  The   code   in   this   example   saves   an   image   stored   in   a   .gif   file   to   the   logo   column   in   the   first   record   of   the   pub_info   table   by   overwriting   its   current   contents,   as   follows:    
  Open   a   new   Standard   EXE   Visual   Basic   project.  
  On   the   Project   menu,   click   to   select   References,   and   then   set   a   reference   to   the   Microsoft   ActiveX   Data   Objects   2.5   Object   Library.  
  Place   a   CommandButton   on   Form1.    
  Make   the   following   declarations   in   the   form's   General   declarations   section:Dim   cn   As   ADODB.Connection  
  Dim   rs   As   ADODB.Recordset  
  Dim   mstream   As   ADODB.Stream  
   
  Cut   and   paste   the   following   code   in   the   Click   event   of   the   CommandButton   that   you   added   to   the   form:Set   cn   =   New   ADODB.Connection  
  cn.Open   "Provider=SQLOLEDB;data   Source=<name   of   your   SQL   Server>;  
  Initial   Catalog=pubs;User   Id=<Your   Userid>;Password=<Your   Password>"  
   
  Set   rs   =   New   ADODB.Recordset  
  rs.Open   "Select   *   from   pub_info",   cn,   adOpenKeyset,   adLockOptimistic  
   
  Set   mstream   =   New   ADODB.Stream  
  mstream.Type   =   adTypeBinary  
  mstream.Open  
  mstream.LoadFromFile   "<path   to   .gif   file>"  
  rs.Fields("logo").Value   =   mstream.Read  
  rs.Update  
   
  rs.Close  
  cn.Close  
   
  Save   and   run   the   Visual   Basic   project.  
  Click   on   the   CommandButton   to   run   the   code   to   stream   the   contents   of   the   .gif   file   to   the   ADO   Stream   object,   and   save   the   data   in   the   Stream   to   the   logo   column   in   the   first   record   of   the   recordset.  
  Verify   that   the   image   in   the   logo   column   has   been   modified   by   using   the   code   in   Example   1.  
  Top

4 楼yoki(小马哥--鬓微霜,又何妨)回复于 2003-11-01 17:52:17 得分 0

简单来说就是两点:  
   
   
  存:  
  Dim   rs   As   ADODB.Recordset  
  Dim   mstream   As   ADODB.Stream  
   
  Set   rs   =   New   ADODB.Recordset  
  rs.Open   "Select   *   from   pub_info",   cn,   adOpenKeyset,   adLockOptimistic  
   
  Set   mstream   =   New   ADODB.Stream  
  mstream.Type   =   adTypeBinary  
  mstream.Open  
  mstream.LoadFromFile   "c:\temp.jpg"  
  rs.Fields("logo").Value   =   mstream.Read  
  rs.Update  
   
  rs.Close  
  cn.Close  
   
   
   
  取:  
  Dim   cn   As   ADODB.Connection  
  Dim   rs   As   ADODB.Recordset  
  Dim   mstream   As   ADODB.Stream  
   
  Set   rs   =   New   ADODB.Recordset  
  rs.Open   "Select   *   from   pub_info",   cn,   adOpenKeyset,   adLockOptimistic  
   
  Set   mstream   =   New   ADODB.Stream  
  mstream.Type   =   adTypeBinary  
  mstream.Open  
  mstream.Write   rs.Fields("logo").Value  
  mstream.SaveToFile   "c:\publogo.gif",   adSaveCreateOverWrite  
   
  rs.Close  
  cn.CloseTop

5 楼yoki(小马哥--鬓微霜,又何妨)回复于 2003-11-01 17:53:06 得分 0

主题:《   在VB中存取数据库中的图片   》    
   
    我最近在做一个人事管理的系统,要求把个人信息存入数据库(包括个人的像片)。费了一个多星期时间研究,终于实现,现在提出来和大家共享。    
    一、   数据库的设计  
   
    数据库可以采用微软的Access97或者SQL   Server来进行,首先新建一张表,取名为Table,添加三个字段,分别是:姓名   Char型(SQL   Server中)文本型(Access中);编号Char型(SQL   Server中)文本型(Access中);照片image型(SQL   Server中)OLE对象(Access中),设计好后存盘。为了可以进行远程调用,我们采用ODBC的方法进行,双击打开控制面板里的ODBC数据源,界面如图1所示:  
    点“系统DSN”选项卡,按“添加”按钮选择对应的数据源驱动程序Access的*.mdb或者SQL   Server,依照添加向导加添加数据源,下面就可以开始程序的编写了。    
    二、   程序的编写  
   
    运行VB,新建一个工程。本程序采用ADO控件和动态链接库访问数据库,需要加入ADO的运行库,单击“工程\引用”菜单,出现引用对话框,选择Microsoft   ActiveX   Data   Objects2.0   Library并确定。  
    添加一个Form,四个Label控件,两个TextBox控件,一个PictureBox控件,一个ADODC控件,三个CommandButton控件,一个CommandDialog控件,如果ADODC和CommandDialog控件没有出现在工具框上,请单击菜单“工程\部件”。点“控件”选项卡,在其中选中Microsoft   ADO   Data   Control   6.0(OLEDB)和Microsoft   Common   Dialog   Control   6.0两项按“确定”按钮。  
    下面是以上各个控件的一些属性:  
    Form1.MaxButton=False  
    Label1.Caption=姓名:  
    Label2.Caption=编号:  
    Label3.Name=   ResName  
    Label3.BackColor=   &H80000009&  
    Label3.BorderStyle=1-Fixed   Single  
    Label3.DataField=姓名  
    Label3.DataSource=   AdoCtr  
    Label4.Name=   ResNumb  
    Label4.BackColor=   &H80000009&  
    Label4.BorderStyle=1-Fixed   Single  
    Label4.DataField=编号  
    Label4.DataSource=   AdoCtr  
    Text1.Name=   Names  
    Text2.Name=   Numb  
    CommonDialog1.Name=   CDlg  
    Adodc1.Name=AdoCtr  
    CommonButton1.Name=PreView  
    CommonButton1.Caption=预览  
    CommonButton2.Name=Save  
    CommonButton2.Caption=保存  
    CommonButton3.Name=   Update  
    CommonButton3.Caption=更新  
    PictureBox1.Name=   PicBox  
    PictureBox1.AutoSize=False  
    PictureBox1.AutoRedraw=False  
    PictureBox1.DataField=照片  
    PictureBox1.DataSource=AdpCtr  
    下面是程序代码:  
    ′此工程需有Microsoft   ActiveX   Data   Object   2.1   Library(msado15.dll)  
    Dim   Constr   As   String   ′ODBC路径  
    Dim   FileName   As   String   ′图片文件名  
    Const   BLOCKSIZE   =   4096   ′每次读写块的大小  
    Dim   ADOCon   As   New   ADODB.Connection   ′ADODB   Connection对象  
    Dim   ADORst   As   New   ADODB.Recordset   ′ADODB   Recordset   对象  
    Dim   ADOFld   As   ADODB.Field   ′ADODB   Field   对象  
  ------------------------  
    Private   Sub   SaveToDB(ByRef   Fld   As   ADODB.Field,   DiskFile   As   String)  
    Dim   byteData()   As   Byte   ′定义数据块数组  
    Dim   NumBlocks   As   Long   ′定义数据块个数  
    Dim   FileLength   As   Long   ′标识文件长度  
    Dim   LeftOver   As   Long′定义剩余字节长度  
    Dim   SourceFile   As   Long   ′定义自由文件号  
    Dim   i   As   Long   ′定义循环变量  
    SourceFile   =   FreeFile   ′提供一个尚未使用的文件号  
    Open   DiskFile   For   Binary   Access   Read   As   SourceFile   ′打开文件  
    FileLength   =   LOF(SourceFile)   ′得到文件长度  
    If   FileLength   =   0   Then   ′判断文件是否存在  
    Close   SourceFile  
    MsgBox   DiskFile   &   ″   无   内   容   或   不   存   在   !″  
    Else  
    NumBlocks   =   FileLength   \   BLOCKSIZE   ′得到数据块的个数  
    LeftOver   =   FileLength   Mod   BLOCKSIZE   ′得到剩余字节数  
    Fld.Value   =   Null  
    ReDim   byteData(BLOCKSIZE)   ′重新定义数据块的大小  
    For   i   =   1   To   NumBlocks  
    Get   SourceFile,   ,   byteData()   ′   读到内存块中  
    Fld.AppendChunk   byteData()   ′写入FLD  
    Next   i  
    ReDim   byteData(LeftOver)   ′重新定义数据块的大小  
    Get   SourceFile,   ,   byteData()   ′读到内存块中  
    Fld.AppendChunk   byteData()   ′写入FLD  
    Close   SourceFile   ′关闭源文件  
    End   If  
    End   Sub  
    ----------------------  
    Private   Sub   Form_Load()  
       Constr   =   ″DSN=image″   ′定义ODBC连接  
       ADOCon.Open   Constr   ′创建一个连接  
       ADORst.Open   ″table″,   ADOCon,   adOpenDynamic,   adLockOptimistic    
    ′打开一个ADO动态集   表名为table  
       Set   AdoCtr.Recordset   =   ADORst   ′将动态集赋给ADO控件  
    End   Sub  
    ----------------------  
    Private   Sub   Form_Unload(Cancel   As   Integer)  
    ′记得关闭打开的数据集,释放资源  
    ADORst.Close  
    ADOCon.Close  
    Set   ADORst   =   Nothing  
    Set   ADOCon   =   Nothing  
    End   Sub  
    ----------------------  
    Private   Sub   PreView_Click()  
    ′显示打开文件的公用对话框,选择需要加入数据库的图片  
    CDlg.Filter   =   ″位图(*.bmp)|*.bmp″  
    CDlg.ShowOpen  
    FileName   =   CDlg.FileName  
    PicBox.Picture   =   LoadPicture(FileName)   ′预览图片  
    End   Sub  
    ----------------------  
    Private   Sub   Save_Click()  
    ADORst.AddNew   ′新增纪录  
    ADORst(″姓名″).Value   =   Names.Text   ′给动态集的第一个字段赋值  
    ADORst(″编号″).Value   =   Numb.Text   ′给动态集的第二个字段赋值  
    Set   ADOFld   =   ADORst(″照片″)   ′给ADODB.Field对象赋值  
    Call   SaveToDB(ADOFld,   FileName)   ′调用子程序,给第三个字段(image)赋值  
    ADORst.Update  
    End   Sub  
    ----------------------  
    Private   Sub   Update_Click()  
    ′重新打开纪录集,刷新纪录  
    ADORst.Close  
    ADOCon.Close  
    Set   ADORst   =   Nothing  
    Set   ADOCon   =   Nothing  
    ADOCon.Open   Constr  
    ADORst.Open   ″table″,   ADOCon,   adOpenDynamic,   adLockOptimistic  
    Set   AdoCtr.Recordset   =   ADORst  
    End   Sub  
    程序运行后的结果如图2。  
    本程序在VB6.0/Windows98/WindowsNT下编译通过。Top

相关问题

  • VB 存取图片入 SQL SERVER
  • VB连接SQL-SERVER
  • 在VB中如何对MS-SQL进行图片的存取
  • 如何通过VB往SQL数据库里存取WORD文档??
  • 如何通过VB象SQL SERVER存取WORD文挡?
  • vb 如何连接sql server?
  • vb与SQL连接问题
  • VB如何连接SQL
  • 请问如何用VB往SQL数据库里存取WORD文挡?
  • 请问VB连接SQL Server的方法

关键词

  • mstream
  • adodb
  • recordsetrs
  • adlockoptimisticset
  • adtypebinarymstream
  • closecn
  • openmstream
  • recordsetdim
  • streammstream
  • logo

得分解答快速导航

  • 帖主:sxhv998
  • TechnoFantasy

相关链接

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

广告也精彩

反馈

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