Reading binary data from SQL Server database


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading binary data from SQL Server database

  1. #1
    Danny Desmet Guest

    Reading binary data from SQL Server database


    I use this code to put the binary data from a SQL Server database in a file.
    Why can I not open the file afterwords ?

    Dim cn As New ADODB.Connection
    Dim rstPubInfo As New 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
    Dim StrMsg As Variant
    Const ConChunkSize = 100

    ' Set connection '
    Let strCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial
    Catalog=pubs;Data Source=SQLServer"
    cn.Open strCn, "sa"

    ' Open recordset - Table pub_info
    rstPubInfo.CursorLocation = adUseServer
    rstPubInfo.Open "pub_info", strCn, adOpenDynamic, adLockOptimistic

    Open "c:\test.bmp" For Binary Access Write Lock Write As #1

    rstPubInfo.Filter = "pub_id ='1266'"
    Let lngOffset = 0
    Let lngLogoSize = rstPubInfo!logo.ActualSize

    ' Reading out the binary data and putting it in the file test.bmp

    Do While lngOffset < lngLogoSize
    Let VarChunk = rstPubInfo!logo.GetChunk(ConChunkSize)
    Put #1, , VarLogo
    Let VarLogo = varlog & VarChunk
    Let lngOffset = lngOffset + ConChunkSize
    If lngOffset > lngLogoSize Then Exit Do
    Loop

    Close #1

  2. #2
    Ian Drake Guest

    Re: Reading binary data from SQL Server database


    Danny -
    I would recomend moving the Put# statement out of the Loop. Use the GetChunk
    Method like you are, put all your chunks together and then write. It looks
    at though if your loop construct did not have to loop then you would be writing
    VarLogo to the file before you assigned anything to it.

    Ian Drake

    PS I think it's time to drop the Let statements!


    "Danny Desmet" <dovy@skynet.be> wrote:
    >
    >I use this code to put the binary data from a SQL Server database in a file.
    >Why can I not open the file afterwords ?


    >


    > Dim cn As New ADODB.Connection
    >Dim rstPubInfo As New 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
    >Dim StrMsg As Variant
    >Const ConChunkSize = 100
    >
    >' Set connection '
    >Let strCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial
    >Catalog=pubs;Data Source=SQLServer"
    >cn.Open strCn, "sa"
    >
    >' Open recordset - Table pub_info
    >rstPubInfo.CursorLocation = adUseServer
    >rstPubInfo.Open "pub_info", strCn, adOpenDynamic, adLockOptimistic
    >
    >Open "c:\test.bmp" For Binary Access Write Lock Write As #1
    >
    >rstPubInfo.Filter = "pub_id ='1266'"
    >Let lngOffset = 0
    >Let lngLogoSize = rstPubInfo!logo.ActualSize
    >
    >' Reading out the binary data and putting it in the file test.bmp
    >
    >Do While lngOffset < lngLogoSize
    > Let VarChunk = rstPubInfo!logo.GetChunk(ConChunkSize)
    > Put #1, , VarLogo
    > Let VarLogo = varlog & VarChunk
    > Let lngOffset = lngOffset + ConChunkSize
    > If lngOffset > lngLogoSize Then Exit Do
    >Loop
    >
    >Close #1



  3. #3
    Danny Desmet Guest

    Re: Reading binary data from SQL Server database


    Ian,
    Thanks for your suggestion.
    I am sorry but it do not solve the problem.
    Have you any idee wher I can find more information about the subject.


    Danny Desmet



    "Ian Drake" <ian.drake1@home.com> wrote:
    >
    >Danny -
    >I would recomend moving the Put# statement out of the Loop. Use the GetChunk
    >Method like you are, put all your chunks together and then write. It looks
    >at though if your loop construct did not have to loop then you would be

    writing
    >VarLogo to the file before you assigned anything to it.
    >
    >Ian Drake
    >
    >PS I think it's time to drop the Let statements!
    >
    >
    >"Danny Desmet" <dovy@skynet.be> wrote:
    >>
    >>I use this code to put the binary data from a SQL Server database in a

    file.
    >>Why can I not open the file afterwords ?


    >
    >>


    >
    >> Dim cn As New ADODB.Connection
    >>Dim rstPubInfo As New 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
    >>Dim StrMsg As Variant
    >>Const ConChunkSize = 100
    >>
    >>' Set connection '
    >>Let strCn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial
    >>Catalog=pubs;Data Source=SQLServer"
    >>cn.Open strCn, "sa"
    >>
    >>' Open recordset - Table pub_info
    >>rstPubInfo.CursorLocation = adUseServer
    >>rstPubInfo.Open "pub_info", strCn, adOpenDynamic, adLockOptimistic
    >>
    >>Open "c:\test.bmp" For Binary Access Write Lock Write As #1
    >>
    >>rstPubInfo.Filter = "pub_id ='1266'"
    >>Let lngOffset = 0
    >>Let lngLogoSize = rstPubInfo!logo.ActualSize
    >>
    >>' Reading out the binary data and putting it in the file test.bmp
    >>
    >>Do While lngOffset < lngLogoSize
    >> Let VarChunk = rstPubInfo!logo.GetChunk(ConChunkSize)
    >> Put #1, , VarLogo
    >> Let VarLogo = varlog & VarChunk
    >> Let lngOffset = lngOffset + ConChunkSize
    >> If lngOffset > lngLogoSize Then Exit Do
    >>Loop
    >>
    >>Close #1

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center