CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    12

    Database & Image

    Hi!

    I have a SQLserver table with a field whose datatype is Image.
    I have a form with a Picture bound to this
    table field.
    When I´m in AddNew or Update and I load the Image
    (LoadPicture) I can see my image in the form.
    But when i update the register, the image is lost.
    How can I avoid this? Maybe Must I load the image with GetChuck?

    Thank you



  2. #2
    Join Date
    May 1999
    Location
    Geneva
    Posts
    32

    Re: Database & Image

    I give you a class that I have created to do this work. I use it on SQL 7.0


    VERSION 1.0 CLASS
    BEGIN
    MultiUse = -1 'True
    Persistable = 0 'NotPersistable
    DataBindingBehavior = 0 'vbNone
    DataSourceBehavior = 0 'vbNone
    MTSTransactionMode = 0 'NotAnMTSObject
    END
    Attribute VB_Name = "CImageSql"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = True
    Attribute VB_PredeclaredId = False
    Attribute VB_Exposed = True
    Option Explicit

    Const BLOCK_SIZE = 16384

    Public Sub SQLToFile(fld As ADODB.Field, ByVal FName As String, _
    Optional FieldSize As Long = -1, _
    Optional Threshold As Long = 1048576)
    '
    ' Assumes file does not exist
    ' Data cannot exceed approx. 2Gb in size
    '
    Dim F As Long, bData() As Byte, sData As String
    F = FreeFile
    Open FName For Binary As #F
    Select Case fld.Type
    Case adLongVarBinary
    If FieldSize = -1 Then ' blob field is of unknown size
    WriteFromUnsizedBinary F, fld
    Else ' blob field is of known size
    If FieldSize > Threshold Then ' very large actual data
    WriteFromBinary F, fld, FieldSize
    Else ' smallish actual data
    bData = fld.Value
    Put #F, , bData ' PUT tacks on overhead if use fld.Value
    End If
    End If
    Case adLongVarChar
    If FieldSize = -1 Then
    WriteFromUnsizedText F, fld
    Else
    If FieldSize > Threshold Then
    WriteFromText F, fld, FieldSize
    Else
    sData = fld.Value
    Put #F, , sData ' PUT tacks on overhead if use fld.Value
    End If
    End If
    End Select
    Close #F
    End Sub

    Private Sub WriteFromBinary(ByVal F As Long, fld As ADODB.Field, _
    ByVal FieldSize As Long)
    Dim Data() As Byte, BytesRead As Long
    Do While FieldSize <> BytesRead
    If FieldSize - BytesRead < BLOCK_SIZE Then
    Data = fld.GetChunk(FieldSize - BLOCK_SIZE)
    BytesRead = FieldSize
    Else
    Data = fld.GetChunk(BLOCK_SIZE)
    BytesRead = BytesRead + BLOCK_SIZE
    End If
    Put #F, , Data
    Loop
    End Sub

    Private Sub WriteFromUnsizedBinary(ByVal F As Long, fld As ADODB.Field)
    Dim Data() As Byte, Temp As Variant
    Do
    Temp = fld.GetChunk(BLOCK_SIZE)
    If IsNull(Temp) Then Exit Do
    Data = Temp
    Put #F, , Data
    Loop While LenB(Temp) = BLOCK_SIZE
    End Sub

    Private Sub WriteFromText(ByVal F As Long, fld As ADODB.Field, _
    ByVal FieldSize As Long)
    Dim Data As String, CharsRead As Long
    Do While FieldSize <> CharsRead
    If FieldSize - CharsRead < BLOCK_SIZE Then
    Data = fld.GetChunk(FieldSize - BLOCK_SIZE)
    CharsRead = FieldSize
    Else
    Data = fld.GetChunk(BLOCK_SIZE)
    CharsRead = CharsRead + BLOCK_SIZE
    End If
    Put #F, , Data
    Loop
    End Sub

    Private Sub WriteFromUnsizedText(ByVal F As Long, fld As ADODB.Field)
    Dim Data As String, Temp As Variant
    Do
    Temp = fld.GetChunk(BLOCK_SIZE)
    If IsNull(Temp) Then Exit Do
    Data = Temp
    Put #F, , Data
    Loop While Len(Temp) = BLOCK_SIZE
    End Sub

    Public Sub FileToSQL(ByVal FName As String, fld As ADODB.Field, _
    Optional Threshold As Long = 1048576)
    '
    ' Assumes file exists
    ' Assumes calling routine does the UPDATE
    ' File cannot exceed approx. 2Gb in size
    '
    Dim F As Long, Data() As Byte, FileSize As Long
    F = FreeFile
    Open FName For Binary As #F
    FileSize = LOF(F)
    Select Case fld.Type
    Case adLongVarBinary
    If FileSize > Threshold Then
    ReadToBinary F, fld, FileSize
    Else
    Data = InputB(FileSize, F)
    fld.Value = Data
    End If
    Case adLongVarChar
    If FileSize > Threshold Then
    ReadToText F, fld, FileSize
    Else
    fld.Value = Input(FileSize, F)
    End If
    End Select
    Close #F
    End Sub

    Private Sub ReadToBinary(ByVal F As Long, fld As ADODB.Field, _
    ByVal FileSize As Long)
    Dim Data() As Byte, BytesRead As Long
    Do While FileSize <> BytesRead
    If FileSize - BytesRead < BLOCK_SIZE Then
    Data = InputB(FileSize - BytesRead, F)
    BytesRead = FileSize
    Else
    Data = InputB(BLOCK_SIZE, F)
    BytesRead = BytesRead + BLOCK_SIZE
    End If
    fld.AppendChunk Data
    Loop
    End Sub

    Private Sub ReadToText(ByVal F As Long, fld As ADODB.Field, _
    ByVal FileSize As Long)
    Dim Data As String, CharsRead As Long
    Do While FileSize <> CharsRead
    If FileSize - CharsRead < BLOCK_SIZE Then
    Data = Input(FileSize - CharsRead, F)
    CharsRead = FileSize
    Else
    Data = Input(BLOCK_SIZE, F)
    CharsRead = CharsRead + BLOCK_SIZE
    End If
    fld.AppendChunk Data
    Loop
    End Sub




    Christian Niquille


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured