VB6 保存和读取图片到数据库

dim stm as ADODB.Stream

dim rs as ADODB.Recordset

sub SavePictureToDB(cn   As   ADODB.Connection)  
  '将图片存入数据库  
  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  
   
   
Sub   LoadPictureFromDB(cn   As   ADODB.Connection)  
  '载数据库中读出图片  
  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 

也可用FileSystemObject的方式来保存

Dim Sql As String
Dim fs As New FileSystemObject

Sub SavePicture()
    Dim sByte() As Byte
    Dim bIsNull As Boolean
   
    If fs.FileExists(g_FilePath) Then
        Open g_FilePath For Binary Access Read As #1
        ReDim sByte(1 To LOF(1))
        Get #1, 1, sByte()
        Close #1
        bIsNull = False
    Else
        bIsNull = True
    End If
   
    Dim rs As New ADODB.Recordset
    rs.Open "select empid,empname,pic from emp where empid = '" & Trim(txtEmpId.Text) & "'", cn, adOpenStatic, adLockOptimistic
    rs.AddNew
    rs!EmpId = txtEmpId.Text
    rs!EmpName = txtEmpName.Text
    If bIsNull <> True Then
        rs!pic = sByte
    End If
    rs.Update
    MsgBox "save data ok!"
    txtEmpId.Text = ""
    txtEmpName.Text = ""
    Set picView.Picture = Nothing
    cmdAdd.Enabled = True
End Sub

Sub viewJpg()


    Dim TmpFile As String
    Dim jByte() As Byte

    Sql = "select * from emp"
    Set rsViewJpg = New ADODB.Recordset
    rsViewJpg.Open Sql, cn, adOpenStatic, adLockOptimistic
    rsViewJpg.MoveFirst


    If Not rsViewJpg.BOF Then
        If Not rsViewJpg.EOF Then
            txtEmpId.Text = rsViewJpg!EmpId
            txtEmpName.Text = rsViewJpg!EmpName
            Set pic.Picture = Nothing
            If Not fs.FolderExists(App.Path + "/temp") Then
                fs.CreateFolder (App.Path + "/temp")
            End If
            TmpFile = App.Path + "/Temp/" + rsViewJpg.Fields(0) + ".jpg"
            If Not IsNull(rsViewJpg!pic) Then
                jByte = rsViewJpg!pic
                Open TmpFile For Binary Access Write As #1
                Put #1, , jByte
                Close #1
                pic.Picture = LoadPicture(TmpFile)
            End If
        End If
    End If
End Sub


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值