• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

MSSQL - VB

I am using MSSQL to store an image/tiff file, under the image datatype. I want to retrieve the image file from the database for viewing through VB. The codes below is to retrieve the image file, which is rather correct if I am not mistaken.

ChunkSize = objRS("Fax").ActualSize
ImageFile = (objRS("Fax").GetChunk(ChunkSize))

The problem is if the actual image size is 10000 bytes, all I get to retrieve is 5000 bytes, 50% of the actual file size. I tried it using different file formats but it is still the same. When I edited the retrieved image file's contents... I found garbled characters different from the actual image file's contents. Anybody know the answer to this and the way to solve it. Please any ideas... I tried MS Knowledgebase and Technet but I cant find the solution.
0
LawrenceY
Asked:
LawrenceY
  • 3
1 Solution
 
wqwCommented:
the error is in

ImageFile = (objRS("Fax").GetChunk(ChunkSize))

should be

ImageFile = objRS("Fax").GetChunk(ChunkSize)

and ImageFile must be

Dim ImageFile() As Byte

The parentheses convert the byte array to string which is the intrinsic way in VB to convert from Unicode to Ascii. so you get 1 byte for every 2 bytes :-))

Let me know if this works for you.

HTH,

</wqw>
0
 
LawrenceYAuthor Commented:
The codes cant work. Below is my actual program codes.

objRS.MoveFirst (my database)
    For i = 1 To numrecords (total records)
        ChunkSize = objRS("Image").ActualSize
        FaxMailData = objRS("Image").GetChunk(ChunkSize)
        Image2 = Image        ChunkSize2 = Len(Image2)
        MsgBox "ActualSize=" & ChunkSize & " | " & "ConvertedSize=" & ChunkSize2    'Debug
        MsgBox FaxMailData()    'Debug
        objRS.MoveNext
    Next i

Find anything wrong with it? Please anybody tell me. Thanks.    
0
 
wqwCommented:
give me the Dim's. each variable declaration

</wqw>
0
 
TimCotteeCommented:
You should use getchunk in a loop to retrieve the data in pieces rather than by trying to get the whole lot. There are restrictions on the number of bytes that can be extracted from a field in one go, this is defaulted to 5K unless you use settextsize etc which can prove a real pain!

This example gets the data in 1K blocks from the field.

   conChunkSize = 1000
   lngLogoSize = rstPubInfo!logo.ActualSize
   Do While lngOffset < lngLogoSize
      varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
      varLogo = varLogo & varChunk
      lngOffset = lngOffset + conChunkSize
   Loop
   
0
 
wqwCommented:
sorry for the late call. check this code.

i created a table first in northwind db with this sql:

create table images(id int identity, blob image)

you need a form in VB with 2 command buttons, 2 picture boxes and a common dialog control (ctr+t - components dialog). leave default names. reference ado 2.1 too. paste this code in your form

Option Explicit

Private Const CONN_STR = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"

Private Sub Command1_Click()
    Dim rs As New Recordset, oPic As StdPicture, oBag As New PropertyBag
    rs.CursorLocation = adUseClient
    rs.Open "images", CONN_STR, adOpenKeyset, adLockOptimistic
    CommonDialog1.Filter = "Graphics (*.bmp;*.gif;*.jpg)|*.bmp;*.gif;*.jpg|All files (*.*)|*.*"
    CommonDialog1.ShowOpen
    If CommonDialog1.FileName <> "" Then
        Set oPic = LoadPicture(CommonDialog1.FileName)
        oBag.WriteProperty "FileName", CommonDialog1.FileName
        oBag.WriteProperty "Image", oPic
        rs.AddNew
        rs!Blob = oBag.Contents
        rs.Update
        Set Picture1.Picture = oPic
    End If
End Sub

Private Sub Command2_Click()
    Dim rs As New Recordset, oBag As New PropertyBag
    rs.CursorLocation = adUseClient
    rs.Open "images", CONN_STR, adOpenKeyset, adLockOptimistic
    If Not rs.EOF Then
        rs.MoveLast
        oBag.Contents = rs!Blob.Value
        Set Picture2.Picture = oBag.ReadProperty("Image")
    End If
End Sub

basicly this code save images to image field. unfortunately uncompressed. may i suppose it's datatype problem that you have -- binary field instead of image. binary field can take up to 8060 bytes of data.

let me know if this is working for you.

HTH,

</wqw>
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now